简体   繁体   中英

Replace %%20 in string using windows batch script

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 and Option3 . And it would have been good to see the lines which define the environment variables Option1 , Option2 and Option3 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 all Option environment variables.

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "Option1=Calculation%%20One"
set "Option2=Calculation %%%%20Two!"
set "Option3=Any other string"

cls
echo The options before processing:
echo/
set Option

for /F "delims==" %%I in ('set Option 2^>nul') do call :ProcessOption "%%I"

echo/
echo The options after processing:
echo/
set Option
echo/
endlocal
pause
goto :EOF

:ProcessOption
setlocal EnableDelayedExpansion
set "ModifiedOption=!%~1:%%20= !"
endlocal & set "%~1=%ModifiedOption%"
goto :EOF

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 string Option in format variable=value as it can be seen twice on running this batch file.

FOR executes this command with using 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 as 2>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 with Option 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 executes cmd.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 with delims== . The string left to first equal sign on each line is assigned to loop variable I . This is the name of the environment variable, ie Option1 , Option2 and Option3 for this batch file.

With each OptionX environment variable name the subroutine ProcessOption 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.

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "!Option1!=Calculation%%%%20One"
set "!Option2!=City%%20Country"
set "!Option3!=State%%%%20Country"
set "!Option4!=Any other address/string"

cls
echo The options before processing:
echo/
set !Option

for /F "delims==" %%I in ('set !Option 2^>nul') do call :ProcessOption "%%I"

echo/
echo The options after processing:
echo/
set !Option
echo/
endlocal
pause
goto :EOF

:ProcessOption
call set "ModifiedOption=%%%~1%%"
setlocal EnableDelayedExpansion
set "ModifiedOption=!ModifiedOption:%%%%20= !"
set "ModifiedOption=!ModifiedOption:%%20= !"
endlocal & set "ModifiedOption=%ModifiedOption%"
set "%~1=%ModifiedOption%"
set "ModifiedOption="
goto :EOF

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 with Option 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 of echo . to output an empty line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM