1

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
7
  • If the machine's timezone is always the same. You can just manually adjust it by adding the current offset to UTC. WMIC is the wrong choice to do something like this. You are going to find it very hard, if not impossible, to do this with WMIC
    – Ramhound
    Oct 26, 2016 at 13:34
  • I often travel from Europe and USA and back
    – Millemila
    Oct 26, 2016 at 13:36
  • You can set the timezone on the machine to UTC then you don't have to do anything special. If thats not acceptable, parase, HKLM\System\CurrentControlSet\Control\TimeZoneInformation to determine the current timezone setting and make the adjustments based on a conditional statement.
    – Ramhound
    Oct 26, 2016 at 13:37
  • You really should improve your script so its human readable though
    – Ramhound
    Oct 26, 2016 at 13:41
  • Sorry there was an extra line.i think its fine now
    – Millemila
    Oct 26, 2016 at 14:02

2 Answers 2

2

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

1

Indeed, timestamp arithmetic is a pain. Adjusting the hour to UTC could result in a different date, which means you may have to worry about how many days in the month, and you might have to worry about leap years.

WMIC does not have any built in provision to format the timestamp as UTC. And batch certainly has virtually no support for handling date/time arithmetic.

I have written a utility called JREN.BAT that can solve your problem easily. It was primarily written to allow renaming of files/folders using regular expressions. But it has extra functionality that can solve this problem.

JREN.BAT is pure script (hybrid batch/JScript) that runs natively on any Windows machine from XP onward - no 3rd party exe file required. Full documentation is available by executing jren /? from the command prompt (or jren /?? for paged help)

The following will provide the same functionality as your script, except it will properly display the timestamp as UTC.

@echo off
for /f "delims=" %%A in (
  'jren "^.*" "ts({dt:'modified',tz:0})" /j /list /p "%~dp0" /fm %1'
) do set "ts=%%A"
echo %ts%

The output will be in ISO 8601 format, without punctuation, which will properly sort chronologically using string semantics. Something like 20161006T155621.746+0000.

You can easily add the fmt: option to the ts() call to format the timestamp any way you see fit. Use jren /?ts() to get full help on all the options available for working with timestamps.

It is very simple to use JREN to provide a full directory listing with UTC times.

The following lists last modified timestamp, file size, and file name for all .txt files that begin with t in the current directory.

D:\test>jren "^.*" "ts({dt:'modified',tz:0,fmt:'{iso-dt}  {iso-tm}'})+size('              ')+'  '+$0" /j /list /fm t*.txt
2015-06-22  19:11:22.134            74  temp1.txt
2015-06-22  19:11:22.259             0  temp2.txt
2015-06-22  19:11:22.384             0  temp3.txt
2016-09-07  15:20:08.146         10376  tempfile.txt
2016-10-06  15:56:21.746            14  test.txt
2015-06-22  19:11:23.600           342  text2.txt
2015-06-22  19:11:23.741           288  textfile1.txt
2015-06-22  19:11:23.866           144  textfile2.txt
2015-10-19  19:12:16.441           707  TL.txt
2016-01-13  17:35:17.505          1030  tl64.txt
2016-01-13  17:36:48.501           974  tl64_2.txt
2016-01-13  17:45:16.383           943  tl64_3.txt
2016-01-13  17:37:21.308           707  tl_1.txt
2016-01-13  17:37:30.356           707  tl_2.txt
2016-01-13  17:45:50.469           707  tl_3.txt
2015-06-22  19:11:24.006            42  tmp.txt
2015-06-22  19:11:24.427        541553  toc-z.txt
2015-08-31  20:59:25.202      17442083  tree.txt
2015-06-22  19:11:24.583           304  tsc_call_layout.txt

JREN uses WMI to get the last modified timestamp, which is quite slow. Depending on your language setting, you may be able to use fsomodified, instead of modified, to get the timestamp using the FileSystemObject. This is much faster, but it does not provide milliseconds, and it only works if your language formats timestamp strings in a way that can be interpreted by the JScript date object.

D:\test>jren "^.*" "ts({dt:'fsomodified',tz:0,fmt:'{iso-dt}  {hh}:{nn}:{ss}'})+size('              ')+'  '+$0" /j /list /fm t*.txt
2015-06-22  19:11:22            74  temp1.txt
2015-06-22  19:11:22             0  temp2.txt
2015-06-22  19:11:22             0  temp3.txt
2016-09-07  15:20:08         10376  tempfile.txt
2016-10-06  15:56:21            14  test.txt
2015-06-22  19:11:23           342  text2.txt
2015-06-22  19:11:23           288  textfile1.txt
2015-06-22  19:11:23           144  textfile2.txt
2015-10-19  19:12:16           707  TL.txt
2016-01-13  18:35:17          1030  tl64.txt
2016-01-13  18:36:48           974  tl64_2.txt
2016-01-13  18:45:16           943  tl64_3.txt
2016-01-13  18:37:21           707  tl_1.txt
2016-01-13  18:37:30           707  tl_2.txt
2016-01-13  18:45:50           707  tl_3.txt
2015-06-22  19:11:24            42  tmp.txt
2015-06-22  19:11:24        541553  toc-z.txt
2015-08-31  20:59:25      17442083  tree.txt
2015-06-22  19:11:24           304  tsc_call_layout.txt

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .