【问题标题】:Batch Script Parsing Dir Command批处理脚本解析目录命令
【发布时间】:2013-07-27 00:27:24
【问题描述】:

你好新手寻求帮助。我正在尝试编写一个脚本来搜索驱动器中具有特定标识符的所有文件和文件夹。例如“ID-”

我正在使用以下内容来获取日志文件中列出的文件和目录:

dir ID * /A:-D-H /B /S >> C:\FileCatalogue.log 2>> C:\FileCatalogue.log
dir ID * /A:D-H /T:C /S >> C:\DirCatalogue.log 2>> C:\DirCatalogue.log

但是我希望输出位于 3 个选项卡列中:

文件 {tab} DIR {tab} ID

ID123 - YYYY - myfile.txt {tab} C:/tmp/tmp {tab} ID123 - YYYY

等等。

等等……

任何帮助将不胜感激! 维克多

【问题讨论】:

    标签: windows variables batch-file scripting dir


    【解决方案1】:
    @echo off
    for /f "delims=" %%A in ('dir /a:-d-h /b /s /t:c ID123*') do for /f "tokens=1,2,* delims=-" %%B in ("%%~nxA") do echo(%%~nxA    %%~dpA  %%B-%%C
    

    输出

    ID123 - YYYY - myfile.txt   C:\Users\User\Desktop\  ID123 - YYYY
    

    【讨论】:

      【解决方案2】:

      试试这个:

      for /f "tokens=1*delims=-" %%a in ('dir /a-d/b ID*') do echo %%a-%%b  %cd%  %%a
      

      【讨论】:

      • 你忘记了/s 和末尾的 YYYY - (嗯,YYYY 很容易使用 tokens=1,2*
      • @Stephan 我什么都没得到 :) 请问/s 是什么?
      • 在你的回答中是dir /a-d/b ID*,user2623499 请求dir /a:-D-H /B /S。 /s 代表“包括子目录” - 但我非常确定,你知道 ;)
      • 你觉得怎么样,额外的 for 循环会有帮助吗? :)
      • 如果我们只能确定,路径中没有-...那么您的代码将可以正常运行(即使添加了/s)。你觉得forfiles怎么样?
      猜你喜欢
      • 1970-01-01
      • 2013-03-12
      • 2012-11-17
      • 2015-05-08
      • 2015-10-11
      • 2012-12-23
      • 2013-07-11
      • 1970-01-01
      • 2013-01-08
      相关资源
      最近更新 更多