Basically I'm trying to do !var1:SomeText=!var2!! but this code doesn't work. What am I missing?
Batch Scripting Help - Replace Substring of a DelayedExpansion Var with another DelayedExpansion Var
2.5k Views Asked by Kigen At
1
There are 1 best solutions below
Related Questions in VARIABLES
- Simplexml get path from variable
- Python - Dynamically naming variables
- Azure Web App PATH Variable Modification
- How can I pass variable to ansible playbook in the command line?
- Fetching URL vars into form and submitting to other page
- Iframe not passing url parameters
- Stata: Retrieve variable label in a macro
- How to make all variables in javascript script have two decimal points
- Javascript works with HTML but not in Rails
- Running Line of code twice causes activeX error 0x8000ffff
- Erlang syntax error unclear
- CMD specifying columns to save?
- Simplify code with sending Methods as Variable
- Calling a variable in Matlab without using the full name?
- How to call c-style cleaner functions implicitly?
Related Questions in BATCH-FILE
- Get Maximum Log Size
- Running batch fine onclick delay
- Automating Telnet Scripts from .bat with a teamspeak instance
- Variable setting exhibits unexpected behavior when set inside for loop
- Execute a command over multiple files
- How to run multiple "mvn test"-commands from batch file?
- Referencing a Schema's table batch/perl
- Can I use \ and / to be directory delimiter in CLASSPATH of .bat
- SSIS exit code error with batch file
- CMD/BATCH - Had to run batch script 3 times to get full result
- Trying to create a Bath file that will copy a specific directory path (no lower) and zip the directories in that path
- curl command don't work in some cases
- Set environment in current cmd using batch script
- What is the correct way to pass the password to OpenSSL
- Batch file to process files in sub sub directory
Related Questions in SUBSTRING
- Impala Query: Combine multiple COUNT DISTINCT WHERE clauses
- How to i extract a substring from a string in Python and store it in a list?
- String index out of bound exception in if statement
- Saving multiple occurrences of strstr() from a line in C?
- Error in printing all substrings of a string
- Function for word by word string comparison in T-SQL
- How to construct a suffix Trie for all the substrings of a string?
- TERADATA - How to split a character column and keep the last token?
- Excel VBA Macro to get the substring of text before semicolon
- Remove first part of the string by batch command
- Substring a stringRaw from String1 to String2
- How to split dataframe column in R
- Is the time complexity of this code O(N^2)
- How to make a youtube link as embed?
- How to get an specific extract from a String?
Related Questions in WINDOWS-SCRIPTING
- PsExec fails to open the stdin, stdout and stderr named pipes
- JScript is crashing with a very poor reason given
- Undo the actions performed by a .bat file?
- How can I write "exit" word to txt from bat file?
- Use environment variable set by a batch script in the next batch script run
- How to pipe bash script output to a file on windows
- need to check the version value in text file using vbscript
- Why does this jenkins job never complete?
- Is it possible to assign a logon script to a user in a windows machine which is not a part of domain?
- How to call/ execute .WSF (windows script file) from browser
- Visual Studio Build Agent scripted setup
- When to choose development of a PowerShell Module over PowerShell Script
- Getting Working Processes within IIS App Pools
- Implementing a Windows script
- parse the source safe history output and run the diff command
Related Questions in DELAYEDVARIABLEEXPANSION
- Batch File Pass Delayed Variable Expansion As Argument
- Why can I not get a substring of a delayed expansion variable in an if statement?
- Inner delayed expansion in a batch script (windows)
- Watermark Not Working For Videos With Exclamation Marks
- windows batch SET inside IF not working
- Loop through "array" in batch file to shift elements
- Why can't I access local variable in for loop (batch file) even with setlocal enabledelayedexpansion?
- Find the latest sub-folder within a directory only if that directory exists batch script
- Batch delayed expansion not working
- Batch Scripting Help - Replace Substring of a DelayedExpansion Var with another DelayedExpansion Var
- How to call one Windows batch script from another with delayed expansion on in both
- how to store jpg image dimensions using for command, store the results in a var, and use the var in an imagemagick command line
- String replace with delayed expansion (case sensitive) - Batch
- Batch array, associative array, and how to navigate in array
- Evaluating a DelayedExpansion variable as a drive letter when checking for file existence
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
The order of expansion is critical when doing a search and replace operation that uses a variable for the search and/or the replace. The inner variable must be expanded before the outer search and replace expansion takes place. Trying to used delayed expansion for both obviously can't work because the delayed expansion occurs at one point in time.
The classic method for expansion of a variable within another variable uses delayed expansion for the outer, and normal for the inner:
echo !var1:SomeText=%var2%!"I am going to assume you wanted to use delayed expansion for both for a reason. Perhaps the expansion occurs within a block of code and one of the variables was set in the same block. Normal expansion won't work because it can't see the value that was assigned within the block until after the block concludes.
Solution 1
One way to solve the problem is to use CALL:
This works as follows:
The percent phase of the parser converts double percents into single percents, resulting in
call echo %var1:SomeText=!var2!%The delayed expansion expands !var2!, resulting in
call echo %var1:SomeText=ReplacementText%The CALL ECHO is executed and an additional level of percent processing takes place. The search and replace expansion is executed, resulting in
ResultOfSearchAndReplacebeing echoed to the screen.This works, but it is relatively slow. It also can have problems if the expanded value has special characters like
>,&or|. I rarely use this technique.Solution 2
The fast and more reliable method is to do the expansion in two steps. First transfer the value of
!var2!to a FOR variable. You can then use the FOR variable as the replacement string and use delayed expansion for the second step. This completely avoids the more brittle percent expansion.The above works because FOR variable expansion takes place before delayed expansion.
This is by far my preferred method to attack this problem.
For a more thorough explanation of the various phases of the batch parser, refer to jeb's answer to How does the Windows Command Interpreter (CMD.EXE) parse scripts?