I am trying to understand the for
loop in batch scripting; in particular with skip
and tokens
parameters. I have found a great example in this answer followed by this nice read.
I would like to see how I can extract the date and time from a dir
in Windows XP which has a slightly different dir
output (see below example outputs) compared to Windows 7. In other words, how to extract creation date of a specific folder in Windows XP.
The following modified code from the above mentioned links correctly extracts the date and time of a DIR
in Windows 7 but fails (i.e. extracts other characters) in Windows XP:
SETLOCAL ENABLEDELAYEDEXPANSION
set "path_of_folder=C:\folderA\folderB"
for /f "skip=5 tokens=1,2,4 delims= " %%a in (
'dir /ad /tc "%path_of_folder%\."') do IF "%%c"=="." (
set "dt=%%a"
set dirdate=%%a
set dirtime=%%b
echo !dirdate!, !dirtime!
set dirday=!dirdate:~0,2!
echo !dirday!
)
In Windows 7 the dir
command in the command prompt outputs something similar to:
Directory of C:\Windows
12/21/2016 12:56 <DIR> .
12/21/2016 12:56 <DIR> ..
09/18/2017 07.42 678 config
In Windows XP the dir
command in the command prompt outputs something similar to:
Directory of C:\WINDOWS
12/21/2016 12:56 PM <DIR> .
12/21/2016 12:56 PM <DIR> ..
09/18/2017 07.42 PM 678 config
The only difference (as far as I can notice) is the PM
column(?) which does not exist in Windows 7 but does exist in Windows XP.
I know this is probably a trivial question but I am new in batch scripting and seeing a specific example using a complex for
loop would help my understanding. Assume use of delayed expansion.