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
2
votes
0 answers

Windows Batch - variable as string offset? Data extraction from multiple files

Preface: Things are pretty locked down at work and I can't use powershell or any macros/scripting in Excel. I can't take files off-site or use on personal computers. I don't have access to the originating program for the files. I seem to be limited…
2
votes
0 answers

String replace with delayed expansion (case sensitive) - Batch

I would like to write a really simple script to "encode" and "decode" strings, by replacing each character with a number, with delayed expansion enabled. But the syntax I use should be wrong as it doesn't work at all. Plus, is there a way to perform…
2
votes
1 answer

Variables not set in the batch file

setlocal enabledelayedexpansion If "%computername%"=="USER-PC" ( set abc = ZZZ.bat echo %abc% pause ) Here abc always shows blank. What could be the possible reason?
Lalatendu Bag
  • 11
  • 1
  • 3
2
votes
0 answers

Delayed expansion with batch script loops

I have a batch script which creates some arrays in order to store some WMI data regarding disk space in partitions, which I'd then like to loop through. It seems there is some problem with the way I'm trying to take advantage of the delayed…
2
votes
3 answers

Workaround for problems with "!" literals in a For loop with delayed variable expansion

How do I make a workaround for the FOR Loop? The problem is that DelayedExpansion fails for filenames with "!" in them The code (this is part of a bigger script) :FileScan ::Sets the filenames(in alphabetical order) to variables SETLOCAL…
Mav
  • 47
  • 1
  • 5
2
votes
1 answer

Escaping exclamation marks required in replace string but not in search string (substring replacement with delayed expansion on)?

Supposing one wants to replace certain substrings by exclamation marks using the substring replacement syntax while delayed expansion is enabled, they have to use immediate (normal) expansion, because the parser cannot distinguish between !s for…
aschipfl
  • 33,626
  • 12
  • 54
  • 99
2
votes
2 answers

Windows Batch File - ENABLEDELAYEDEXPANSION query

Having read an existing post on stackoverflow and done some reading around on the net. I thought it was time to post my question before I lost too much hair! I have the following code within a batch file which I double click to run, under Windows XP…
Matt Woodward
  • 1,941
  • 20
  • 24
2
votes
2 answers

How to use delayedexpansion to handle value in batch script

I am facing issue with reading contents of CSV files in batch script. I have a series of files say My_A_File.csv, My_B_File.csv ... My_Z_File.csv. The issue I was facing is reading special characters in them. Hence, wanted to read the values with…
James Jithin
  • 10,183
  • 5
  • 36
  • 51
2
votes
2 answers

Issue with *For loop* and *delayed expansion*

This batch is to create a report which outputs the latest file of a folder. Main purpose is to see the date of generated files inside directories. To also easily check whether the target directory is update or not. Issue : Change the output to get…
2
votes
2 answers

Why the loop is not running as expected?

I have folders like E:\Backups\code\Hazard\test1 ... testn And inside these test folders something like E:\Backups\code\Hazard\test1\it0 ... itn The root folder is E:\Backups\code from where the code runs. The below code runs on each subfolders and…
bob
  • 71
  • 1
  • 1
  • 6
2
votes
1 answer

batch file delayed expansion line by line file copy that contains exclamation points

Ok, so I have two files which contain identical text with the exception of one line, what I'm doing is copying the differing line from the %source% file to the %target% file (which in essence is just writing the whole file line by line to the new…
kjg5763
  • 37
  • 3
2
votes
1 answer

Bizarre behavior with DOS batch if statement: expected or bug?

I just had something completely bizarre come up, and I was wondering if this was expected behavior or if I've found some sort of strange bug in batch file processing. Without going into the details of what I'm trying to do, below is an example…
2
votes
2 answers

Different behavior with delayed variable expansion in batch script in 2 identical piece of code

@echo off pushd setlocal enabledelayedexpansion enableextensions set VARY=before if "!VARY!" == "before" ( set VARY=2 if "!VARY!" == "2" @echo If you see this, yes echo !VARY! ) set VAR=before if "!VAR!" == "before" ( set VAR=1 if "!VAR!" ==…
2
votes
3 answers

Batch File w/ Delayed Variable Expansion - URL Encoding - equal sign - RSA Token Converter

I am automating RSA soft token conversion and passing the conversion string over to Outlook. I realize I cannot make the string into a URL using a batch to outlook command line, however that is not my issue. My issue is converting the pesky equal…
Justin
  • 569
  • 2
  • 5
  • 8
2
votes
2 answers

Dos inline IF test for errorlevel, without use of Delayed Expansion

Is there anyway to do the below without delayed expansion (one line, broken for readability)? %comspec% /v:on /c %windir%\System32\reg.exe import c:\temp\test.reg & if !errorlevel! neq 0 pause If it were an echo, I know that call can be used, but…
user66001
  • 774
  • 1
  • 13
  • 36
1 2
3
8 9