Questions tagged [delayedvariableexpansion]

Delayed variable expansion causes a Windows batch file to evaluate variables at run time instead of parse time.

Variables in a Windows batch files are normally evaluated when the script is parsed. With delayed variable expansion, variables are evaluated at execution time instead. This is especially useful in FOR loops. To turn on, use setlocal EnableDelayedExpansion. To turn off, setlocal DisableDelayedExpansion.

127 questions
128
votes
2 answers

windows batch SET inside IF not working

when I'm running this script (from a .bat file): set var1=true if "%var1%"=="true" ( set var2=myvalue echo %var2% ) I always get: ECHO is on. Meaning the var2 variable was not really set. Can anyone please help me understand why?
Orad SA
  • 1,885
  • 5
  • 16
  • 16
80
votes
12 answers

Arrays, linked lists and other data structures in cmd.exe (batch) script

I was playing with cmd.exe, but in its help I didn't find any info, how to define arrays. I have found, how to define simple variables: set a=10 echo %a% But, I want to create arrays, linked list etc... So, does it able in cmd.exe ( I mean: does in…
user1131997
75
votes
4 answers

How do SETLOCAL and ENABLEDELAYEDEXPANSION work?

I notice in most scripts, the two are usually in the same line as so: SETLOCAL ENABLEDELAYEDEXPANSION Are the two in fact separate commands and can be written on separate lines? Will setting ENABLEDELAYEDEXPANSION have an adverse effect on a script…
Anthony Miller
  • 15,101
  • 28
  • 69
  • 98
61
votes
5 answers

Example of delayed expansion in batch file

Can someone give me an example of where a batch script would act differently with or without delayed expansion? Are there any situations where you would NOT want to use delayed expansion? Thanks.
51
votes
2 answers

Windows Batch Variables Won't Set

I think I ran into a bug in Window's batch scripting. I cannot set variables that are in an expanded if statement. Here is an isolated part of my script: @echo off set success=1 set Version=12345 set Target=Client set Type=456 set dir= set zip= if…
Nyaarium
  • 1,540
  • 5
  • 18
  • 34
31
votes
1 answer

Variables are not behaving as expected

I've been wrestling trying to get the syntax right on this batch file and I cannot figure out why some things aren't working. The variable i is not getting incremented each time I do it. Concatenation on strc doesn't seem to concatenate. Here is…
20
votes
1 answer

Why are my set commands resulting in nothing getting stored?

I am trying to access the value of TOMCAT_VER later on, but it appears as an empty string. if exist "%_REALPATH%\tomcat-%TOMCAT_VER2%" ( set CATALINA_HOME=%_REALPATH%\tomcat-%TOMCAT_VER2% set TOMCAT_VER=%TOMCAT_VER2% echo "%TOMCAT_VER%" ) else…
gregturn
  • 2,625
  • 3
  • 25
  • 40
16
votes
2 answers

Why can I not get a substring of a delayed expansion variable in an if statement?

Why does string-manipulation work inline in an if statement without using delayed expansion variables - but fails otherwise. For example: set test=testString if %test:~0,4%==test echo Success This works correctly; returning Success. However if I do…
unclemeat
  • 5,029
  • 5
  • 28
  • 52
9
votes
2 answers

Why setlocal interferes with chdir in windows batch files?

If I run the batch file setlocal chdir .. the directory is not changed, but if I run setlocal endlocal chdir .. it works normally. This must be exactly what is expected with setlocal. However, it is not entirely obvious when you read the…
user2066805
6
votes
4 answers

Batch delayed expansion not working

Ok, I'm getting crazy and I don't know what else to do, I've tried several things and nothing is working. Look at this sample code (test.cmd): setlocal enabledelayedexpansion enableextensions set VAR=before if "%VAR%" == "before" ( set…
Richard
  • 63
  • 1
  • 1
  • 4
5
votes
1 answer

Batch file : SET variable=string doesn't work

I've this code : if %Ret:~6,4% EQU %Year% ( SET test=text ECHO %test% ) else ( ECHO NO ) The code enters in the if loop but it returns always Echo is off! I've pay attention to the space before and after the =. Any ideas?
Alice
  • 313
  • 2
  • 5
  • 16
4
votes
1 answer

Batch: Returning a value from a SETLOCAL EnableDelayedExpansion

I wonder why this code does not work as expected: @ECHO off SET S1=HELLO SETLOCAL EnableDelayedExpansion SET S2=!S1! WORLD^^! ECHO !S2! ENDLOCAL & SET S1=!S2! ECHO %S1% PAUSE Output: HELLO WORLD! !S2! Expected output: HELLO WORLD! HELLO…
mcu
  • 3,302
  • 8
  • 38
  • 64
4
votes
1 answer

wrong value of %errorlevel% in nested if in .bat file

I have written a .bat file to first run a program, if it is correctly finished I run another program and check return value of it. first-program.exe IF "%ERRORLEVEL%"=="0" ( second-program.exe IF "%ERRORLEVEL%"=="0" ( ECHO OK )…
Ebrahimi
  • 1,209
  • 8
  • 15
4
votes
2 answers

Passing exclamation marks as parameters in batch subroutine call

Thanks to this community I have finally learned how to escape exlamation marks for immediate use in a batch delayedExpansion block. (use two escape carets not just one, awesome) But I can't seem to find or figure out how to pass the contents of a…
4
votes
1 answer

Cmd and exclamation marks - Part II

I'm really wondering why my string replacement procedure works when parsing text files containing any special characters including exclamation marks. I expected that delayed variable expansion would switch off special meaning of ampersand, percent…
CmdQuestion
  • 142
  • 1
  • 2
  • 8
1
2 3
8 9