【问题标题】:Print a directory listing with full filename and path?打印带有完整文件名和路径的目录列表?
【发布时间】:2014-04-25 04:27:57
【问题描述】:

我正在尝试创建一个包含两列的简单 txt 文件,第一列是最后修改日期的日期,第二列是完整的文件名(包括路径):

/dir > list.txt 几乎可以完成这项工作,但它按目录对所有文件进行分组,我试图将目录与文件名和上次修改日期放在同一行。

我需要使用批处理脚本吗?还是CMD线?我使用的是 Windows XP。

YYYY/MM/DD   C:/Directory1/Directory2/Directory3/filename.txt
YYYY/MM/DD   C:/Directory1/Directory2/Directory3/filename.txt
YYYY/MM/DD   C:/Directory1/Directory2/Directory3/filename.txt
YYYY/MM/DD   C:/Directory1/Directory2/Directory3/filename.txt
YYYY/MM/DD   C:/Directory1/Directory2/Directory3/filename.txt

这几乎也可以,但我无法获得最后修改日期:

dir /s /b > list.txt

注意 - 我将把这个 cmd 或 .bat 文件放在父目录中,并且我不能安装任何程序或软件(即 python、perl 等)解决方案必须是简单的 CMD 行或 .bat 文件。

【问题讨论】:

    标签: shell batch-file cmd directory


    【解决方案1】:
    for /f "delims=" %%a in ('dir/s/b/a-d') do echo %%~ta %%a
    

    作为批处理行,如果您是从提示符运行,则将 %% 始终减少为 %

    (嗯 - 给你日期和时间,然后是文件名 - 你真的只想要日期吗?)

    @ECHO OFF
    SETLOCAL
    for /f "delims=" %%a in ('dir/s/b/a-d') do (
     FOR /f "tokens=1-3delims=/: " %%q IN ("%%~ta") DO echo %%s/%%r/%%q %%a
    )
    
    GOTO :EOF
    

    产生您在在我的系统上指定的 yyyy/mm/dd 格式,但我使用 dd/mm/yyyy 的日期格式 - 您尚未指定您的日期格式,因此详细信息可能会有所不同。

    【讨论】:

    • @chepelucho %~za 会告诉你尺寸。如需完整列表,请在提示符下输入 for/? - 选项在末尾处详细说明。
    【解决方案2】:

    由于您使用的是 Windows XP,与 CMDLine/Batch 脚本相比,使用 Powershell 获得所需的文件属性会更容易。使用 CMD 检索文件的最后修改日期有点麻烦。

    试试这个:

    $files=Get-ChildItem -recurse "full_path_to_test_directory"
    for($i=0;$i -lt $files.Count;$i++) {
    $f=$files.Get($i)
    $fName=$f.FullName
    $fModified=Get-Date $f.LastWriteTime -Format "yyyy\\MM\\dd"
    $out="$($fModified)    $($fName)"
    Add-Content -Path "full_path_to_destination_file" -Value $out
    

    }

    将其保存为 getFileProperties.ps1 并从 Powershell 控制台运行它。

    您还可以从 命令行 运行上述 powershell 脚本,如下所示:

    powershell -File "full_path_to_above_powershell_script"
    

    【讨论】:

      【解决方案3】:

      我需要同样的东西。我想获取目录和所有子目录中所有文件的最后修改日期,并将其输出到文件中,就像 OP 一样。 Magoo 列出了它的内容,但缺少的是保存到文件中。所以,这就是在命令提示符下运行良好的方法:

      for /f "delims=" %a in ('dir/s/b/a-d') do echo %~ta %a >>c:\temp\filelist.txt
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-11-11
        • 2020-12-19
        • 2019-02-24
        • 1970-01-01
        • 2020-08-30
        • 2012-03-01
        • 2021-06-11
        相关资源
        最近更新 更多