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

微笑、不失礼 提交于 2020-01-11 11:42:26

问题


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 format (for instance, ISO8601)?

I found that the output of dir uses system-dependent date/time format, with the seconds missing.
The same is true for the ~t modifier of the parameter (%~t1) and for variable expansion (%~tI).
Also the forfiles variables @fdate and @ftime depend on the system settings (although the latter returns also the seconds at least).

I have learned that wmic OS GET LocalDateTime /VALUE is a way to get the current system date/time (the output may look like LocalDateTime=20151021020000.000000+120, it can be captured by for /F).
But is there also a command (line) that returns a file or directory date/time stamp in a similar format?


回答1:


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


来源:https://stackoverflow.com/questions/33248149/how-to-get-standardized-date-time-stamp-of-file-or-dir-in-pure-batch-scripting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!