How to replace double quotes in findstr batch file

萝らか妹 提交于 2020-07-19 18:08:27

问题


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

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" />

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.

Any help would be appreciated.


回答1:


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.

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.

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.

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.

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.

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.

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%"


来源:https://stackoverflow.com/questions/13728577/how-to-replace-double-quotes-in-findstr-batch-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!