How to get locale-independent modified time and creation time of a file in batch?

落花浮王杯 提交于 2019-11-28 13:00:30

I'll post few options

1)First one is with wmic (not available for XP home edition) (LastModified can be changed with CreationDate or LastAccessed )

@echo off

set file_loc=.\temp_file
for %%# in ("%file_loc%") do set file_loc=%%~dpfnx#
set file_loc=%file_loc:\=\\%


for /f "delims=." %%t in ('"WMIC DATAFILE WHERE name="%file_loc%" get LastModified /format:value"') do (
    for /f %%$ in ("%%t") do if "%%$" neq "" set %%$
)

echo %LastModified%
echo year : %LastModified:~0,4%
echo month : %LastModified:~4,2%
echo day : %LastModified:~6,2%

2). Jscript/.bat hybrid (DateLastModified can be changed to DateCreated or DateLastAccessed .Time format can be changed to whatever you want ):

@if (@X)==(@Y) @end /****** jscript comment ******
@echo off

set file_loc=.\temp_file
for %%# in ("%file_loc%") do set file_loc=%%~dpfnx#
::set file_loc=%file_loc:\=\\%
cscript //E:JScript //nologo "%~f0" "%file_loc%"
exit /b 0

****** end of jscript comment ******/

var file_loc = WScript.Arguments.Item(0);
var fso = new ActiveXObject("Scripting.FileSystemObject");
var the_file=fso.GetFile(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) selfcompiled jscript.net/bat hybrid (GetLastWriteTime can be changed to GetLastAccessTime or GetCreationTime . Time format can be changed) :

@if (@X)==(@Y) @end /****** silent line that start 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 file_loc=.\temp_file
for %%# in ("%file_loc%") do set file_loc=%%~dpfnx#


"%~n0.exe" "%file_loc%"


exit /b 0


****** end of jscript comment ******/
import System;
import System.IO;

var arguments:String[] = Environment.GetCommandLineArgs();

var the_file=arguments[1];
var last_modified=File.GetLastWriteTime(the_file);
 Console.WriteLine(last_modified.ToString("yyyy-MM-dd"));

4). robocopy - with this you can get only last modified date (with other methods you can get all time attributes).As time stamps in robocopy are always YYYY/MM/DD HH:mm:SS this can be used...

@ECHO OFF

set file_loc=.\temp_file
for %%# in ("%file_loc%") do set file_dir=%%~dp#
for %%# in ("%file_loc%") do set file_name=%%~nx#
pushd %file_dir%

for /f "tokens=1,2" %%a in ('robocopy  "." "%temp%" /l /fat /ts /LEV:1 /NP /NC /NS /NJS /NJH^|findstr /i /e /c:"%file_name%"') do (
    echo %%a %%b
)
popd

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