【问题标题】:automate .bat to get all content of a given directory自动化 .bat 以获取给定目录的所有内容
【发布时间】:2018-01-17 08:49:14
【问题描述】:

我正在尝试自动化将所有内容(目录、子目录、文件等)存储在 .txt 文件中的过程。我想出了这个:

cd "D:\sources\SVN_WorkingCopy\"
dir /s /b>filelist.txt

将上述内容放入 .bat 文件并手动运行时,它可以正常工作并创建 .txt 文件。但是,当我将它包含在 Windows 计划任务中时,它什么也没做。谁能帮忙,让它工作?

谢谢!

【问题讨论】:

  • cd /d "D:... 应该可以。
  • 谢谢!这完成了工作。

标签: batch-file filelist


【解决方案1】:

如果您真的想分两行执行此操作,那么您需要确保使用 /D 选项来更改驱动器:

CD /D "D:\sources\SVN_WorkingCopy"
Dir /S /B > "filelist.txt"

或者,您可以使用单行而不更改当前工作目录:

Dir /S /B "D:\sources\SVN_WorkingCopy" > "filelist.txt"

或者使用不同的命令:

Where /R "D:\sources\SVN_WorkingCopy" * > "filelist.txt"

您还需要注意,当作为计划任务运行时,工作目录是%SystemRoot%\System32,默认情况下是写保护的,(取决于权限)。因此,除非您提供可访问的位置,否则该命令将尝试将其输出发送到那里的文件:

> "C:\Users\kalinkula\Desktop\filelist.txt"

您可以将输出发送到评估目录中的文件D:\sources\SVN_WorkingCopy\filelist.txt,只要您不介意该文件出现在它自己包含的列表中。

这些答案还假设 D: 在脚本/命令运行时已映射和/或可用。

【讨论】:

    【解决方案2】:

    这可能只是一个 FYI,但您也可以使用 tree 进行结构化输出

    tree /f /a "D:\sources\SVN_WorkingCopy" > filelist.txt
    

    这将导致文件结构类似于:

    D:.
    ├───e-shopper
    │   └───Eshopper
    |       └───temp folder
    |           └───Performance Report Table.xlsx
    |           └───Sales Document.docx
    ├───test Folder
    │   └───Hire Freelancers
    |       └───Performance Report Table.xlsx
    |       └───Sales Document.docx
    └───Some other Directory with no subs or files
    

    【讨论】:

      猜你喜欢
      • 2011-02-01
      • 2010-10-03
      • 2013-08-01
      • 1970-01-01
      • 2017-11-26
      • 1970-01-01
      • 2022-01-05
      • 1970-01-01
      • 2013-09-23
      相关资源
      最近更新 更多