在批处理文件中不断删除感叹号 [英] exclamation point being removed constantly in batch file

查看:25
本文介绍了在批处理文件中不断删除感叹号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Someone please help...oh, I will be so grateful. I have a very long batch file that is working perfectly except that every time the user enters input, it replaces strings in the files desired BUT it is removing the !s in the files causing issues due to them being XML config files and these are commented out sections that need to stay. I wont put the whole code in here unless requested, but in a nut shell, the user makes certain inputs and then the batch file runs...here is the part of the code for one file....the user enters the drive letter of the install and the name of the bdi server. I want the user input to replace %drive% and %bdi1%....which it does....but I do NOT want it to replace commented out sections...ie: :

<!-- 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 的回答进展顺利,但我无法让它发挥作用.提前致谢

here is my code...what do i need to do to get it to stop removing the !. I tried looking on here and i thought i was well on my way with Jeb's answers but i couldnt get it to work. thanks in advance

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

推荐答案

这是延迟扩展和批处理解析器的效果.
如果禁用延迟扩展,则感叹号没有问题,但如果启用它,解析器会假设 ! 用于扩展变量,并且当只有一个标记时,它将被丢弃.

It's an effect of delayed expansion and the batch parser.
If delayed expansion is disabled there aren't problems with exclamation marks, but if it's enabled the parser suppose that the ! are for expanding a variable, and when there is only one mark, it will be dropped.

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

So the solution is to disable delayed expansion, but as you need it, you must enable it too!

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

This can be done with simply toggling it in the right moment.

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 块,它更快,您无需先删除文件.
我尝试将您的所有替换项移到一个块中,因此您只需读取一次文件.

I move the rediretion to the complete FOR-block, it's faster and you don't need to delete the file first.
I try to move all your replacements into one block, so you only need to read the file only once.

这篇关于在批处理文件中不断删除感叹号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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