How to replace character "-" by "%_%" in a batch file?

set var=this-is-a-test
ECHO I would like to convert the value of this variable to "this%_%is%_%a%_%test"

I tried with:

SET VAR=%VAR:-=%_%%

But doesn't work :(

Please help

1 Answer

Because of the way percents are used for variables it messes up what you are trying to do. One way around this is with delayed expansion. You also have to escape the percent by putting two of them in a row %%.

Something like:

Setlocal EnableDelayedExpansion
set _name=s-t-r-i-n-g
set _name=!_name:-=%%_%%!
echo %_name%
2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like