Windows 批处理脚本 - 如何过滤具有定义扩展名的文件

Jin*_* Ho 2 windows for-loop cmd batch-file

我想在变量中定义扩展名的图像上做一些事情。以下脚本运行良好:

set AllowExt="jpg png bmp"
forfiles /p D:\Pictures /m *.* /c "cmd /c if not %AllowExt:jpg=% == %AllowExt% echo @file
Run Code Online (Sandbox Code Playgroud)

但以下脚本引发错误

set AllowExt="jpg png bmp"
forfiles /p D:\Pictures /m *.* /c "cmd /c if not %AllowExt:@ext=% == %AllowExt% echo @file"
Run Code Online (Sandbox Code Playgroud)

错误:无效的参数/选项 - 'png'。输入“文件/?” 用于使用。

End*_*oro 5

你可以试试这个:

set "AllowExt=.jpg .png .bmp"
for %%a in (%AllowExt%) do (
  forfiles /p D:\Pictures /m *%%a /c "cmd /c echo @file"
)
Run Code Online (Sandbox Code Playgroud)

"cmd /c echo @file"是默认命令,请参阅forfiles /?