【问题标题】:How to get a list of files and folders of just those folders listed in a text file into a list file?如何将文本文件中列出的那些文件夹的文件和文件夹列表获取到列表文件中?
【发布时间】:2018-09-30 16:25:20
【问题描述】:

我创建了一个批处理文件,用于将指定目录中的所有文件、文件夹和子文件夹导出到文本文件。我需要的是从包含其路径的文本文件中列出带有子文件夹的特定文件。批处理文件将列出仅包含在此文本文件中的文件和子文件夹。例如,我需要仅将这些文件夹下的文件导出到文本文件中,就像输入文本文件中一样。

C:\Users\Username\Documents\test1
C:\Users\Username\Documents\test2
C:\Users\Username\Documents\test3
C:\Users\Username\Documents\test4

提前致谢。

编辑添加更多细节;通常,我们使用这个命令列出一个目录下的所有文件:

dir > output.txt

我想应用另一种方法。我只想列出特定目录下的文件,文本文件将包含那些特定的目录路径。

这是文本文件:

C:\Users\Username\Documents\myfolder1
C:\Users\Username\Documents\myfolder2
C:\Users\Username\Downloads\yet another folder

虽然 Documents 和 Downloads 中还有许多其他文件夹,但只会列出路径在我的输入文本文件中列出的文件夹下的文件。然后,myfolder1myfolder2yet another folder 的所有子文件夹和文件将被发送到一个输出文本文件。

【问题讨论】:

  • Murray,查看命令“tree”,我相信这就是您要寻找的,但也许有更好的方法来使用“dir”,但我不知道很多关于windows命令行的内容
  • 嗨,LuRsT。实际上,这不是我要找的东西。我需要使用文本文件中的路径列表来输出文件。
  • 也可以使用 FOR 从文本文件中读取目录,它还可以递归搜索目录树中的文件,就像直接在 cmd 窗口中使用一样 @ 987654327@。通过在命令提示符窗口for /? 中执行获取命令FOR 的帮助。如果要查看命令列表,请在cmd窗口中运行help
  • 嗨莫菲,非常感谢您的回复。你的提议听起来很不错。您的第二条评论正是我想要做的。但是,我无法调整代码。它出什么问题了? for /F "usebackq eol=| delims=" %I in ("C:\Users\Username\Documents\Listfile.txt") for /R "%~I" %J in (*) do echo %J
  • @Murray 如果您在批处理文件中使用此行,请将 % 各加倍:%%I%%~I%%J。 (这是罕见的情况之一,命令行和批处理文件的语法不同)

标签: batch-file cmd


【解决方案1】:

读取包含文件夹名称的文件并列出每个文件夹及其子文件夹中的文件:

@echo off
for /f "delims=" %%a in (file.txt) do dir /s /b "%%a\*"

根据您的需要调整dir 开关和文件掩码。

【讨论】:

    【解决方案2】:

    这个非常简单的批处理文件可用于此任务:

    @echo off
    rem First check existence of folders list file and exit
    rem batch file execution if this file does not exist.
    
    if not exist "%UserProfile%\Documents\FoldersList.txt" goto :EOF
    
    rem Delete output list file if already existing from a previous execution.
    rem The error message output on file not existing is suppressed by
    rem redirecting it from handle STDERR (standard error) to device NUL.
    
    del "%UserProfile%\Documents\OutputList.txt" 2>nul
    
    rem For each folder path in folders list file run the command DIR to output
    rem in bare format all files and folders including hidden files and folders
    rem in the folder and all its subfolders with redirecting the output written
    rem to handle STDOUT (standard output) to the output list file with appending
    rem the new lines at end of the list file. The list file is automatically
    rem created on first line written to the output list file.
    
    for /F "usebackq eol=| delims=" %%I in ("%UserProfile%\Documents\FoldersList.txt") do dir "%%~I\*" /A /B /S >>"%UserProfile%\Documents\OutputList.txt"
    

    通过将 DIR 选项 /A 替换为 /A-D 来生成没有文件夹的文件列表,这意味着除了属性目录之外的任何属性。这与使用 for last 命令行相同,不同之处在于内部 FOR 不输出具有隐藏属性集的文件:

    for /F "usebackq eol=| delims=" %%I in ("%UserProfile%\Documents\FoldersList.txt") do for /R "%%~I" %%J in (*) do echo %%J>>"%UserProfile%\Documents\OutputList.txt"
    

    要了解所使用的命令及其工作原理,请打开命令提示符窗口,在其中执行以下命令,并仔细阅读每个命令显示的所有帮助页面。

    • del /?
    • dir /?
    • echo /?
    • for /?
    • rem /?

    另请参阅有关 Using command redirection operators 的 Microsoft 文章。

    【讨论】:

    • 嗨@Mofi 非常感谢。您的解决方案按描述工作。我不能感谢你。我投票支持你和斯蒂芬的解决方案。你们俩都是很棒的程序员!
    猜你喜欢
    • 2011-01-15
    • 1970-01-01
    • 2015-02-27
    • 2013-04-11
    • 2017-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多