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
- Azure IOT Hub Rest API Unauthorized
- Stream Analytics: Dynamic output path based on message payload
- Iot Hub connection string not working in azure-iot-sdk-c samples
- Azure,Android,Raspberry pi
- Any APIs to get list of consumer groups created in Azure IotHub?
- In queue message count in service bus
- Uploading an image with Azure IoT SDK for Python
- Azure IoT Python SDK how to set content type on uploaded images
- Bi-Directional Communication via IoTHub/Xamarin App/ESP8266
- How to delete all devices from Azure IoT Hub using C#?
Related Questions in BATCH-FILE
- Azure IOT Hub Rest API Unauthorized
- Stream Analytics: Dynamic output path based on message payload
- Iot Hub connection string not working in azure-iot-sdk-c samples
- Azure,Android,Raspberry pi
- Any APIs to get list of consumer groups created in Azure IotHub?
- In queue message count in service bus
- Uploading an image with Azure IoT SDK for Python
- Azure IoT Python SDK how to set content type on uploaded images
- Bi-Directional Communication via IoTHub/Xamarin App/ESP8266
- How to delete all devices from Azure IoT Hub using C#?
Related Questions in SUBSTRING
- Azure IOT Hub Rest API Unauthorized
- Stream Analytics: Dynamic output path based on message payload
- Iot Hub connection string not working in azure-iot-sdk-c samples
- Azure,Android,Raspberry pi
- Any APIs to get list of consumer groups created in Azure IotHub?
- In queue message count in service bus
- Uploading an image with Azure IoT SDK for Python
- Azure IoT Python SDK how to set content type on uploaded images
- Bi-Directional Communication via IoTHub/Xamarin App/ESP8266
- How to delete all devices from Azure IoT Hub using C#?
Related Questions in WINDOWS-SCRIPTING
- Azure IOT Hub Rest API Unauthorized
- Stream Analytics: Dynamic output path based on message payload
- Iot Hub connection string not working in azure-iot-sdk-c samples
- Azure,Android,Raspberry pi
- Any APIs to get list of consumer groups created in Azure IotHub?
- In queue message count in service bus
- Uploading an image with Azure IoT SDK for Python
- Azure IoT Python SDK how to set content type on uploaded images
- Bi-Directional Communication via IoTHub/Xamarin App/ESP8266
- How to delete all devices from Azure IoT Hub using C#?
Related Questions in DELAYEDVARIABLEEXPANSION
- Azure IOT Hub Rest API Unauthorized
- Stream Analytics: Dynamic output path based on message payload
- Iot Hub connection string not working in azure-iot-sdk-c samples
- Azure,Android,Raspberry pi
- Any APIs to get list of consumer groups created in Azure IotHub?
- In queue message count in service bus
- Uploading an image with Azure IoT SDK for Python
- Azure IoT Python SDK how to set content type on uploaded images
- Bi-Directional Communication via IoTHub/Xamarin App/ESP8266
- How to delete all devices from Azure IoT Hub using C#?
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 # Hahtags
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
ResultOfSearchAndReplace
being 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?