.CMD脚本查找&在多个文件中替换文本 [英] .CMD Script to Find & Replace Text in Multiple Files

查看:265
本文介绍了.CMD脚本查找&在多个文件中替换文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里的一个泡菜编程新手。我知道没有什么关于.cmd或.bat文件或类似的,但我要明白.cmd是去一个小项目,我目前正在做的方式。基本上,我想分发一个文件,当执行时,改变两个非常具体的行从位于与.cmd文件相同的文件夹中的两个单独的.ini文件。



对于NewGame.ini,我非常希望将以下行更改为:



FullHealthLimit = 1000



...到...



FullHealthLimit = 9999



对于NewGameUI.ini,我想改变下面的行:



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



...到...



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



就是这样。我只需要一个可执行文件来执行两个 - 希望是相当基本的任务。原因是我的朋友和我真的在改装某些视频游戏,但想要更容易分发文件。



我会诚实: ve有这么多麻烦尝试实现解决方案从其他主题,并使其工作对我的情况(主要是因为存在一个等号造成悲伤),我开始怀疑是否甚至可能。我在我的智慧结束,我告诉ya! ;)



如果任何人可以为我提供必要的代码,准备被插入一个文本文件并保存为.cmd文件或什么,感激。我只想让我和我的小圈子chums能够分发我们的小mods在我们的谦卑的社区。

非常感谢您阅读,并希望看我的困境!我真的很感激任何和所有的帮助。干杯!

解决方案

这里有一些代码应该做的伎俩。我没有测试它,所以你必须尝试看看它做了什么 - 然后报告,如果有任何异常。只需将 Fix.bat 文件放在与两个INI文件相同的文件夹中,并从 cmd 提示符从文件的目录)。



Fix.bat



  @echo off 

for / feol = 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

::一旦你知道它的工作原理,将'::'从下一行移动到第一个'ren'行
:: del NewGame.ini
ren NewGame.ini OldNewGame.ini
ren NewGame2.ini NewGame.ini

for / feol = 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



::一旦你知道它的工作原理,将'::'从下一行移动到第一个'ren'行
:: del NewGameUI.ini
ren NewGameUI.ini OldNewGameUI.ini
ren NewGameUI2.ini NewGameUI.ini

您可以看到它不会以任何方式改变您的原始 .ini 文件,并将它重命名为 OldNewGame .ini OldNewGamUI.ini 只是为了安全。


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!

解决方案

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.

这篇关于.CMD脚本查找&在多个文件中替换文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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