-2

I'm trying to use the command: "set n=0" "for /f %%i in ('dir /a:d /b') do (set /a n+= 1) & (set var%n%=%%i)" - It always sets %%i into var0, but i can see that "set n+= 1" is working. How can i use "n" to create a different variable for each turn?

Like: var1,var2,var3.. for each folder in the directory.

Regardless of the answer, i need 'n' to hold the number of times the command ran(so the program will know the amount of folders)(Edit: Yes i know my example provides this, i was meaning if anyone changed the code, that 'n' still needed to be set to the number of runs)

Edit: I have reviewed the page: Arrays, linked lists and other data structures in cmd.exe (batch) script And found it usefull, but i don't really understand the proper usage. Could someone please help me by either fixing my code to work with DelayedExpansion or better explaining how to implement it?

Edit: Guys! This i NOT a duplicate. I tried to use DelayedExpansion and couldn't get it to work. My problem was cause by my text editor i was using (NotePad++) that is why i thought i was using DelayedExpansion wrong and was asking for clarification. I ended up fixing it myself after the answer below proved that my code WAS correct.

3
  • 3
    If I had a nickel for every time somebody needed to use delayed expansion, I could retire. Commented May 12, 2016 at 0:25
  • 5
    See: stackoverflow.com/questions/10166386/… Commented May 12, 2016 at 0:42
  • n does hold the correct number of runs - put echo %n% after the for loop and you will see... Commented May 12, 2016 at 0:42

1 Answer 1

1

When you create or update a variable inside of a set of parentheses, you need to use delayed expansion and refer to the variable with !variable! instead of %variable%.

As Stephan said in his answer, this is because the cmd interpreter parses an entire code block (everything inside a set of parentheses) at once and replaces variables with their values at parse time, while variables referenced with the delayed expansion method are evaluated at runtime.

@echo off
setlocal enabledelayedexpansion
cls

:: Initialize n to 0
set "n=0"

:: Use delayed expansion to create an array of
:: directories in the current directory
for /f "delims=" %%i in ('dir /a:d /b') do (
    set /a n+=1
    set var!n!=%%i
)

:: Display the list of all var_ found
set var

pause
Sign up to request clarification or add additional context in comments.

5 Comments

I tried that, but 'set var' returns 'var!n!=Windows' (Windows is the last folder in the dir) do have setlocal and enabledelayedexpansion
@ThatCyanTypo - Then you did it wrong. Copy and paste my code exactly as I have written it.
I found the issue, i had setlocal and enabledelayedexpansion on different lines due to my syntax highlighter in NotePad++(it didnt show as a working command when they were on the same line)
Yeah, Notepad++'s syntax highlighting could do with some tweaking.
Thanks for your help! Funny that i'm better at Python than BATCH xD

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.