字符串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.这是环境变量的名称,即此批处理文件的Option1,Option2和Option3.使用每个OptionX环境变量名称,都会调用子例程ProcessOption,以用空格字符替换其值中的所有%20.这是解决环境变量的方法,该环境变量在变量名称中带有一个或多个感叹号.@echo offsetlocal EnableExtensions DisableDelayedExpansionset "!Option1!=Calculation%%%%20One"set "!Option2!=City%%20Country"set "!Option3!=State%%%%20Country"set "!Option4!=Any other address/string"clsecho The options before processing:echo/set !Optionfor /F "delims==" %%I in ('set !Option 2^>nul') do call :ProcessOption "%%I"echo/echo The options after processing:echo/set !Optionecho/endlocalpausegoto :EOF:ProcessOptioncall set "ModifiedOption=%%%~1%%"setlocal EnableDelayedExpansionset "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 offsetlocal 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 tempFileCan 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 offsetlocal EnableExtensions DisableDelayedExpansionset "Option1=Calculation%%20One"set "Option2=Calculation %%%%20Two!"set "Option3=Any other string"clsecho The options before processing:echo/set Optionfor /F "delims==" %%I in ('set Option 2^>nul') do call :ProcessOption "%%I"echo/echo The options after processing:echo/set Optionecho/endlocalpausegoto :EOF:ProcessOptionsetlocal EnableDelayedExpansionset "ModifiedOption=!%~1:%%20= !"endlocal & set "%~1=%ModifiedOption%"goto :EOFIt 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 offsetlocal EnableExtensions DisableDelayedExpansionset "!Option1!=Calculation%%%%20One"set "!Option2!=City%%20Country"set "!Option3!=State%%%%20Country"set "!Option4!=Any other address/string"clsecho The options before processing:echo/set !Optionfor /F "delims==" %%I in ('set !Option 2^>nul') do call :ProcessOption "%%I"echo/echo The options after processing:echo/set !Optionecho/endlocalpausegoto :EOF:ProcessOptioncall set "ModifiedOption=%%%~1%%"setlocal EnableDelayedExpansionset "ModifiedOption=!ModifiedOption:%%%%20= !"set "ModifiedOption=!ModifiedOption:%%20= !"endlocal & set "ModifiedOption=%ModifiedOption%"set "%~1=%ModifiedOption%"set "ModifiedOption="goto :EOFThis 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-21 00:33