问题
I am trying to write one batch file from another, using echo
. However, there is one line with special characters that I cannot work out how to write.
I have the following line:
echo >nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system" >> "c:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp\OrchestrationCleanUp.bat"
But what gets written to the target file is:
"C:\Windows\system32\cacls.exe" "C:\Windows\system32\config\system"
It should be:
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
I have tried putting it in double quotes.
I have tried putting ^ in front of the >
I have tried putting it in a variable and escaping it with double quotes
I have tried putting it in a variable and using ! at each end
I have tried putting it in a variable and using a : at the end of the variable name.
回答1:
The special characters need to be escaped with ^
and %
signs have to be doubled. Try this:
The (
directly after echo stops issues with some leading characters.
echo(^>nul 2^>^&1 "%%SYSTEMROOT%%\system32\cacls.exe" "%%SYSTEMROOT%%\system32\config\system" >> "c:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp\OrchestrationCleanUp.bat"
来源:https://stackoverflow.com/questions/19486995/writing-special-characters-from-one-batch-file-to-another