使用Windows批处理脚本替换字符串中的%% 20 [英] Replace %%20 in string using windows batch script

查看:256
本文介绍了使用Windows批处理脚本替换字符串中的%% 20的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用例,其中我想替换作为字符串一部分的%% 20,例如:"Calculation %% 20One".我想用计算一"代替它.

这就是我被困住的地方:

@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

有人可以帮我吗?我有一个结合了批处理和python的程序.

谢谢

解决方案

对于我而言,目前尚不清楚为什么将选项写入临时文件以处理环境变量Option1Option2Option3的值.最好能看到为定义给它们的实际值定义环境变量Option1Option2Option3的行.

因此,我在这里建议使用一个批处理文件,用所有Option环境变量中的空格字符代替例如在HTML文件或电子邮件中找到的%20.

@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

在用空格字符替换所有出现的%20时,必须使用延迟扩展,因为否则Windows命令解释器将不知道使用字符串替换的环境变量引用在何处结束.由于这个原因,无法使用立即扩展用百分号替换字符串.

命令set Option以字母Option的格式输出variable=value,按字母顺序输出所有环境变量(不区分大小写),因为在运行此批处理文件时可以看到两次.

FOR 在后台启动的单独命令过程中执行此命令,并捕获由命令 SET 编写的所有行以处理 STDOUT .

字符串2^>nul最后以2>nul执行,并将命令 SET 输出的错误消息重定向到处理 STDERR 的设备 NUL 强>压制它.如果在当前环境中未定义以Option开头的环境变量,则 SET 将输出错误消息.此批处理文件不会发生此错误情况.阅读有关使用命令重定向操作符的Microsoft文章.在执行命令 FOR 之前,Windows命令解释器处理整个 FOR 命令行时,重定向运算符>必须在此处使用^进行转义,以便首先被解释为文字字符.会在后台执行cmd.exe /c set Option 2>nul.

FOR 逐行处理set Option的捕获输出,并使用delims==指定的等号作为分隔符,将每行分成子字符串(令牌).每行上第一个等号左边的字符串分配给循环变量I.这是环境变量的名称,即此批处理文件的Option1Option2Option3.

使用每个OptionX环境变量名称,都会调用子例程ProcessOption,以用空格字符替换其值中的所有%20.

这是解决环境变量的方法,该环境变量在变量名称中带有一个或多个感叹号.

@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

此批处理代码将名称中以!Option开头的所有环境变量中的%20%%20替换为一个空格字符,而在上述环境代码中,以%20开头的环境变量仅将%20替换为一个空格.名称.

在变量名中使用字符当然是个坏主意,这些变量名对于Windows命令解释器来说具有特殊含义,例如字符&()[]{}^=;!'+,`~|<>%.如上所述,这是可能的,但绝对不是一个好主意.

要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读每个命令显示的所有帮助页面.

  • call /?
  • cls /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • set /?
  • setlocal /?

有关&运算符的说明,另请参见使用Windows批处理文件的单行包含多个命令的答案./p>

并阅读DosTips论坛主题 ECHO.不能输入文本或空白行-而是使用ECHO/,为什么最好使用echo/而不是echo.输出一个空行.

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 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 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, i.e. 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.

这篇关于使用Windows批处理脚本替换字符串中的%% 20的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆