Thursday, March 27, 2008

Writing a timestamp in Batch using WMIC

WMIC(Windows Management Instrumentation Command-line) is one of the less used, but very cool & powerful features of windows.

Try this from a command prompt for an example:

wmic process


One of the things that sucks in windows is trying to use system date/time strings within a batch file, but WMIC makes this less painful:
:: Use WMIC to retrieve date and time
FOR /F "skip=2 tokens=2-7 delims=," %%A IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:csv') DO (
SET /A SortDate = 10000 * %%F + 100 * %%D + %%A
SET /A SortTime = 10000 * %%B + 100 * %%C + %%E
)

:: display timestamp
echo %SortDate%%SortTime%


References:
http://www.robvanderwoude.com/datetiment.html
http://blogs.technet.com/jhoward/archive/2005/02/23/378726.aspx