1

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 Answer 1

2
  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%%
    
Sign up to request clarification or add additional context in comments.

8 Comments

Do you have an example with the provided code? I still can't seem to get it to work. Thanks for your help.
@wOxxOmI was able to get it work with this code. However, it's inserting before that line. How do I insert it after it?
I'm not sure what you mean? I also updated the code above.
:WRITENEW set inputfile=game.txt set outputfile=game.temp.txt (for /f usebackq^ delims^=^ eol^= %%a in ("%inputfile%") do ( echo %%a if "%%~a"=="[/Script/MyGame.Mode]" call echo %NewURL% ))>>"%outputfile%"
@wOxxOmI Thanks for your help!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.