如何在findstr批处理文件中替换双引号 [英] How to replace double quotes in findstr batch file

查看:652
本文介绍了如何在findstr批处理文件中替换双引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定我的旧帐户发生了什么,因此必须创建这个新帐户.我写的批处理文件有麻烦.我在用户将输入某些信息的地方工作,然后输入将替换文件中的文本.但是,我遇到的问题是我似乎无法在此处找到确切的答案或无法帮助我的问题是...如何替换引号中的数据?我试图转义引号,但我不确定是否找到它,只是不替换它或什么.这是部分

I'm not sure what happened to my old account so had to create this new one. I am having trouble with a batch file I wrote. I have it working where the user will input certain information and then the input will replace text within a file. However, the issue I am having that I couldn't seem to find an answer to exactly on here or help me out is...how do you replace data within quotes? I tried escaping the quotes and i am not sure if it is finding it and just not replacing it or what. Here is the part

setlocal DisableDelayedExpansion
(
  for /F "usebackq delims=" %%a in ("%drive%:\bdi\%bdi1%\importer.config") do (
    set "str=%%a"
    setlocal EnableDelayedExpansion
    set "str=!str:""saveTcpMessage"" value=""True""="saveTcpMessage" value="False"!"

    echo(!str!
    endlocal
  )
) > %drive%:\bdi\%bdi1%\newfile.txt
%drive%:
cd "%drive%:\bdi\%bdi1%"
del importer.config
rename newfile.txt importer.config
pause

在上面的示例中,我希望批处理文件在名为importer.config的文件中找到以下字符串.

In the above example I want the batch file to find the following string in a file called importer.config

<add key="saveTcpMessage" value="True" />

如果找到上述值,我希望将其替换为:

If it find the above value I want it to replace it with:

<add key="saveTcpMessage" value="False" />

我非常确定find部分可以正常运行,因为我看到您必须转义dbl引号..但是replace部分不起作用.我也尝试在替换中使用双引号,但无济于事.

I am pretty sure the find part works OK as I see you have to escape the dbl quotes..but the replace part isnt working. I have tried using double quotes in the replace too but with no avail.

任何帮助将不胜感激.

推荐答案

cmd.exe解析器不会转义这样的引号.为了转义引号,您必须使用^",但这意味着所有引号以及所有特殊字符(例如< &等)都必须转义.一旦解析器看到未转义的引号,则引号的语义就在其中.播放直到遇到下一个报价.无法逃避引用的引号.

The cmd.exe parser does not escape quotes like that. In order to escape a quote you must use ^", but that means all quotes must be escaped, as well as all special characters like < & etc. Once the parser sees an unescaped quote then the quote semantics are in play until the next quote is encountered. It is impossible to escape a quoted quote.

但是,上述所有内容对您而言并不重要:您的整个方法无法正常工作,因为不可能在变量扩展搜索和替换的搜索部分中包含=.它是困扰批量编程的许多陷阱" 之一.

But all the above doesn't really matter in your case: Your entire approach cannot work because it is impossible to include = in the search part of variable expansion search and replace. It is one of the many little "gottchas" that plague batch programming.

如果可以保证与
<add key="saveTcpMessage" value="True" />元素相同的行上不存在其他XML元素,则只需验证您具有正确的行,然后将"False"替换为"True"即可.但是,您很有可能无法保证这一点. XML没有固有的行结构,因此可以将数据重新格式化为一条连续的行,并且它仍然是具有相同含义的有效XML.或者,该元素可以分为多行.

If you can guarantee that no other XML elements exist on the same line as your
<add key="saveTcpMessage" value="True" /> element, then you could simply verify that you have the correct line and then substitute "False" for "True". But more than likely you cannot make that guarantee. XML has no inherent line structure, so the data could be reformatted as one continuous line and it would still be valid XML with the same meaning. Or the element could be split across multiple lines.

批处理文件通常不能很好地处理文本文件,尤其是在处理XML方面很糟糕.您可以执行此操作,但是这将花费很多奥秘代码,而这些奥秘代码将很慢.除非您非常聪明,否则任何批处理文件解决方案都可能会很脆弱,这意味着有人可以重新格式化XML输入并破坏您的代码.

Batch files are generally not very good at processing text files in general, and in particular they are lousy at working with XML. You can do it, but it will take a lot of arcane code that will be slow. Unless you are extremely clever, any batch file solution will probably be brittle, meaning someone could reformat the XML input and break your code.

我建议您使用其他语言或实用程序-理想情况下,旨在与XML一起使用的实用程序.但是VBScript,JScript或PowerShell也可以工作.这些语言至少可以使您轻松地进行文字搜索并替换您要尝试的文字.

I recommend you use some other language or utility - ideally a utility that is designed to work with XML. But VBScript, JScript or PowerShell could also work. Those languages at least would allow you to easily do the literal search and replace that you are attempting.

如果您真的要使用批处理脚本,我已经发布了在DosTips上搜索最终"文件并替换批处理实用程序.代码中嵌入了有关如何使用脚本的文档.

If you really want to use a batch script, I've posted The "ultimate" file search and replace batch utility over on DosTips. Documentation on how to use the script is embedded within the code.

另一种选择是混合JScript/批处理解决方案:正则表达式搜索并替换为批处理-轻松编辑文件!,再次发布在DosTips上.完整的文档也嵌入在该代码中.

Another option is a hybrid JScript/Batch solution: regex search and replace for batch - Easily edit files!, again posted on DosTips. Full documentation is embedded within that code as well.

假设您创建在第二个链接处找到的REPL.BAT脚本,则可以执行以下操作:

Assuming you create the REPL.BAT script found at the 2nd link, you could do the following:

@echo off
setlocal

:: define drive and bdi1 as needed

set "file=%drive%:\bdi\%bdi1%\importer.config"
set "search=<add key="saveTcpMessage" value="True" />"
set "replace=<add key="saveTcpMessage" value="False" />"

type "%file% | repl.bat search replace el >"%file%.new"
move /y "%file%.new" "%file%"

这篇关于如何在findstr批处理文件中替换双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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