I have a use case where I want to replace %%20 which is part of a string for example: "Calculation%%20One". I want to replace this with "Calculation One".
This where I am stuck:
@echo off
setlocal enableextensions disabledelayedexpansion
>"temp" (
echo !Option1!
echo !Option2!
echo !Option3!
)
set "pattern=%%20"
for /f "usebackq delims=" %%a in ("temp") do (
echo data: %%a
set "line=%%a"
setlocal enabledelayedexpansion
if "!line:%pattern%=!"=="!line!" (
set string=!Option1!
set string2=%!string1!:%%20= %
) else (
set string2=%%a
)
endlocal
)
del /q tempFile
Can someone please help me with this? I have a program which is a combination of batch and python.
Thanks
It is unclear for me why the options are written into a temporary file for processing the values of the environment variables
Option1
,Option2
andOption3
. And it would have been good to see the lines which define the environment variablesOption1
,Option2
andOption3
for the real values assigned to them.So I suggest here a batch file which replaces
%20
as found for example in HTML files or emails by a space character in allOption
environment variables.It is necessary to use delayed expansion on replacing all occurrences of
%20
by a space character because otherwise Windows command interpreter would not know where the environment variable reference with string substitution ends. Replacing a string with percent sign is not possible using immediate expansion for that reason.The command
set Option
outputs alphabetically sorted all environment variables starting case-insensitive with the stringOption
in formatvariable=value
as it can be seen twice on running this batch file.FOR executes this command in a separate command process started in background and captures all lines written by command SET to handle STDOUT.
The string
2^>nul
is executed finally as2>nul
and redirects the error message output by command SET to handle STDERR to the device NUL to suppress it. SET would output an error message in case of no environment variable starting withOption
is defined in current environment. This error condition does not occur with this batch file. Read the Microsoft article about Using Command Redirection Operators. The redirection operator>
must be escaped here with^
to be interpreted first as literal character when Windows command interpreter processes the entire FOR command line before executing the command FOR which executescmd.exe /c set Option 2>nul
in background.FOR processes the captured output of
set Option
line by line and splits each line up into substrings (tokens) with using the equal sign as delimiter as specified withdelims==
. The string left to first equal sign on each line is assigned to loop variableI
. This is the name of the environment variable, i.e.Option1
,Option2
andOption3
for this batch file.With each
OptionX
environment variable name the subroutineProcessOption
is called for replacing all%20
in its value by a space character.Here is a solution for environment variables with one or more exclamation marks in variable name.
This batch code replaces
%20
AND%%20
in all environment variables starting with!Option
in name by a space character in comparison to above replacing just%20
by a space in environment variables beginning withOption
in name.It is of course a bad idea to use characters in variable names which have a special meaning for Windows command interpreter like the characters
&()[]{}^=;!'+,`~|<>%
. It is possible as demonstrated above, but it is definitely not a good idea.For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
cls /?
echo /?
endlocal /?
for /?
goto /?
set /?
setlocal /?
See also answer on Single line with multiple commands using Windows batch file for an explanation of
&
operator.And read DosTips forum topic ECHO. FAILS to give text or blank line - Instead use ECHO/ why it is better to use
echo/
instead ofecho
. to output an empty line.