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
4
votes
1 answer

Batch Scripting Help - Replace Substring of a DelayedExpansion Var with another DelayedExpansion Var

Basically I'm trying to do !var1:SomeText=!var2!! but this code doesn't work. What am I missing?
3
votes
1 answer

How to copy un-sanitized string to clipboard with batch script

I want to put a user inputted string which contains an ampersand into the clipboard using batch. I can modify the string, and I can get it to print to the terminal using setlocal EnableExtensions EnableDelayedExpansion but I can't pipe it to the…
3
votes
2 answers

using a DelayedExpansion index variable for an array within an IF statement fails

I have a batch file where I am displaying all *.pem files in a directory, giving the user an option to choose one, and at this time just trying to show the user what file they picked by using the number they chose to ECHO the contents of the array.…
3
votes
1 answer

Why does FOR fail with delayed sub-string expansion after IN?

Sub-string expansion works within the set of a for loop (that is the parenthesised part after in) when immediate expansion is used (write %%I instead of %I in a batch file): set "X=123" for %I in (%X:~1,1%) do @echo %I However, it fails when…
aschipfl
  • 33,626
  • 12
  • 54
  • 99
3
votes
1 answer

Batch script - Set/Define environment variable if not already set/defined

I need a batch script to be invoked as SetEnvironmentVariableIfNotSet.bat environment_variable_name from another script or the command line. I am by no means specialist in Windows batch scripting but by trial and error and piecing different…
CristiArg
  • 103
  • 2
  • 7
3
votes
1 answer

Delayed expansion errors

I'm playing around with batch files and delayed expansions. I am simply attempting to print the file/folder names of all items within a directory. @ECHO off SET title=%1 SET mp4="mp4" SET mkv="mkv" SET avi="avi" SET minimumSize=300 CD…
wmash
  • 4,032
  • 3
  • 31
  • 69
3
votes
3 answers

How to find and replace a string in %PATH% system variable with variables in a batch file?

I am trying to upgrade Java with a batch file, and I need to change the PATH system variable to reflect that change. At the beginning of the PATH variable I have C:\Program Files\Java\jdk1.8.0_51;... I need to change the jdk value to be…
2
votes
2 answers

Batch - return value from inside a loop

I have the following Problem: I have 2 functions/labels called STARTERand Get_age. The Get_age function stores ages and names in some related variables and shall return the age of the person i passed to it (passed the name). But the variable which…
2
votes
2 answers

Batch - Get System Language of current running System

I am trying to get the current setted system-language under use of wmic, but i don´t know why my code here is kind of "broken"... The readback info from wmiccontains a lot of spaces and CRLF´s. Therefore i wanted to cut all that out in the loop, but…
2
votes
0 answers

delaydexpansion and endlocal conflict, any solution?

I find it an embarrassment when I want to use endlocal inside an if/for statement. Please check this delayexpe.bat : @echo off setlocal EnableDelayedExpansion set dvar=oldata if "1" == "1" ( REM Deliberately need a delayed-expansion context …
Jimm Chen
  • 3,411
  • 3
  • 35
  • 59
2
votes
2 answers

How to use delayed expansion on a variable like %%a or %1?

I'm trying to make a batch file I call from another batch file, but the variables don't work, all the results are "a", while the expected result would be option1=a, option2=b, etc. Here's the code to demonstrate the issue: call temp.bat a b c d e f…
2
votes
1 answer

How to call one Windows batch script from another with delayed expansion on in both

I have two .bat files, both use delayed expansion, so that I can set variables within for loops. The example below is greatly simplified just to show the problem Script one.bat @echo off setlocal enableextensions setlocal…
2
votes
3 answers

Windows command working in cmd but not .bat file

I am trying to replace a line in certain files (based on file extension). The program is not working as desired, and the command which is causing issue is the one below. FOR /F %%k IN ('TYPE !FILE! ^| FINDSTR /N "^"') DO ( This comes back with…
stariq
  • 23
  • 5
2
votes
1 answer

How to set %ERRORLEVEL% in cmd /c?

I want to be able to set the %ERRORLEVEL% environment variable (also referred to as a "system variable") to an arbitrary value in the beginning of a command prompt script (i.e. cmd /c) running in PowerShell. Whenever people want to set %ERRORLEVEL%…
Code Doggo
  • 2,146
  • 6
  • 33
  • 58
2
votes
2 answers

Keep variable after endlocal in Batch

I have a folder structure, which is like for example C:\Temp\ and there are a lot of folder and file, and within each folder there are a "callme.bat". I would like to create a so called main.bat which is one after another call the callme files…
Gregori
  • 85
  • 7
1
2
3
8 9