【问题标题】:Directory listing is slow when doing it in a batch script在批处理脚本中执行目录列表时速度很慢
【发布时间】:2013-09-19 20:45:16
【问题描述】:

您好 StackOverflow 成员!

我正在尝试运行以下命令:

REM the below line lists the folder names that are to be read
FOR /F "TOKENS=* DELIMS=" %%d in (%start_dir%\folder_list.txt) DO (
    ECHO Entering into: %%d Directory
    REM The below line lists the folders and all of it's subfolders. It than outputs it to a file.

        FOR /F "TOKENS=* DELIMS=" %%e in ('DIR /s "%work_dir%\%%d"') DO (
           ECHO %%e>>%start_dir%\tmp_folder\%%d.size
        )
)

上面的代码有效。

问题是:如果我的文件夹大小只有几 GB,那很好。

如果我有一个大于 100GB 的文件夹,脚本将需要大约一个小时来输出 DIR /S>>%%d 命令。

当我在大约 150GB 的单个文件夹上运行时: Dir /s "150GB_Folder">>dir_ouput_file.txt 它在大约 6-10 秒内完成。

我的问题是:为什么从脚本中输出 DIR /S>>whatever.txt 需要一个小时,而它不在脚本中时只需要几秒钟?

提前谢谢你!

【问题讨论】:

  • 但是你为什么要逐行写DIR /s的输出呢?为什么不直接做DIR /s "%work_dir%\%%d" >%start_dir%\tmp_folder\%%d.size

标签: windows batch-file scripting


【解决方案1】:

这是for 中的一个错误,其中使用命令解析大量行会导致巨大的延迟。
解决方法是创建一个包含信息的文件,然后读取该文件。

REM the below line lists the folder names that are to be read
FOR /F "TOKENS=* DELIMS=" %%d in (%start_dir%\folder_list.txt) DO (
    ECHO Entering into: %%d Directory
    REM The below line lists the folders and all of it's subfolders. It than outputs it to a file.

DIR /s "%work_dir%\%%d" >%temp%\temp.tmp

        FOR /F "TOKENS=* DELIMS=" %%e in (%temp%\temp.tmp) DO (
           ECHO %%e>>%start_dir%\tmp_folder\%%d.size
        )
del %temp%\temp.tmp
)

【讨论】:

  • 1+,没听说过!
  • 效果很好。我认为这是某种错误,但我不是 100% 确定。感谢您清除它!
  • 我想我明白你在说什么,但我不确定你的措辞是否正确,或者我可能遗漏了什么。 FOR /F 循环不解析文件,它解析文本行。我认为问题在于 IN 子句中命令输出产生的行数(在这种特殊情况下恰好是 DIR)——这可能更有意义(无论如何对我来说)。跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-19
  • 1970-01-01
  • 1970-01-01
  • 2018-01-22
  • 1970-01-01
  • 1970-01-01
  • 2015-11-20
相关资源
最近更新 更多