码:
@echo on & setlocal ENABLEEXTENSIONS set "InFile=%~1" set "OutFile=%~2" set "Replace=%~3" CALL :ParseCue "%%InFile%%" "%%OutFile%%" "%%Replace%%" endlocal &GOTO:EOF :ParseCue @echo on & setlocal ENABLEEXTENSIONS DISABLEDELAYEDEXPANSION set "FileToParse=%~1" set "OutputFile=%~2" set "NewExtension=%~3" for /F "usebackq tokens=* delims=" %%a in ("%FileToParse%") DO ( set "line=%%a" @echo on & setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION set "line=!line:.wav=%NewExtension%!" echo(!line!>>"%OutputFile%" endlocal ) endlocal &GOTO:EOF
InputFile.txt:
This a test for parsing lines with special characters Rock & Roll.wav Rock & Roll!.wav Special | < > ~ \ ²³ { [ ] } ! " ´ ' ` üäö @ ; : € $% & / ( ) = ? chars.wav
命令行语法:
D:\Users\Public\Batch\YAET>parse.bat "InputFile.txt" "OutputFile.txt" ".flac"
OutputFile.txt:
This a test for parsing lines with special characters Rock & Roll.flac Rock & Roll!.flac Special | < > ~ \ ²³ { [ ] } ! " ´ ' ` üäö @ ; : € $% & / ( ) = ? chars.flac
编辑/补充:
1年半后,我不得不再次使用此代码段.请参阅另外两个处理毒性字符的示例.第一个是暂时启用延迟扩展(请参阅Ansgars答案),第二个使用CALL.两者都将解析当前目录中和下面的非空文件的路径和名称,但不会跟踪驱动器号和当前目录的路径.
示例#1(在“File =!File …”集合中包含双引号,并且不需要echo“!FILE!”>> …):
@echo off & setlocal ENABLEEXTENSIONS DISABLEDELAYEDEXPANSION del NonEmptyFiles.txt >NUL 2>&1 echo Searching non-empty files in and below current directory ... for /f "tokens=*" %%I in ('dir /s /b /a:-D') do ( if not %%~zI==0 ( set "File=%%I" setlocal ENABLEDELAYEDEXPANSION set "File=!File:%cd%\=!" echo "!File!">> NonEmptyFiles.txt endlocal ) ) echo Done. See NonEmptyFiles.txt. endlocal &goto:EOF
示例#2(较慢,需要包含双引号):
@echo off & setlocal ENABLEEXTENSIONS DISABLEDELAYEDEXPANSION del NonEmptyFiles.txt >NUL 2>&1 echo Searching non-empty files in and below current directory ... for /f "tokens=*" %%i in ('dir /s /b /a:-D') do ( if not %%~zi==0 ( set "File=%%i" call set "File=%%File:%cd%\=%%" call echo "%%File%%">> NonEmptyFiles.txt ) ) echo Done. See NonEmptyFiles.txt. endlocal &goto:EOF
D:\Martin\Any & Path>dir /s /b /a:-D D:\Martin\Any & Path\Hello! World!.txt D:\Martin\Any & Path\Rock & Roll\!File! !!File!!.txt D:\Martin\Any & Path\Rock & Roll\%File% %%File%% %%I.txt D:\Martin\Any & Path\Rock & Roll\Poison! !§$%&()=`´'_;,.-#+´^ßöäüÖÄÜ°^^#.txt D:\Martin\Any & Path\Rock & Roll\SizeZero.txt
输出:
D:\Martin\Any & Path>stringinforloop.bat Searching non-empty files in and below current directory ... See NonEmptyFiles.txt. Done. D:\Martin\Any & Path>type NonEmptyFiles.txt "Hello! World!.txt" "Rock & Roll\!File! !!File!!.txt" "Rock & Roll\%File% %%File%% %%I.txt" "Rock & Roll\Poison! !§$%&()=`´'_;,.-#+´^ßöäüÖÄÜ°^^#.txt"
享受批量!马丁
set "line=%%a" @echo on & setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION set "line=!line:.wav=%NewExtension%!"
如果在分配%% a之前启用延迟扩展:
@echo on & setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION set "line=%%a" set "line=!line:.wav=%NewExtension%!"
你会得到
Special | < > ~ \ ²³ { [ ] } € $% & / ( ) = ? chars.flac
代替
Special | < > ~ \ ²³ { [ ] } ! ” ´ ‘ ` üäö @ ; : € $% & / ( ) = ? chars.flac
编辑:延迟扩展控制何时扩展语句中的变量.脚本中的关键语句就是行
set "line=%%a"
它将循环变量%% a的值赋给变量行.如果禁用延迟扩展,则会分配%% a的文字值,因为脚本解释器无法在分析时扩展%% a.但是,当您启用延迟扩展时,爆炸变量会在执行时扩展,因此解释器会查看%% a的值并展开任何!在将结果分配给变量行之前.
一个例子也许会变得更加清晰.如果添加一行
%foo% !foo!
到输入文件并在脚本中定义变量:
@echo on & setlocal ENABLEEXTENSIONS set "foo=bar" set "InFile=%~1" ...
在设置“line = %% a”之后启用延迟扩展时,既没有%foo%也没有!foo!在%% a被赋值给变量行之前被扩展(解释器在执行时间之前没有看到%% a的值),所以你得到这个输出:
%foo% !foo!
在设置“line = %% a”之前启用延迟扩展时,解释器会在将结果赋给变量行之前扩展爆炸变量,因此您将获得此输出:
%foo% bar
%foo%只会在解析时扩展,此时解释器无法看到%% a的实际值,因此%foo%在这里仍然是文字%foo%.
进一步的分配,比如set“line =!line:.wav =%NewExtension%!”不要影响变量中的刘海或百分号,因为扩展不是传递的,即它会翻译!行!到%foo%bar(或%foo%!foo!)然后停止.
但是,您可以使用call命令强制扩展变量内的(百分比)变量.命令调用设置为“line =!line!”首先在当前上下文中扩展为调用set“line =%foo%bar”,然后在新的上下文中调用set“line =%foo%bar”,其中%foo%也扩展为bar,因此变量行被赋值为条形栏.
正如旁注:你的代码太复杂了.你会得到完全相同的结果:
set "FileToParse=%~1" set "OutputFile=%~2" set "NewExtension=%~3" for /F "usebackq tokens=* delims=" %%a in ("%FileToParse%") DO ( set "line=%%a" @setlocal ENABLEDELAYEDEXPANSION set "line=!line:.wav=%NewExtension%!" echo(!line!>>"%OutputFile%" endlocal )