batch-file - 用换行符替换内容
问题描述
我正在使用 bat 脚本替换另一个 bat 文件中的内容。除此之外,我想添加换行符,但 "\n" 反映的是 outfile.bat 中的换行符而不是换行符
%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\powershell.exe -Command "(gc outfile.txt) -replace '
svn', ' \n C:\Projects\TortoiseSVN\bin\svn.exe' | Out-File -编码默认 outfile.bat"
有人可以帮我吗?
解决方案
您应该使用反引号而不是反斜杠,并且包含它的替换字符串应该是双引号而不是单引号。由于这些双引号将嵌套在双引号命令中,因此您需要使用反斜杠对 cmd.exe 进行转义,以便根据需要将它们传递给 powershell.exe:
%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -Command "(Get-Content 'outfile.txt') -Replace 'svn', \"`nC:\Projects\TortoiseSVN\bin\svn.exe\" | Out-File -Encoding Default 'outfile.bat'"
我不经常使用单引号,更喜欢使用双引号,这意味着在双引号命令中,我需要转义所有这些,而不仅仅是包含换行符的那个:
%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile "(Get-Content \"outfile.txt\") -Replace \"svn\", \"`nC:\Projects\TortoiseSVN\bin\svn.exe\" | Out-File -Encoding Default \"outfile.bat\""