提问人:fortissimo 提问时间:1/16/2023 最后编辑:fortissimo 更新时间:1/16/2023 访问量:81
字符串变量中的 & 符号扰乱了执行
ampersands inside of string variables are messing up execution
问:
批处理文件中下面的代码片段工作得很好,但当字符串变量中有 & 符号时不起作用,不幸的是,这将发生在我正在处理的项目中。有没有办法重写这两个语句,以便与号不会劫持语句的执行?
如果这样做的“目的”不明显,我深表歉意;我必须粘贴大量的线条才能真正清楚地看到我想做什么。我希望人们能够专注于这些线条的微观层面,而不是让明显缺乏“大局观”成为障碍。
除了此批处理文件之外,还有一个名为 file.txt 的文件,其中只有一行:.以下是批处理文件代码:/Here and There/Over & Yonder/This & That
setlocal enabledelayedexpansion
set "FolderName=This & That"
set "ChosenFolder=Here & There/Over & Yonder/This & That"
set "NewFolder=Yin & Yang/Up & Down"
:: Ideally the following problematic line would change the value of %ChosenFolder% to 'Here & There/Over & Yonder', but the ampersands make it fail
For /f "delims=" %%a in ("/!FolderName!") do set "ChosenFolder=!ChosenFolder:%%a=!"
set /a count=0
:: The next problematic line should search file.txt for a string that matches "/Here & There/Over & Yonder" and count the number of matches, but the ampersands make it fail
for /f "delims=" %%a in ('findstr /i /n /c:"/!ChosenFolder!" file.txt" ^| find /c /v ""') do set "count=%%a">nul
:: And finally this problematic line is supposed to actually replace the string with "Yin & Yang/Up & Down"
if %count% gtr 0 type file.txt"|jrepl "/!ChosenFolder!" "/!NewFolder!" /l >temp.txt
echo.
echo FolderName: %FolderName%
echo ChosenFolder: %ChosenFolder% - should be "Here & There/Over & Yonder"
echo NewFolder: %NewFolder% - should be "Yin & Yang/Up & Down"
echo.
echo file.txt should display "Yin & Yang/Up & Down"
type file.txt
echo.
这不是简单地转义各种字符可以解决的问题。字符串本身必须是“阴阳”等,而不是、等,因为它们将用于文件名、页面标题等,转义字符本身最终会显示出来。我以前见过这种事情非常棘手的解决方案,比如放在语句前面,将语句括在括号中,或者,正如我在下面尝试的那样,将它们粘在循环中......我只是无法自己破解它。Yin && Yang
Yin ^& Yang
call
for
这使用 jrepl.bat,可以在这里找到。作为旁注,我很高兴考虑文本文件搜索/替换的替代方案,这些替代方案不需要我将鸡蛋放入一些 Windows 12 或 Windows 13 中可能不支持的 .exe 命令行实用程序中,例如我像 20 年前一样使用的精彩“srch.exe”文件,但今天不再有效。
答:
1赞
Gerhard
1/16/2023
#1
我不确定你的预期结果应该是什么,因为你声称包含什么,但你从不替换任何东西,而是将输出重定向到 .file.txt
file.txt
temp.txt
无论如何,这里是您的代码工作示例:
@echo off
setlocal enabledelayedexpansion
set "FolderName=This & That"
set "ChosenFolder=Here & There/Over & Yonder/This & That"
set "NewFolder=Yin & Yang/Up & Down"
For /f "delims=" %%a in ("/!FolderName!") do set "ChosenFolder=!ChosenFolder:%%a=!"
set /a count=0
for /f "delims=" %%a in ('findstr /i /n /c:"/!ChosenFolder!" "file.txt" ^| find /c /v ""') do set "count=%%a"
if %count% gtr 0 type "file.txt"|"jrepl.bat" "/!ChosenFolder!" "/!NewFolder!" /l > temp.txt
echo(
echo FolderName: "%FolderName%"
echo ChosenFolder: "%ChosenFolder%" - should be "Here & There/Over & Yonder"
echo NewFolder: "%NewFolder%" - should be "Yin & Yang/Up & Down"
echo(
echo file.txt should display "This & That"
type file.txt
echo temp.txt should display "/Yin & Yang/Up & Down/This & That"
type temp.txt
echo.
评论
!
%
&
&
for
%SystemRoot%\System32\findstr.exe /MLIC:"!OldChosenFolder!/!OldFolderName!" "..\source\!ChosenFolder!\!FolderName!\index.txt" 2>NUL 1>&2 && type "..\source\!ChosenFolder!\!FolderName!\index.txt" | P:\athTo\jrepl.bat "!OldChosenFolderLinux!/!OldFolderName!" "!OldChosenFolderLinux!/!NewFolderName!" /l 1>"temp.txt"