Skip to content

Instantly share code, notes, and snippets.

@Ephellon
Last active October 5, 2017 04:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ephellon/8a942ea4bf271cf880dccc4eade49091 to your computer and use it in GitHub Desktop.
Save Ephellon/8a942ea4bf271cf880dccc4eade49091 to your computer and use it in GitHub Desktop.
Replace asterisks in BAT variables
:: 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