.CMD Script to Find & Replace Text in Multiple Fil

2019-08-17 04:20发布

问题:

Programming newbie in a bit of a pickle here. I know next to nothing about .cmd or .bat files or the like, but I'm to understand that .cmd is the way to go for a little project I'm currently undertaking. Basically, I'm looking to distribute a file which, when executed, alters two very specific lines from two individual .ini files located in the same folder as the .cmd file.

For NewGame.ini, I would very much like for the following line to be changed from:

FullHealthLimit = 1000

...to...

FullHealthLimit = 9999

And for NewGameUI.ini, I'd like for the following line to be changed from:

m_PosOffset=(X=25,Y=-99)

...to...

m_PosOffset=(X=-9999,Y=-9999)

That's it. I just need one executable file to perform two -- hopefully -- quite basic tasks. Reason being is that my friends and I are really into modding certain video games, but want to make it much easier to distribute files.

I'll be honest: I've had so much trouble trying to implement solutions from other topics and make it work for my situation (largely because of the presence of an equals sign causing grief) that I'm beginning to wonder if it's even possible. I'm at my wits' end, I tells ya! ;)

If anyone could provide the necessary code for me, ready to be plonked into a text file and saved as a .cmd file or whatnot, then I would be eternally grateful. I just want for me and my little circle of chums to be able to distribute our little mods amongst our humble community.

Thank you so much for reading about and hopefully looking into my quandary! I really appreciate any and all help. Cheers!

回答1:

Here's a bit of code that should do the trick. I haven't tested it though, so you'll have to give it a try and see what it does -- and then report back if there are any anomalies. Just place the Fix.bat file in the same folder with the two INI files and run it from a cmd prompt (from the directory with the files).

Fix.bat

@echo off

for /f "eol= delims=" %%i in (NewGame.ini) do (
  if "%%i"=="FullHealthLimit = 1000" (
    echo FullHealthLimit = 9999>> NewGame2.ini
  ) else (
    echo %%i>> NewGame2.ini
  )
)

echo [Unreal.Game_MaxAmmo]>>NewGame2.ini
echo p_zApplyOffset=true>>NewGame2.ini
echo p_zPosOffset=(X=-500,Y=-333)>>NewGame2.ini

:: Once you know it works, move the '::' from the next line to the first 'ren' line
::del NewGame.ini
ren NewGame.ini OldNewGame.ini
ren NewGame2.ini NewGame.ini

for /f "eol= delims=" %%i in (NewGameUI.ini) do (
  if "%%i"=="m_PosOffset=(X=25,Y=-99)" (
    echo m_PosOffset=^(X=-9999,Y=-9999^)>> NewGameUI2.ini
  ) else (
    echo %%i>> NewGameUI2.ini
  )
)

:: Once you know it works, move the '::' from the next line to the first 'ren' line
::del NewGameUI.ini
ren NewGameUI.ini OldNewGameUI.ini
ren NewGameUI2.ini NewGameUI.ini

You can see that it does not alter your original .ini files in any way, and that it renames them to OldNewGame.ini and OldNewGamUI.ini just to be safe.