Windows批处理脚本 - 如何筛选具有已定义扩展名的文件

时间:2013-07-14 10:41:46

标签: windows for-loop batch-file cmd

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

set AllowExt="jpg png bmp"
forfiles /p D:\Pictures /m *.* /c "cmd /c if not %AllowExt:jpg=% == %AllowExt% echo @file

但是以下脚本会抛出错误

set AllowExt="jpg png bmp"
forfiles /p D:\Pictures /m *.* /c "cmd /c if not %AllowExt:@ext=% == %AllowExt% echo @file"

错误:无效的参数/选项 - 'png'。 输入“FORFILES /?”用法。

2 个答案:

答案 0 :(得分:4)

你可以试试这个:

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

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

答案 1 :(得分:0)

这可以为您提供所需的文件名。

@echo off
set "AllowExt=jpg png bmp"
set "AllowExt= %AllowExt%"
set "AllowExt=%AllowExt: = *.%"
  pushd "D:\Pictures"
   for %%a in (%AllowExt%) do (
     echo "%%a"
   )
  popd