0

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?

2
  • 1
    Show us your ENABLEDELAYEDEXPANSION attempt.
    – Anders
    Commented Jul 6, 2017 at 12:02
  • @Anders I included the other attempt as you requested along with a minor fix for saving the variables as the should however they are not 'properly' saved since the following substring attempt does not work (i.e. returns nothing).
    – user6003691
    Commented Jul 6, 2017 at 12:13

1 Answer 1

0

Your 2nd script works perfectly. The only error I can spot is in this line: set day=%vara:~0,2%. It should be set day=!vara:~0,2!.

enter image description here

@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. 
)
2
  • I was echoing the day using the % instead of !... Thank you for your answer. Could you please also provide a short explanation of why the %% requires !?
    – user6003691
    Commented Jul 6, 2017 at 12:18
  • 1
    Take a look at this: ss64.com/nt/delayedexpansion.html This should explain everything.
    – MichaelS
    Commented Jul 6, 2017 at 12:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.