提问人:JonF 提问时间:11/14/2023 最后编辑:JonF 更新时间:11/14/2023 访问量:31
替换批处理文件中环境变量中的文本时转义百分比
Escape percent when replacing text in an environment variable in a batch file
问:
在不讨论原因的情况下,我需要一个批处理文件,它从 INI 文件中提取一组部分名称。幸运的是,我想要的所有部分名称都以相同的字符串开头,所以小菜一碟,对吧?问题是部分名称可能包含“%20”。例如,以下 INI 文件:
[Testing\Test%20x]
A=aaa
B=bbb
不幸的是,findstr 将 %20 替换为 0。这是我为我的批处理文件想出的:
@ECHO off
SETLOCAL
FOR /F "delims=" %%A IN ('findstr \[Testing* Test.ini') DO (CALL :DOIT %%A)
ENDLOCAL
GOTO :EXIT
:DOIT
SET First=%1
SET Second=%First:[=%
SET Third=%Second:]=%
SET Fourth=%Third:0=20%
SET Fifth=%Fourth:20=%%20%
ECHO %1 %First% %Second% %Third% %Fourth% %Fifth%
EXIT /B
:EXIT
这将产生输出:
[Testing\Test0x] [Testing\Test0x] Testing\Test0x] Testing\Test0x Testing\Test20x Testing\Testx0
使用反斜杠或插入符号转义百分比不起作用。如何将百分号放入字符串中?
答:
1赞
Magoo
11/14/2023
#1
@ECHO OFF
SETLOCAL
SET "sourcedir=u:\your files"
SET "filename1=%sourcedir%\q77476155.txt"
FOR /f "delims=" %%b IN ('findstr \[Testing* "%filename1%"') DO SET "string=%%b"&CALL :showme
GOTO :EOF
:showme
ECHO %string%
GOTO :eof
我使用了一个名为包含您的数据的文件进行测试。q77476155.txt
它不是在进行替换,而是出现在正在替换的子例程的参数中。findstr
%2
现在 - 你想用字符串做什么?
评论
0赞
JonF
11/14/2023
我想提取定义 WinSCP FTP 程序连接的部分,以将它们插入到 WinBuilder 脚本中 WinSCP 的 INI 文件中,以创建一个 ISO,从中启动和排除 Windows 安装故障,尤其是恶意软件。
0赞
JonF
11/14/2023
没有“回复”选项?
0赞
JonF
11/14/2023
啊,现在我明白了。谢谢。
评论