How to get standardized date/time stamp of file or dir. in pure batch scripting?

前端 未结 1 2065
闹比i
闹比i 2021-01-24 10:38

Is there a way in Windows command line to retrieve the date/time stamps (modification, creation, access) of a file or directory in a standardized locale-independent

相关标签:
1条回答
  • 2021-01-24 11:05

    Here you can find a ways to get the times of a file.

    1)For directory you can use this WMIC query :

    @echo off
    set "directory=."
    
    for /f "delims=" %%# in ("%directory%") do set "directory=%%#"
    
    set "directory=%temp%"
    for /f "usebackq delims=" %%# in (`"WMIC path Win32_Directory WHERE name='%directory:\=\\%' get creationdate,LastAccessed,LastModified /format:value"`) do (
      for /f %%@ in ("%%#") do echo %%@
    )
    

    2) or with jscript hybrid (just like in the file version you can use also LastModified , CreationDate or LastAccessed):

    @if (@X)==(@Y) @end /****** jscript comment ******
    @echo off
    
    set "directory=."
    for %%# in (%directory%) do set "directory=%%~f#"
    if exist "%directory%\" (
    
     cscript //E:JScript //nologo "%~f0" "%directory%"
    )
    exit /b %errorlevel%
    
    ****** end of jscript comment ******/
    
    var file_loc = WScript.Arguments.Item(0);
    var fso = new ActiveXObject("Scripting.FileSystemObject");
    var the_file=fso.GetFolder(file_loc);
    var the_date= new Date(the_file.DateLastModified);
    WScript.Echo(the_date.getFullYear());
    WScript.Echo(the_date.getMonth());
    WScript.Echo(the_date.getUTCDate());
    

    3) with self-compiled jscript.net (which can be the most powerful option because of .NET formatting capabilities):

    @if (@X)==(@Y) @end /****** start of jscript comment ******
    
    @echo off
    ::::::::::::::::::::::::::::::::::::
    :::       compile the script    ::::
    ::::::::::::::::::::::::::::::::::::
    setlocal
    if exist "%~n0.exe" goto :skip_compilation
    
    set "frm=%SystemRoot%\Microsoft.NET\Framework\"
    :: searching the latest installed .net framework
    for /f "tokens=* delims=" %%v in ('dir /b /s /a:d /o:-n "%SystemRoot%\Microsoft.NET\Framework\v*"') do (
        if exist "%%v\jsc.exe" (
            rem :: the javascript.net compiler
            set "jsc=%%~dpsnfxv\jsc.exe"
            goto :break_loop
        )
    )
    echo jsc.exe not found && exit /b 0
    :break_loop
    
    
    call %jsc% /nologo /out:"%~n0.exe" "%~dpsfnx0"
    ::::::::::::::::::::::::::::::::::::
    :::       end of compilation    ::::
    ::::::::::::::::::::::::::::::::::::
    :skip_compilation
    
    
    
    set "directory=."
    for %%# in (%directory%) do set "directory=%%~f#"
    
    
    "%~n0.exe" "%directory%"
    
    
    exit /b 0
    
    
    ****** end of jscript comment ******/
    import System;
    import System.IO;
    
    var arguments:String[] = Environment.GetCommandLineArgs();
    
    var the_dir=arguments[1];
    
    var last_modified=Directory.GetLastWriteTime(the_dir);
    Console.WriteLine("modified time "+last_modified.ToString("yyyy-MM-dd"));
    
    var created=Directory.GetCreationTime(the_dir);
    Console.WriteLine("created time "+created.ToString("yyyy-MM-dd"));
    
    var accessed=Directory.GetLastAccessTime(the_dir);
    Console.WriteLine("accessed time "+accessed.ToString("yyyy-MM-dd"));
    

    EDIT Here are ready to use parametrized scripts using all the listed methods:

    1. fileModifiedTime.bat - gets last modified time of file with settings independent format.Based on robocopy
    2. fileTimes.bat - gets file time stamps with WMIC
    3. dirTimes.bat - gets directory time stamps with WMIC
    4. fileTimesJS.bat - file time stamps with jscript
    5. dirTimesJS.bat - directory time stamps with jscript
    6. fileTimesNET.bat - file time stamps with .NET
    7. dirTimesNET.bat - dir time stamps with .NET
    0 讨论(0)
提交回复
热议问题