我有以下批处理文件来查找并删除文本文件中的字符串。文本文件将采用以下格式:
079754,Billing & Business Adv..,E:\MyDirectory\079754_35931_Billing & Business Adv...pdf,Combined PDF
我只想从文件中删除“E:\MyDirectory\”,然后将文件移动到子目录。我的批处理文件按预期工作,除了文件中存在&符号的情况(例如上面的)。
而不是我的结果文件包含:
079754,Billing & Business Adv..,Billing & Business Adv...pdf,Combined PDF
相反,它包含,
079754,Billing
我对编写批处理文件有点陌生,我知道&号会以某种方式影响标记化。任何帮助将不胜感激!
批处理文件:
@echo off
cd C:\Temp\broker
for %%f in (*.dat) do (
if exist newfile.txt del newfile.txt
FOR /F "tokens=* delims=" %%a in (%%f) do @call :Change "%%a"
del %%f
rename newfile.txt %%f
move %%f "import\%%f"
)
exit /b
pause
:Change
set Text=%~1
set Text=%Text:E:\MyDirectory\=%
FOR /F "tokens=3 delims=," %%d in ("%Text%") do @set File=%%d
(echo %Text%)>> newfile.txt
move "%File%" "import\%File%"
exit /b
你应该引用像这样的命令set
,逃避&
和其他特殊字符。
并使用延迟扩展,因为延迟扩展会忽略特殊字符。
并且百分比扩展是在执行块之前评估的,因此您的 for 循环无法按预期工作。
setlocal EnableDelayedExpansion
...
:Change
set "Text=%~1"
set "Text=!Text:E:\MyDirectory\=!"
FOR /F "tokens=3 delims=," %%d in ("!Text!") do @set File=%%d
(echo !Text!)>> newfile.txt
move "!File!" "import\!File!"
exit /b
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)