gpt4 book ai didi

file - 在批处理文件中不断删除感叹号

转载 作者:行者123 更新时间:2023-12-02 07:39:10 25 4
gpt4 key购买 nike

有人请帮助...哦,我将不胜感激。我有一个非常长的批处理文件,它运行良好,除了每次用户输入时,它都会替换所需文件中的字符串但是它正在删除文件中的 !s 由于它们是 XML 配置文件而导致问题并且这些被注释出需要留下的部分。除非有要求,否则我不会将整个代码放在这里,但简而言之,用户进行某些输入,然后批处理文件运行……这是一个文件的部分代码……用户进入驱动器安装字母和 bdi 服务器的名称。我希望用户输入替换 %drive% 和 %bdi1%......它确实如此......但我不希望它替换注释掉的部分......即::

<!-- Tcp message preamble and postamble are flags that mark the beginning and end of an HL7 message. turns into <-- Tcp message preamble and postamble are flags that mark the beginning and end of an HL7 message.

注意没有!

这是我的代码...我需要做什么才能让它停止删除 !。我试着在这里查看,我认为我已经很好地了解了 Jeb 的答案,但我无法让它发挥作用。提前致谢

if exist newfile.txt del newfile.txt
for /F "usebackq delims=" %%a in ("%drive%:\mckesson\%bdi1%\importer.config") do (
set str=%%a
set str=!str:server_name=%server%!
echo !str! >> newfile.txt
)
del importer.config
rename newfile.txt importer.config



if exist newfile.txt del newfile.txt
for /F "usebackq delims=" %%a in ("%drive%:\mckesson\%bdi1%\importer.config") do (
set str=%%a
set str=!str:bdi_name=%bdi1%!
echo !str! >> newfile.txt
)
del importer.config
rename newfile.txt importer.config



if exist newfile.txt del newfile.txt
for /F "usebackq delims=" %%a in ("%drive%:\mckesson\%bdi1%\importer.config") do (
set str=%%a
set str=!str:share_name=%share%$!
echo !str! >> newfile.txt
)
del importer.config
rename newfile.txt importer.config


if exist newfile.txt del newfile.txt
for /F "usebackq delims=" %%a in ("%drive%:\mckesson\%bdi1%\importer.config") do (
set str=%%a
set str=!str:drive_bdi=%drive%!
echo !str! >> newfile.txt
)
del importer.config
rename newfile.txt importer.config

最佳答案

这是延迟扩展和批解析器的影响。
如果延迟扩展被禁用,感叹号就没有问题,但如果它被启用,解析器假设 ! 用于扩展一个变量,当只有一个标记时,它将被删除。

所以解决方案是禁用延迟扩展,但如果您需要它,您也必须启用它!

这可以通过在适当的时候简单地切换它来完成。

setlocal DisableDelayedExpansion
(
for /F "usebackq delims=" %%a in ("%drive%:\mckesson\%bdi1%\importer.config") do (
set "str=%%a"
setlocal EnableDelayedExpansion
set "str=!str:server_name=%server%!"
set "str=!str:bdi_name=%bdi1%!"
set "str=!str:share_name=%share%$!"
set "str=!str:drive_bdi=%drive%!"
echo(!str!
endlocal
)
) > newfile.txt

我将重定向移动到完整的 FOR block ,这样速度更快,而且您不需要先删除文件。
我尝试将所有替换移动到一个 block 中,因此您只需要读取文件一次。

关于file - 在批处理文件中不断删除感叹号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13060943/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com