0

I have a variable set to a value. I want to escape multiple characters in the string with a backtick (`). Here is an example of replacing occurences of a parenthesis with a backtick:

@echo off

REM SET EXAMPLE VALUE
set EXAMPLEINSTALLDIR="C:\Program Files(x86)"
echo %EXAMPLEINSTALLDIR%

REM REMOVE QUOTES
set EXAMPLEINSTALLDIR=%EXAMPLEINSTALLDIR:"=%
echo %EXAMPLEINSTALLDIR%

REM REPLACE ( WITH `(
set EXAMPLEINSTALLDIR=%EXAMPLEINSTALLDIR:(=`(%
echo %EXAMPLEINSTALLDIR%

I'd prefer to have one function to replace all specified characters (*[]+ etc). But I'm struggling. From an example found here: http://www.dostips.com/DtTutoFunctions.php

I've tried this:

set EXAMPLEINSTALLDIR="C:\Program Files(x86)\Test"
echo %EXAMPLEINSTALLDIR%
call:myGetFunc EXAMPLEINSTALLDIR %EXAMPLEINSTALLDIR%
echo %EXAMPLEINSTALLDIR%

:myGetFunc
set "%~1=%%~2:(=`(%
goto:eof

But it doesn't work. Any advice appreciated.....

Captain_Planet
  • 846
  • 9
  • 23

3 Answers3

2

When you have two % signs next to each other, batch will interpret it as the type of variable used inside for loops (or as a literal %) and not do what you want. You can get around this by either using delayed expansion and placing the replace code inside of ! or you can set the value to replace inside its own unique variable like this:

@echo off
set example_dir="C:\Program Files(x86)\test"
echo %example_dir%
call :addTick example_dir %example_dir%
echo %example_dir%
exit /b

:addTick
set return_var=%~2
set return_var=%return_var:(=`(%
set "%~1=%return_var%"
SomethingDark
  • 10,902
  • 5
  • 44
  • 47
0

Use this code:

@echo off

set "EXAMPLEINSTALLDIR=C:\Program Files(x86)"
echo %EXAMPLEINSTALLDIR%

set "EXAMPLEINSTALLDIR=%EXAMPLEINSTALLDIR:(=`(%"
echo %EXAMPLEINSTALLDIR%

set "EXAMPLEINSTALLDIR=%EXAMPLEINSTALLDIR:)=`)%"
echo %EXAMPLEINSTALLDIR%

set "EXAMPLEINSTALLDIR=!EXAMPLEINSTALLDIR:%%=%%%%!"
echo %EXAMPLEINSTALLDIR%

endlocal

It is important to have the double quotes on right place. See answer on specify redirect location using a variable and Why is no string output with 'echo %var%' after using 'set var = text' on command line? and others to understand the difference between

set var="value"

and

set "var=value"

I do not know why you want to insert a backtick as escape character in environment variable value as all the special characters listed at end of help output after entering cmd /? in a command prompt are interpreted literarily if the string containing the special characters is enclosed in double quotes.

And command set does not support regular expression replaces. Therefore several replaces are necessary to escape all special characters with a backtick.

The last one escaping a percent sign with one more percent sign is a little bit tricky. It requires delayed environment variable expansion and specifying 2 percent signs in batch file for each percent sign in string.

BTW: There is environment variable ProgramFiles(x86) containing the path to the default program files directory for 32-bit applications on 65-bit Windows computers.

Community
  • 1
  • 1
Mofi
  • 38,783
  • 14
  • 62
  • 115
0
@ECHO OFF
SETLOCAL
set EXAMPLEINSTALLDIR="C:\Program Files(x86)\Test"
echo %EXAMPLEINSTALLDIR%
call:myGetFunc EXAMPLEINSTALLDIR %EXAMPLEINSTALLDIR%
echo %EXAMPLEINSTALLDIR%
GOTO :EOF

:myGetFunc
set "%~1=%~2"
CALL set "%~1=%%%~1:(=`(%%"
GOTO :EOF

Note that this uses the target variable as a temporary variable for manipulation

Note also the extra "goto :eof" before the subroutine - otherwise batch will simply continue into the subroutine...

@ECHO OFF
SETLOCAL
set EXAMPLEINSTALLDIR="C:\Program Files(x86)\Test"
echo %EXAMPLEINSTALLDIR%
call:myGetFunc EXAMPLEINSTALLDIR %EXAMPLEINSTALLDIR%
echo %EXAMPLEINSTALLDIR%
GOTO :EOF

:myGetFunc
set "%~1=%~2"
CALL set "%~1="%%%~1:(=`(%%""
GOTO :EOF

This version to restore the "quotes"

Note that there are limitations to this technique caused by the special meanings of some characters to batch.

Magoo
  • 68,705
  • 7
  • 55
  • 76