Windows批处理文件和“&" [英] Windows Batch Files and "&"
问题描述
我正在使用以下批处理文件在网络媒体文件夹中创建排序的.m3u播放列表:
I am using the following batch file to created sorted .m3u playlists within my networked media folder:
@Echo Off
color 0e
Echo PLEASE WAIT, BUILDING PLAYLIST FILE, EXCLUDING BAT, TXT, M3U, SRT and JPG FILES
del "playlist.m3u"
Setlocal EnableDelayedExpansion
Set _t0=1
If ["%CD%"]==["%CD:~0,3%"] Set _t0=0
For /F "tokens=*" %%A In ('Dir "%*" /a-d /b /on /s ^|findstr /v /i ".bat .m3u .txt .db .srt .jpg"^| sort') Do (
Set _t1=%%A
Set _t2=!_t1:%CD%=!
Echo !_t2:~%_t0%!)>>playlist.m3u
示例文件夹结构:
E:\Share\Media\TV\SHOW_NAME\Season 1\
E:\Share\Media\TV\SHOW_NAME\Season 2\
...等
示例脚本输出(playlist.m3u):
Sample script output (playlist.m3u):
SHOW_NAME\Season 1\S01E01.avi
SHOW_NAME\Season 1\S01E02.avi
SHOW_NAME\Season 1\S01E03.avi
SHOW_NAME\Season 2\S02E01.avi
...等
您会注意到.m3u缺少完整路径(这是有意的),因为我希望脚本具有灵活性.用户可以将驱动器映射到任何深度/驱动器号,或者以UNC( domain.com \ Share \ Media \ TV \ SHOW_NAME
)等方式访问共享,.m3u播放列表仍然可以使用.我最近遇到的麻烦是,如果文件夹中包含&"符号,该脚本将无法正常运行.我创建了一个示例文件夹:
You will notice that the .m3u is lacking the full path (this is intentional) as I want the script to be flexible. Users can map the drive at any depth/drive letter, or access the share as UNC (domain.com\Share\Media\TV\SHOW_NAME
)... etc and the .m3u playlists still function. The trouble I am having lately is that if a folder contains the ampersand (&) symbol, the script does not function as intended. I created a sample folder:
E:\Share\Media\NEW&FOLDER\Season 1
.m3u播放列表示例:
Sample .m3u playlist:
t1:E:\Share\Media\NEW
t1:E:\Share\Media\NEW
t1:E:\Share\Media\NEW
t1:E:\Share\Media\NEW
t1:E:\Share\Media\NEW
PROMPT报告以下内容:
The PROMPT reports the following:
Set _t2=!_t1:E:\Share\Media\NEW & FOLDER=!
Echo !_t2:~1!
) 1>>playlist.m3u
'FOLDER' is not recognized as an internal or external command,
operable program or batch file.
我做错了什么?在创建此批处理文件的过程中,我从这个论坛中获得了很多帮助-我们将不胜感激.
What am I doing wrong? I had much help from this forum in creating this batch file - any insights would be appreciated.
P.S.已在Windows 7和Windows Server 2012R2上对此进行了测试.
P.S. have tested this on Windows 7 and Windows Server 2012R2.
推荐答案
始终使用 set"variable = value"
来避免批处理代码中出现此类问题.在批处理代码中,您需要使用例如 Set"_t2 =!_ t1:%CD%=!"
Always use set "variable=value"
to avoid such problems in your batch code. In your batch code you need to use for example Set "_t2=!_t1:%CD%=!"
&
具有特殊含义,但双引号引起来的字符串除外.
&
has a special meaning, except in double quoted strings.
在命令提示符窗口中运行 cmd/?
并至少读取该命令输出的最后帮助页面,以真正过滤出具有指定文件扩展名的文件.
Run cmd /?
in a command prompt window and read at least last help page output by this command to really filter out files with the specified file extensions.
这是您的宏,其中还另外更新了命令 findstr
的参数.需要/L
来将.
解释为点而不是正则表达式字符以匹配除换行符以外的任何字符.必须使用选项/E
才能使文件扩展名的正匹配仅位于文件名的末尾.
Here is your macro with additionally updating also parameters of command findstr
. /L
is needed to interpret .
as dot and not as regular expression character for matching any character except newline character. Option /E
is required to get a positive match for the file extension only at end of a file name.
@echo on
color 0e
echo PLEASE WAIT, BUILDING PLAYLIST FILE, EXCLUDING BAT, TXT, M3U, SRT and JPG FILES
if exist "playlist.m3u" del "playlist.m3u"
Setlocal EnableDelayedExpansion
set "_t0=1"
if "%CD%"=="%CD:~0,3%" set "_t0=0"
for /F "tokens=*" %%A In ('dir %* /a-d /b /on /s ^| %SystemRoot%\System32\findstr.exe /V /I /E /L ".bat .m3u .txt .db .srt .jpg" ^| %SystemRoot%\System32\sort.exe') do (
set "_t1=%%A"
set "_t2=!_t1:%CD%=!"
echo !_t2:~%_t0%!>>playlist.m3u
)
endlocal
这篇关于Windows批处理文件和“&"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!