Last active
October 5, 2017 04:18
Replace asterisks in BAT variables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:: Replace all asterisk characters in a variable | |
:: Usage: | |
:: set-ast <variable-name> <replacement-value> | |
@echo off | |
@setlocal enabledelayedexpansion | |
set variable=%~1 | |
set value=!%~1! | |
set replacement=%~2 | |
:loopback | |
set righthand=!value:**=! | |
if ["%value%"]==["%righthand%"] (goto quit) | |
if ["%righthand%"]==[""] ( | |
set lefthand=%value:~,-1% | |
) else ( | |
set lefthand=!value:%righthand%=! | |
set lefthand=!lefthand:~0,-1! | |
) | |
set value=%lefthand%%replacement%%righthand% | |
goto loopback | |
:quit | |
for /f "delims=" %%v in ("!value!") do ( | |
endlocal | |
set "%variable%=%%v" | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment