【问题标题】:Windows BATCH filter with regex [closed]带有正则表达式的 Windows BATCH 过滤器 [关闭]
【发布时间】:2018-12-27 16:26:46
【问题描述】:

我需要根据文件名中的数字执行一个 bat 脚本来一个一个地处理文件。这是我可以获得的示例:

  1. CDEP_0003.pdf
  2. CIM_0001.pdf
  3. ESCALE_0002.pdf

是否可以使用正则表达式或类似的方法以有序的方式循环和处理每个文件以排除数字并将其用作循环索引? (0001、0002、0003 等)

【问题讨论】:

    标签: windows batch-file scripting


    【解决方案1】:

    使用how to loop through files matching wildcard in batch file 上的另一个答案,只需循环通过.*0000\.pdf$.*9999\.pdf$

    【讨论】:

      【解决方案2】:

      你可以

      • 通过首先遍历文件来构建一个已使用数字的数组,或者
      • 使用从 10000 到 19999 的计数循环 (for /l) 前导零并取最后 4 位数字..

      如果数字前只有一个下划线:

      @Echo off
      Pushd "X:\folder\to\start"
      ::clear array
      for /f "delims==" %%A in ('Set No[ 2^>Nul') do Set "%%A=" 
      :: fill array
      for /f "tokens=2delims=_." %%A in (
          'Dir /B "*_????.pdf" ^| findstr "^.*_[0-9][0-9][0-9][0-9]\.pdf$"'
      ) Do Set /A "No[%%A]=%%A"
      :: process existing numbers
      for /f "tokens=2 delims=[]" %%A in ('Set No[ 2^>Nul') do (
         Rem dir "*_%%A.pdf" or whatever you want to do
      )
      

      【讨论】:

        猜你喜欢
        • 2014-11-06
        • 2012-04-22
        • 1970-01-01
        • 2022-08-17
        • 2013-03-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多