Windows – On windows: how to get last modified date (UTC time) of a file with wmic

batchwindows 7wmic

I wrote the following batch in order to get the last modified date. The problem is that it gives it in clock time and not in UTC time. I know I have the lag in minutes but it is not that simple to do the shift of the number with the batch file. Is there a flag to give to wmic to obtain UTC time? This is the batch file

@echo off 
setlocal enableextensions disabledelayedexpansion
set file=%1
set WORKINGdir=%~dp0
rem wmic wants double backslash
set PATHfile=%WORKINGdir%%file%
set PATHfile=%PATHfile:\=\\%
for /f %%a in (
    'wmic  DataFile where "Name='%PATHfile%'" get lastmodified   ^| find "+" '
) do set "val=%%a"
echo %val%
rem get first 14 digits (good until year 9999)
echo %val:~0,14%
endlocal

This gives me this output:

20161026144823.620815+120
20161026144823

Best Answer

it is possible with pure batch albeit it might be to slow for a large number of files. The batch routines are derived from functions out of Richie Lawrence Batch Function Library.

:: Demo_DateAddSecs.cmd ::::::::::::::::::::::::::::::::::::::::::::
@Echo off&cls
Call :GetISODT DateTime
Echo %DateTime% is now 
Call :DateAddSecs %DateTime% 7200 NewDT
Echo %NewDT% +7200 secs
Echo y___m_d_h_n_s_
Echo 20160101013000 Subtract 2h from new year 1:30
Call :DateAddSecs 20160101013000 -7200 NewDT
Echo %NewDT% -7200 secs
Echo y___m_d_h_n_s_
Echo 20160301013000 March first -12h this is a leap year
Call :DateAddSecs 20160301013000 -43200 NewDT
Echo %NewDT% -43200 secs
Echo y___m_d_h_n_s_
Pause
Goto :Eof
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:GetISODT Var
SetLocal
for /f "tokens=1-3 delims=.+-" %%A in (
  'wmic os get LocalDateTime^|findstr ^^[0-9]'
    ) do Set _IsoDT=%%A
EndLocal&Set "%1=%_IsoDT%"&Goto :Eof
:: GetISODT.cmd :::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:DateAddSecs %yyyymmddhhnnss% %secs2Add% DTreturnvar
:: Original functions DateToSecs and SecsToDate
:: By:   Ritchie Lawrence, updated 2002-08-13. Version 1.1
:: Func: Converts DateTime to number of seconds elapsed since 
::       1st January 1970 00:00:00 adds the supplied seconds
::       and converts back to a valid Datetime
::  For a given calendar date and time of day. Tested with Win10pro.
:: Args:
::  %1 by val Datetime in the form yyyymmddhhnnss
::  %2 by val seconds integer to add/subtract 1 hour = 60*60 = 3600
::  %3 by ref the var name to receive the resulting Datetime 
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
SetLocal EnableExtensions& Set DT=%1
Set yy=%DT:~0,4%&set mm=%DT:~4,2%&set dd=%DT:~6,2%
set hh=%DT:~8,2%&set nn=%DT:~10,2%&set ss=%DT:~12,2%
Set /a dd=100%dd%%%100,mm=100%mm%%%100
Set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2
Set /a j=j/5+dd+y*365+y/4-y/100+y/400-2472633
if 1%hh% LSS 20 set hh=0%hh%
Set /a hh=100%hh%%%100,nn=100%nn%%%100,ss=100%ss%%%100
Set /a j=j*86400+hh*3600+nn*60+ss
Endlocal&set /A secs=%j%+%2
:: SecsToDate %secs% yy mm dd hh nn ss
SetLocal EnableExtensions
Set /a i=%secs%,ss=i%%60,i/=60,nn=i%%60,i/=60,hh=i%%24,dd=i/24,i/=24
Set /a a=i+2472632,b=4*a+3,b/=146097,c=-b*146097,c/=4,c+=a
Set /a d=4*c+3,d/=1461,e=-1461*d,e/=4,e+=c,m=5*e+2,m/=153,dd=153*m+2
Set /a dd/=5,dd=-dd+e+1,mm=-m/10,mm*=12,mm+=m+3,yy=b*100+d-4800+m/10
(if %mm% LSS 10 set mm=0%mm%)&(if %dd% LSS 10 set dd=0%dd%)
(if %hh% LSS 10 set hh=0%hh%)&(if %nn% LSS 10 set nn=0%nn%)
if %ss% LSS 10 set ss=0%ss%
Endlocal&set "%3=%yy%%mm%%dd%%hh%%nn%%ss%"&Goto :Eof
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Gives this Output

> DemoDateAddSecs.cmd
20161027053749 is now
20161027073749 +7200 secs
y___m_d_h_n_s_
20160101013000 Subtract 2h from new year 1:30
20151231233000 -7200 secs
y___m_d_h_n_s_
20160301013000 March first -12h this is a leap year
20160229133000 -43200 secs
y___m_d_h_n_s_

HTH

Related Question