BATCH - 查找文本文件中字符串,该行后添加一个新的字符串 [英] BATCH - Find string in text file and add a new string after that line

查看:199
本文介绍了BATCH - 查找文本文件中字符串,该行后添加一个新的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图寻找一个文本文件从一个bat文件一个特定的字符串。如果字符串存在,则在下一行后它添加一个新的字符串。我似乎无法得到正确以下工作code。任何想法?

这是我在寻找我的文本文件中的字符串。 [/脚本/ MyGame.Mode]

下面就是该文本文件的样子。

  [/脚本/ Config.Mode]
东西在这里1
这里的东西2[/Script/MyGame.Mode]
东西在这里1
这里的东西2[/Script/Edit.Mode]
东西在这里1
这里的东西2

这就是我想要的样子。

  [/脚本/ Config.Mode]
东西在这里1
这里的东西2[/Script/MyGame.Mode]
RedirectReferences=(PackageName=\"%Package%\",PackageURLProtocol=\"%PackageURLProtocol%\",PackageURL=\"%WebAddress%/%Package%%Ext%\",PackageChecksum=\"\")
东西在这里1
这里的东西2[/Script/Edit.Mode]
东西在这里1
这里的东西2

这里的code我有这么远。<​​/ P>

 关闭@echo:获取信息
回声。
回声。
CLS
回声。
回声让我们为您配置的一些信息。
回声注意:您在下面输入的信息是区分大小写的。您可以复制和粘贴。
回声。
回声这里的包名的例子:DM-MyTest的-WindowsNoEditor
回声。
集/ P封装=输入包的名称:
回声。
回声。
回声。
回声包URL协议将HTTP或https
回声。
集/ p PackageURLProtocol =输入包装URL协议:
回声。
回声。
回声。
呼应这里有一个WebAddress例如:www.myredirect.com/test(不要加尾随/)
集/ p WebAddress =输入重定向(WebAddress)网址:
回声。
回声。
回声。
回声的文件扩展名通常是.pak
回声。
设置/对外部=输入地图文件扩展名:
回声。
CLS
回声。
回声请稍候...目前创建测试参考。:SHOWLINE
回声。
组NewURL=RedirectReferences=(PackageName=\"%Package%\",PackageURLProtocol=\"%PackageURLProtocol%\",PackageURL=\"%WebAddress%/%Package%%Ext%\",PackageChecksum=\"\"^^)暂停:WRITENEW
设置inputfile中= game.txt中
设置OUTPUTFILE = game.temp.txt
(FOR / F有usebackq ^ ^ delims = ^ ^停产= %%一中(%inputfile中%)做(
   如果%%〜一个==[/脚本/ MyGame.Mode]叫回声%NEWURL%
   回声%%一
))&GT;&gt;中OUTPUTFILE%%回声。
暂停


解决方案

  1. 当我在命令提示符控制台运行贴code我看到了一个语法错误:


      

    ),在这个时候意外。


    显然,在 NEWURL 破东西括号时,在环扩大。


    • 一个简单的解决办法是通过使用呼叫延迟扩展招:

       调用回声%% NEWURL %%


    • 或者

        SETLOCAL enableDelayedExpansion&安培;回音!NEWURL! &安培; ENDLOCAL


    • 或者双转义 ^ ^ (一次性右括号为设置,另一个用于内循环扩展值):

       设置NEWURL = ............. PackageChecksum =^^)



  2. 另一个问题是,输出文件名相同输入文件名,但它不可能输出重定向到同一个文件你正在阅读。

    输出名称更改为不同的文件。然后循环结束后,替换原来的:

     设置inputfile中= game.txt中
    设置OUTPUTFILE = game.temp.txt
    ...................
    ))&GT;&gt;中OUTPUTFILE%%
    移动/ YOUTPUTFILE%%%inputfile中%


  3. 和改变新的字符串的顺序找到的行之后打印它简单地交换两个线内环内:

     回声%%一
    如果%%〜一个==[/脚本/ MyGame.Mode]呼回声%% NEWURL %%


I'm trying to search a text file for a particular string from a bat file. If the string exist, add a new string after it on the next line. I can't seem to get the code below working correctly. Any Ideas?

This is the string i'm searching for in my text file. [/Script/MyGame.Mode]

Here's what the text file looks like.

[/Script/Config.Mode]
Something here 1
Something here 2

[/Script/MyGame.Mode]
Something here 1
Something here 2

[/Script/Edit.Mode]
Something here 1
Something here 2

And here's how I want it to look.

[/Script/Config.Mode]
Something here 1
Something here 2

[/Script/MyGame.Mode]
RedirectReferences=(PackageName="%Package%",PackageURLProtocol="%PackageURLProtocol%",PackageURL="%WebAddress%/%Package%%Ext%",PackageChecksum="")
Something here 1
Something here 2

[/Script/Edit.Mode]
Something here 1
Something here 2

Here's the code I have so far.

@echo off

:GETINFO
echo.
echo.
cls
echo.
echo Let's get some information for your config. 
echo Note: The information you enter below is case sensitive. You can copy and paste.
echo.
echo Here's a Package Name example: "DM-MyTest-WindowsNoEditor"
echo.
set /p Package=Enter Package Name:
echo.
echo.
echo.
echo The Package URL Protocol will be "http" or "https"
echo.
set /p PackageURLProtocol=Enter Package URL Protocol:
echo.
echo.
echo.
echo Here's a WebAddress example: "www.myredirect.com/test" (Don't add the trailing /)
set /p WebAddress=Enter Redirect(WebAddress)URL:
echo.
echo.
echo.
echo The file extention is usually ".pak"
echo.
set /p Ext=Enter Map File Extention:
echo.
cls
echo.
echo Please wait... Currently Creating Test References.

:SHOWLINE
echo.
set NewURL=RedirectReferences=(PackageName="%Package%",PackageURLProtocol="%PackageURLProtocol%",PackageURL="%WebAddress%/%Package%%Ext%",PackageChecksum=""^^)

pause

:WRITENEW
set inputfile=game.txt
set outputfile=game.temp.txt
(for /f usebackq^ delims^=^ eol^=  %%a in ("%inputfile%") do (
   if "%%~a"=="[/Script/MyGame.Mode]" call echo %NewURL%
   echo %%a 
))>>"%outputfile%"

echo.
pause

解决方案

  1. When I run the posted code in Command Prompt console I see a syntax error:

    ) was unexpected at this time.

    Apparently the parentheses inside NewURL break things when expanded in the loop.

    • A straightforward solution would be to delay the expansion by using the call trick:

      call echo %%NewURL%%
      

    • Alternatively:

      setlocal enableDelayedExpansion & echo !NewURL! & endlocal
      

    • Or double-escape the closing parenthesis with ^^ (one time for set and another for an expanded value inside the loop):

      set NewURL=.............PackageChecksum=""^^)
      

  2. Another issue is that the output file name is the same as the input file name but it's impossible to redirect output into the same file as you're reading.

    Change the output name to a different file. Then replace the original after the loop is finished:

    set inputfile=game.txt
    set outputfile=game.temp.txt
    ...................
    ))>>"%outputfile%"
    move/y "%outputfile%" "%inputfile%"
    

  3. And to change the order of the new string to print it after the found line simply swap the two lines inside the inner loop:

    echo %%a 
    if "%%~a"=="[/Script/MyGame.Mode]" call echo %%NewURL%%
    

这篇关于BATCH - 查找文本文件中字符串,该行后添加一个新的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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