1

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?

aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • You can run a `for` loop on the command line, you just need to use `%` instead of `%%` for the variable. – SomethingDark Oct 20 '15 at 23:07
  • take a look at [this](http://stackoverflow.com/a/25632042/388389) – npocmaka Oct 20 '15 at 23:10
  • and as addition for wmic with folders you'll need to use `wmic path win32_directory..` . Here's the [description of win32_directory](https://msdn.microsoft.com/en-us/library/aa394130%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396) which will help you to create a query – npocmaka Oct 20 '15 at 23:13
  • @npocmaka, that's exactly what I'm looking for! if you post an answer (at least for the directory stuff, files are covered by your [linked post](http://stackoverflow.com/a/25632042/388389) (§ 1) anyway), I could accept it... – aschipfl Oct 20 '15 at 23:25
  • @aschipfl - I just need few minutes to create the scripts.. – npocmaka Oct 20 '15 at 23:43

1 Answers1

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
Community
  • 1
  • 1
npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • ...concerning the `wmic` approaches: the inner `for /F` loop removes any `wmic` output artefacts like orphaned carriage-returns, is that true? – aschipfl Oct 21 '15 at 00:45
  • @aschipfl - yes.The technique was introduced by dbenham [here](http://www.dostips.com/forum/viewtopic.php?f=3&t=4266) – npocmaka Oct 21 '15 at 06:28
  • 1
    I'm afraid the `wmic` method fails when the file/dir. name contains char.s like `^` or `&`; however, if I remove the `""` around the entire `wmic` command and put the `name=` argument (the path) in between `""` instead of `''`, it works... the only problem left is the `%` character (unless I double it)... – aschipfl Oct 21 '15 at 17:31
  • 1
    @aschipfl - hmm.. didn't thought about that.One possible workaround is to use short names - these characters won't be there in short names , but there's a chance this feature to be turned off.Other is to set double quotes around file name , but I'm not sure how the for loop will handle it (i'll try) or using delayed expansion , but then the `!` will be a problem. – npocmaka Oct 21 '15 at 17:39
  • 1
    ...the work-around I mentioned [above](http://stackoverflow.com/questions/33248149/how-to-get-standardized-date-time-stamp-of-file-or-dir-in-pure-batch-scripting/33248735#comment54332444_33248735) was meant to be done in the `for /F` loop... yes, delayed expansion will solve the `%` problem, but introduces the `!` issue... – aschipfl Oct 21 '15 at 17:48
  • 1
    @aschipfl - I'll update it based on your suggestions . And will think about how to handle both `!` and `%` but probably I wont succeed. [Here](http://stackoverflow.com/a/18663935/388389) dbenham used some bulletproof techniques about similar problem and I'll see if I will be able to used it. – npocmaka Oct 21 '15 at 17:55