How to Replace Character "-" by "%_%" in a Batch File?

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

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

Chloe Bennett
Author

Chloe Bennett

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.