0

In my batch file I scan in a for loop for all .dat files and I need to get the folder names in which these .dat files are located and store them in a variable so I could use it later in this loop. The nested for loop on third line works fine when not in another for loop and I have no idea why it does not work this way. Please help :)

for /f  "delims=" %%G in ('dir *.dat /b/s') do (    
    cd "%%~dG%%~pG"
    for /f "delims=" %%A in ('cd') do (
        set foldername=%%~nxA
    )
    echo. Current Folder Name: %foldername%
Compo
  • 36,585
  • 5
  • 27
  • 39
  • I have modified your code to highlight your missing closing parenthesis. Your variable `%foldername%` requires delayed expansion if `echo`ed like that within the same parenthesised code block it was defined. – Compo Apr 14 '20 at 10:21
  • @Compo Thank you for an advise. I have used SETLOCAL EnableDelayedExpansion and it works perfectly when I use !foldername!. Althought I do not quite understand why this enable is needed (I am new to batch programming). Thanks again for the help. – Roach22 Apr 15 '20 at 06:47
  • Why? Mainly for historic reasons (to stay downwards compatible) – Stephan Apr 15 '20 at 10:38

0 Answers0