代码之家  ›  专栏  ›  技术社区  ›  Ricky Supit

如何在windows命令行中获取文件的最后修改日期?

  •  18
  • Ricky Supit  · 技术社区  · 16 年前

    我一直在使用以下命令来获取文件日期。然而, fileDate 自从我们移到另一个服务器(WindowsServer2003)后,变量一直返回空值。

    FOR /f %%a in ('dir myfile.txt^|find /i " myfile.txt"') DO SET fileDate=%%a 
    

    有没有其他更可靠的方法来获取文件日期?

    6 回复  |  直到 7 年前
        1
  •  33
  •   lcnittl Andy Morris    9 年前

    变化 % %% 用于批处理文件,用于 %~ta 语法输入 call /?

    for %a in (MyFile.txt) do set FileDate=%~ta
    
        2
  •  11
  •   user2607028    11 年前

    你能做到的

    forfiles /M myfile.txt /C "cmd /c echo @fdate @ftime"
    
        3
  •  5
  •   Gerhard    7 年前

    使用批处理文件获取文件属性的有用参考,包括上次修改时间:

    FOR %%? IN ("C:\somefile\path\file.txt") DO (
        ECHO File Name Only       : %%~n?
        ECHO File Extension       : %%~x?
        ECHO Name in 8.3 notation : %%~sn?
        ECHO File Attributes      : %%~a?
        ECHO Located on Drive     : %%~d?
        ECHO File Size            : %%~z?
        ECHO Last-Modified Date   : %%~t?
        ECHO Drive and Path       : %%~dp?
        ECHO Drive                : %%~d?
        ECHO Fully Qualified Path : %%~f?
        ECHO FQP in 8.3 notation  : %%~sf?
        ECHO Location in the PATH : %%~dp$PATH:?
    )
    
        4
  •  2
  •   Cheeso    16 年前

    它在vista上对我有用。一些尝试:

    1. 替换 find 使用find命令的完全限定路径。 找到 是一个常用的工具名。有一个unix查找与windows内置查找非常不同。这样地:
      FOR /f %%a in ('dir ^|%windir%\system32\find.exe /i "myfile.txt"') DO SET fileDate=%%a

    2. 在cmd.exe窗口中检查命令的输出。要做到这一点,您需要将%%替换为。
      FOR /f %a in ('dir ^|c:\windows\system32\find.exe /i "myfile.txt"') DO SET fileDate=%a
      这可能会给你一些想法。

    3. 如果显示为空,则在命令提示下再次尝试:

      dir | c:\windows\system32\find.exe /i "myfile.txt"

    这应该让你知道你需要看什么。

    如果你仍然不能从中找到答案,编辑你的文章,包括你从这些命令中看到的内容,有人会帮助你。

        5
  •  1
  •   ghostdog74    16 年前

    也可以使用vbscript获取文件修改日期

    Set objFS=CreateObject("Scripting.FileSystemObject")
    Set objArgs = WScript.Arguments
    strFile= objArgs(0)
    WScript.Echo objFS.GetFile(strFile).DateLastModified
    

    将上面的另存为mygetdate.vbs并在命令行上

    c:\test> cscript //nologo mygetdate.vbs myfile
    
        6
  •  0
  •   BlueRaja - Danny Pflughoeft    16 年前

    什么产出 (完全正确) dir myfile.txt 在当前目录中给出?如果设置分隔符会发生什么情况?

    FOR /f "tokens=1,2* delims= " %%a in ('dir myfile.txt^|find /i " myfile.txt"') DO SET fileDate=%%a 
    

    (注意后面的空格 delims= )
    (为了简化工作,可以从命令行执行此操作,方法是替换 %%a 具有 %a )