Based from this answer I am trying to save the %%a
and %%b
values into variables but it does not work:
@ECHO OFF
SETLOCAL
set "path_of_folder=C:\folderA\folderB"
if exist "%path_of_folder%" (
echo Path exists.
for /f "skip=5 tokens=1,2,4 delims= " %%a in (
'dir /ad /tc "%path_of_folder%\."') do IF "%%c"=="." (
set "dt=%%a"
echo Created on: %%a, %%b
set "vara=%%a"
set "varb=%%b"
echo %vara%, %varb%
REM substring
set day=%vara:~0,2%
)
) else (
echo Path does not exist.
)
GOTO :EOF
But the output of the echo %vara%, %varb%
is empty even though the previous echo Created on: %%a, %%b
prints the correct information!. The idea is that I can manipulate that variable afterwards for example for extracting substrings.
After searching online, I found this answer from which I tried the setlocal ENABLEDELAYEDEXPANSION
, and the approach with the exclamation marks, but they too failed.
[UPDATE to include my other attempt and partly fix when using exclamation marks]
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
set "path_of_folder=C:\folderA\folderB"
if exist "%path_of_folder%" (
echo Path exists.
for /f "skip=5 tokens=1,2,4 delims= " %%a in (
'dir /ad /tc "%path_of_folder%\."') do IF "%%c"=="." (
set "dt=%%a"
echo Created on: %%a, %%b
set vara=%%a
set varb=%%b
echo !vara!, !varb!
REM substring
set day=%vara:~0,2%
)
) else (
echo Path does not exist.
)
GOTO :EOF
Where the vara
and varb
are now saved and echoed appropriately but the substring now does not work..
I tried reading more about the specifics of a %%
variable (link) but that did not help me either.
Any suggestions/examples please?