正在批处理文件不断移出感叹号(exclamation point being removed con

2019-08-01 21:24发布

有人请帮助......哦,我会很感激。 我有一个是每个用户输入的输入时间正常使用,只是一个很长的批处理文件,它取代了所需的文件字符串,但是它在文件中去掉!小号造成由于问题,他们是XML配置文件和这些评论需要留出部分。 我不会把整个代码在这里,除非要求,但在坚果壳,用户进行一定的输入,然后在批处理文件运行......这里是一个文件中的代码的一部分....用户输入驱动器安装的信和BDI服务器的名称。 我希望用户输入更换%驱动器%和%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.

注意到没有!

这里是我的代码...什么我需要做的就是它停止去除! 我试图在这里寻找上,我想我很好在我与杰布的回答方式,但我不能让它开始工作。 提前致谢

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

Answer 1:

这是推迟扩张和批量解析器的效果。
如果延迟扩展是禁用有没有用感叹号的问题,但如果它的启用解析器假设! 对于扩大一个变量,而当只有一个关口,它会被丢弃。

因此,解决办法是禁用延迟扩展,但是当你需要它,你必须也启用了!

这可以通过简单地在正确的时刻翻转它来完成。

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

我谨rediretion到完成块,它的速度更快,你并不需要先删除该文件。
我尝试所有的替换搬进一个街区,所以你只需要读取文件只一次。



文章来源: exclamation point being removed constantly in batch file