I am writing a batch file and expect to output three lines of string sentences to a txt file, just like this:
powershell "Get-appxprovisionedpackage -Online"
complete
123456
however, the first sentence: powershell "Get-appxprovisionedpackage -Online" contains a space that breaks the string in ouput file, like this:
powershell
"Get-appxprovisionedpackage -Online"
complete
123456
And I don't hope to have quotation mark "" on the first sentence in the output file because I need it can be executed as a powershell command, any experts can kindly give a help on this and advise. Thanks a lot
My batch code:
@ECHO on
SETLOCAL enableDelayedExpansion
for %%A in (
powershell "Get-appxprovisionedpackage -Online"
complete
123456
) do echo %%A>> D:\BatOutput.txt
CodePudding user response:
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
(
for %%A in (
"powershell #Get-appxprovisionedpackage -Online#"
complete
123456
) do SET "line=%%~A"&ECHO !line:#="!
)>filename
GOTO :EOF
Here's a way to do it - but I prefer Mofi's method.
Simply replace each " with an otherwise-unused character. Quote any string containing spaces. Assign the value in %%A to a user-variable line, using %%~A to remove enclosing quotes. Then echo the result in line, substituting " for #.
Enclosing the code in a pair of parentheses forms a code block which allows the output to be redirected to the file. That way, the output file is opened once; using > to start, then >> to append is ugly and repeatedly opens/closes the file.
See for /? and set /? from the prompt for documentation - or browse through thousands of examples on SO
CodePudding user response:
Simple:
@ECHO on
SETLOCAL enableDelayedExpansion
for /F "delims=" %%A in (^"
powershell "Get-appxprovisionedpackage -Online"^
complete^
123456
^") do echo %%A>> D:\BatOutput.txt
Just change the FOR by FOR /F "delims=", terminate each line with a caret ^ character and insert a blank line between lines...
Although a simpler solution would be to store the desired lines in a file and just copy or type/append the file
