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
HKLM\System\CurrentControlSet\Control\TimeZoneInformation
to determine the current timezone setting and make the adjustments based on a conditional statement.