【问题标题】:Windows batch script - How to filter files with defined extensionWindows 批处理脚本 - 如何过滤具有已定义扩展名的文件
【发布时间】:2013-07-14 10:41:45
【问题描述】:

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

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 /?”供使用。

【问题讨论】:

    标签: windows for-loop batch-file cmd


    【解决方案1】:

    你可以试试这个:

    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 /?

    【讨论】:

    • 修复如下,运行正常。 set AllowExt=.jpg .png .bmp for %%a in (%AllowExt%) do (forfiles /p D:\Pictures /m *%%a /c "cmd /c echo @file" )
    【解决方案2】:

    这应该会为您提供所需的文件名。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-16
      • 2016-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-02
      相关资源
      最近更新 更多