Writing special characters from one batch file to another

人走茶凉 提交于 2020-03-16 08:34:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!