Say you need to determine, what is the date format of this machine. Normally you would read a value from HKEY_CURRENT_USER\Control Panel\International like this:
REG QUERY "HKCU\Control Panel\International" /v sShortDate
That command will produce an output similar to this:
HKEY_CURRENT_USER\Control Panel\International sShortDate REG_SZ M/d/yyyy
But you do not need all that. All you need is the string “M/d/yyyy” (or “yyyy-MM-dd”). To get just that string you need to skip to a second line and read third field on the last line. How you can do that in a batch file?
Look at this batch command below. It does just that:
FOR /F "skip=2 tokens=2,*" %%A IN ('reg.exe query "HKCU\Control Panel\International" /v "sShortDate"') DO set "DFMT=%%B" ECHO New variable DFMT = %DFMT%
This amazing command sets variable DFMT to a value stored in REG key sShortDate. It skips two lines (skip=2). It uses default delimiter space (no code is needed). It ignores first two fields (tokens=2,*), and puts everything that remains into %B.
Note: Syntax tokens=2,3 would also work in this case because output doesn’t contain any spaces. This would mean, skip first two fields and use third field as %B. We use double percent sign (%%) in %%B to indicate that this a variable and not an input parameter.
Fantastic! Now variable DFMT contains string “M/d/yyyy”. Based on this variable I can further direct my script to extract year from a proper position from an output of a DATE command.
Note: you have to place this command in *.BAT or *.CMD file. You cannot run this from a command prompt.