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
1
vote
1 answer

How to avoid delayed expansion removing the exclamation marks in file names?

How to avoid delayed expansion removing the exclamation marks in file names? @echo off Set path="C:\example" Set chars=a b c cd /d %path% setlocal EnableDelayedExpansion for %%A in (%chars%) do ( set filesList= for /F "tokens=* delims="…
1
vote
1 answer

bat file enabledelayedexpansion won't expand in IF statement

When I try delayed expansion of an environment variable with a : clause inside a .BAT file IF statement the line fails to parse/execute. Using % signs instead of ! for the expansion works. The identical delayed expansion works in other places, eg…
Paul Houle
  • 735
  • 9
  • 17
1
vote
2 answers

Redefining the value of a variable that has been defined together with another variable

I am currently using this method from "jeb" to handle many variables. BAT-file: variable contents as part of another variable setlocal EnableDelayedExpansion set arr[1]=17 set arr[2]=35 set arr[3]=77 ( set idx=2 set /a var=arr[!idx!] echo…
1
vote
2 answers

Looping through string values using set in Windows cmd

My Windows batch file is supposed to read file names and create a directory that is named according to the filename's 2nd to 5th letter: for %%f in (.\*.txt) do ( set string=%%~nf mkdir %string:~2,5% ) The value of 'string' is not updated…
Joan
  • 33
  • 3
1
vote
1 answer

Missing "!" string when EnableDelayedExpansion

When I enabled DelayedExpansion in the script, it doesn't echo out the "!" string in the file name. For instance: Original File01-TEXT!.txt Echo out File01-TEXT.txt I guess it's because of the setlocal EnableDelayedExpansion, but I can't remove…
WhatWhereWhen
  • 473
  • 1
  • 5
  • 6
1
vote
2 answers

Exclamation (!) Removed from CMD %CD% Output

The Windows CMD shell is stripping the "!" character from directory names. Assume the current working directory is "C:\\!MyFolder" Inside a .CMD file I use this syntax: set _STARTPATH=%CD% echo %_STARTPATH% C:\MyFolder is displayed without the bang…
1
vote
2 answers

batch scripting getting arguments to handle argument plus one

I want to be able to get the next argument to compare to the current argument. So when the argVec is equal to "--define", I want to echo the next argument. I get the result "y" instead of "delivery". My input is: Cmd version version1 --define…
1
vote
1 answer

Delayed variable expansion inside a substring operation

SETLOCAL EnableDelayedExpansion SET str=123456789abcdefgh FOR /l %%x IN (1, 1, 10) DO ( SET /a intLength=10-%%x SET result=!str:~-%%x! ECHO "Works as intended: " !result! SET result=!str:~-intLength! ECHO "Does NOT work as…
Bob
  • 109
  • 1
  • 8
1
vote
1 answer

Batch File Pass Delayed Variable Expansion As Argument

I have a batch variable, which contains the name of another variable. When I perform a CALL ECHO on the variable, the output is what I expect. However, when I try to pipe the output of the CALL ECHO command as an argument to another command, I am…
Jeff G
  • 4,470
  • 2
  • 41
  • 76
1
vote
1 answer

How would I call a dynamic variable name?

Okay, so I'm trying to make a program that "understands" user input and does what they tell it to do. People usually just use specific commands such as "open this file" and it only works if the user types EXACTLY that. I'm trying to give my users a…
Aty'ai
  • 37
  • 7
1
vote
1 answer

Batch for reading a file and output line number + token?

This is what I want to do, a batch that reads a file (e.g. file.txt) and output line# + token. This is what I tried to do (which obviously didn't work): set count=0 set InputFile=file.txt for /f "tokens=1-3 delims=," %%A IN (%InputFile%) DO ( …
1
vote
1 answer

cmd and exclamation marks

the following code fails for folder names containing exclamation marks. I think I need DelayedExpansion enabled to handle the nested for loops. Any ideas to get this work? Thanks! @echo off & setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION set…
CmdQuestion
  • 142
  • 1
  • 2
  • 8
1
vote
3 answers

Nested variable name using EnableDelayedExpansion

I am working on a script to get max lengths of each column, I'm trying to store lengths of max length in _c1...n vars. number of columns unknown. I was able to get length for each column, create variables to store each with set _c!i! = !n!, n is the…
demircioglu
  • 3,069
  • 1
  • 15
  • 22
1
vote
2 answers

Windows batch file - Pick (up to) four random files in a folder

As the title says, I'm trying to pick up to four random files (wallpapers) from a folder, for further processing. The folder does not contain subfolders, just *.jpg's, *.bmp's and *.png's (it may contain a Thumbs.db file, but I already took care of…
0
votes
1 answer

Batch to read out strings from txt with special characters

I am trying to get all known WiFi SSIDs including the Password. When I use the following script, I can't get it to work to set the SSIDs in doublequotes, (so blanks and so on are correctly used). Also when I've got special characters like…
Markus
  • 41
  • 1
  • 4
1 2 3
8 9