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

Batch: IF and delayed expansion

I have a batch file for downloading youtube videos as 01-title1.m4a, 02-title2.m4a etc. I'm having trouble getting the video number n to work properly, though. If it's a single digit number I want to append a zero in front, but I can't get it to…
H.v.M.
  • 1,348
  • 3
  • 16
  • 42
0
votes
1 answer

Modify for loop to not use delayedexpansion in batch script

In my efforts to understand the for..do loops syntax and their use of %% variables. I have gone through 2 specific examples/implementations where the one for loop does not use DELAYEDEXPANSION and another where it does use DELAYEDEXPANSION with the…
user6003691
0
votes
2 answers

How to use array in batch file?

I'm trying to rename the NIC names on my PC. It has A LOT of NICs (they are virtual). I can get their names like so for /f "tokens=2 delems==" %%A in ('wmic nic where "Description like 'foo'" get netconnection id /value') do ( echo %%A ) I'd like…
Thundercleez
  • 327
  • 1
  • 4
  • 18
0
votes
3 answers

Extract a specific portion from a string(filename) using batch file

I am trying to extract a portion of all the filenames(pdf files) in the current directory. The length of filenames vary except for the last portion(datetime and extension) which will always be 16 characters. The remaining part will always have…
Ash
  • 33
  • 2
  • 9
0
votes
2 answers

Batch file ENABLEDELAYEDEXPANSION is not writing variables out

I have the below code snippet which outputs a variable in to a text file. %drone% will be a simple text string, and the %counter% is a number which is in a loop to count the iterations. The echo %drone% !counter! echos the correct values to the…
0
votes
2 answers

Removing unwanted comma for JSON formatting batch

I'm trying to print out the list of patches in JSON format as such: "patches" : { "1" : "KB123456", "2" : "KB123456", ... However this is what I'm getting: "patches" : { "1" : "", "2" : "patch[2]", "3" : "patch[3]", ... This is my code @echo off…
KH17
  • 111
  • 11
0
votes
2 answers

batch string indexing and delayed expansion

I have stripped my problematic code down to the problematic lines. Why the first echo works and the second one does not work? setlocal enabledelayedexpansion set /a r=19 set blocks=MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM for /L %%a in…
Antoni Gual Via
  • 714
  • 1
  • 6
  • 14
0
votes
1 answer

If statement being ignored when using delayed expansion

I have a batch file that takes two, renames the first file with one name and the second file another name. A variable is set to Y and then delayed expansion is used on the variable in an if statement. The code reads in the two files form the SOURCE…
user2073183
  • 141
  • 1
  • 3
  • 13
0
votes
1 answer

Variable is not set

This is extension of the code from Why the loop is not running as expected? Ultimately I want to read values from Report.txt (a comma separated file having values like 1,2,3,4 in a single line) and update these values in some fields of summary.yml…
bob
  • 71
  • 1
  • 1
  • 6
0
votes
2 answers

Batch: variable definition useable in count loop

In my batch I want to copy a variable amount of source- to target destinations. I want to define like this: @setlocal EnableDelayedExpansion set source1="C:\folder1" set target1="f:\folder1" set source2="C:\folder2" set target2="f:\folder2" …
Trashy
  • 3
  • 2
0
votes
1 answer

Assigning a variable inside if loop : Windows Bat script

I'm trying to write a bat script and faced the following issue. Below is the snippet with which the issue is dealt with. setlocal enabledelayedexpansion enableextensions @echo off @echo: echo Select an option: echo ***************** @echo: echo 1)…
Vignesh
  • 364
  • 2
  • 11
  • 19
0
votes
1 answer

Windows Batch: For Block with variable within variable

I want to make a variable Variable, which means that i want to create many variables with variable names, e.g. Var1, Var2, Var3, Var4. This works (see below), but ECHOing was not working since i have to use EnableDelayedExpansion due to single…
0
votes
3 answers

Two For Loops Using %1 - Delayed Expansion?

My working batch file scans a long list of remote servers, copies anything there to a local server, checks the log file for a keyword, and if the keyword is found sends an email. I noticed it is always sending emails, even with a blank log file. I…
UserUnknown
  • 172
  • 2
  • 15
0
votes
1 answer

scripting - delayedexpansion - outside of the for loop

I am trying to use the variable outside of the For loop. The echo inside the loop gives the result as expected. it is not working when i echo the variable outside of the loop. below is the script- 'SETLOCAL ENABLEDELAYEDEXPANSION SET x=0 FOR /f…
0
votes
1 answer

How to quote bash flag arguments to pass through one getopts call and be interpreted by a second?

Script nerf calls script herd, which calls script er. nerf uses a flag on herd that explicitly takes arguments needing to be passed to er. This was not a problem before nerf existed - when herd was just called from the command line, we could…
1 2 3
8
9