-3

I would like to create a batch script which allows to create number and name of folder chosen by the user.

However, I received a syntax error using the script below.

variable "nome" is not taken.

Here is my code:

echo How many folders? 
set /p  cc=tell me how many
SETLOCAL EnableDelayedExpansion
FOR /L %%G IN (1,1,%cc%) DO (set /p nome=tell me the name 
md %nome%)
pause
Jan_V
  • 4,244
  • 1
  • 40
  • 64
mr xyz
  • 1
  • 1
  • 3
    you've got a [delayed expansion](https://stackoverflow.com/a/30284028/2152082) problem. – Stephan Sep 01 '17 at 16:56
  • Thanks a lot,i did not understand what was the problem in the way i wrote my question?Mustn't it have spaces between the lines? – mr xyz Sep 01 '17 at 17:54
  • As you were correctly informed, you have a delayed expansion problem, which can easily be fixed by replacing two characters. – Compo Sep 01 '17 at 19:25
  • I understood that,i fixed my problem now.I have just asked why my post was edited in the form by Stephan?What did i do wrong?Maybe spaces between the rows?Just to not repeat the error next time – mr xyz Sep 01 '17 at 19:28
  • Code on StackOverflow is traditionally indented four spaces to be easily read and identified as code. – SomethingDark Sep 01 '17 at 23:42
  • But mine was indented with four spaces as you said,maybe the problem was between the first row and second one,second one and third and so on,there was spaces among them.Still really haven't understood what was the problem in my way of indenting the code. – mr xyz Sep 02 '17 at 12:49
  • @mrxyz: you used only three spaces. I added the fourth to every code line. Best way: write (or paste) it without identation, then mark the whole code and press the `{}` button above the textbox. – Stephan Sep 02 '17 at 16:31
  • Thanks a lot,now i have understood. – mr xyz Sep 02 '17 at 21:26

1 Answers1

0

mr xyz, please use the search bar to search for delayed expansion. It's not that hard.


What Is DelayedExpansion

Batch-file variables are expanding in the moment the line is parsed. That means the nome variable isn't set when the entire for loop is parsed.


How-To Make The Variable Expand At Run-time?

SETLOCAL Method

Add

setlocal enableDelayedExpansion

anywhere before the for loop, so cmd will process the variable at run-time. And change %nome% to !nome!.


CALL MKDIR/MD Method

Change your MD statement to:

call MD %%nome%%

As this will trigger the emulated delayed expansion using the special property of call and %%.