I have seen this question asked but never with a suitable answer for what I need I work doing remote tech support on computer systems and one of the things I frequently need to do is open ini files and replace certain lines with a new one as an example we will say. program.ini program status = client Well I would need to change that to server so it reads program status = server While yes I can open it in notepad I want to be able to do this via command line in a batch file as I already have a batch tool I have created for everything else I do. Now, note that not every computer supports powershell scripts so this has to be just in the one batch file. I can not download any additional software as these are not my systems I am connecting to. Anyone know how this would be done purely via batch as I am at a loss. Not looking to open an editor via command line, just a straight up run it and that line is changed in the ini file. Thanks for any input -Terra
asked Dec 21, 2018 at 16:14 Terra Envy Terra Envy 11 1 1 gold badge 1 1 silver badge 1 1 bronze badge Do the edited lines need to stay in order? Can we assume VBScript and cscript? Commented Dec 21, 2018 at 16:49Yes, it is an ini with about 100+ Lines and has to stay in order. And as stated, it can not include any extra files, installations, or downloads. Looking for a way in pure command line commands in a batch script. I am much more used to linux bash and still learning this stuff for windows. So correct me if I am wrong, but wouldn't a VBScript or cScript require additional files?
Commented Dec 21, 2018 at 17:021) When you add more information to answer a Comment, please click edit and add it to your original question, so the question contains all the information needed to explain the issue. Then, delete the Comment by clicking on the grey (S) at its end, for Comments can pile up and when they do, some Comments get hidden.
Commented Dec 21, 2018 at 19:42There is no native search text and replace commend in Windows. 2) For PCs w/ PowerShell, you can script w/ a command like cat program.ini | % -replace "program status = client","program status = server"> (replacing w/ a real underscore character.
Commented Dec 21, 2018 at 19:503) For PCs w/ no PowerShell, suggest adding Open Source sed & its dependencies gnuwin32.sourceforge.net/packages/sed.htm to a temp directory (thereby avoiding any installation or change to the Registry), run a sed script in that directory for the task(s), then purge it & its dependencies as the last thing you do. 4) An alternative is Open Source FreeDOS edlin en.wikipedia.org/wiki/Edlin#Scripts It runs in Windows 10 x64 & x32 and earlier Windows versions. It allows simple search text and replace scripting. sourceforge.net/projects/freedos-edlin/files/latest/download
Commented Dec 21, 2018 at 19:57This batch file includes some diagnostic information, too. The setlocal line is REQUIRED in order to properly play with variables in a loop.
The most significant side-effects of using this method are that blank lines are removed (they're ignored by FOR) and that your tests (in the "write out the new value" section) are very specific: spaces and case matter. You can perform case insensitive matching with /i, but since you're including the spaces in your question I suspect you prefer to include them. You'll need to include multiple lines as done in that section to replicate each possible comparison.
@ECHO OFF setlocal enableDelayedExpansion FOR /F "tokens=1,* delims==" %%i IN (test.ini) DO ( SET sdone=0 SET "sname=%%i" SET "svalue=%%j" ECHO.Name: !sname! ECHO.Value: !svalue! :: write out headers IF "!sdone!"=="0" IF "!sname:~0,1!"=="[" SET sdone=1&&ECHO.Type: Header&&ECHO.!sname!>>new.ini :: write out the new value if it's client IF "!sdone!"=="0" IF "!sname!"=="program status" IF "!svalue!"=="client" SET sdone=1&&ECHO.Type: Rewrite&&ECHO.!sname!=server>>new.ini IF "!sdone!"=="0" IF "!sname!"=="program status " IF "!svalue!"==" client" SET sdone=1&&ECHO.Type: Rewrite&&ECHO.!sname!=server>>new.ini :: write out anything else IF "!sdone!"=="0" SET sdone=1&&ECHO.Type: Content&&ECHO.!sname!=!svalue!>>new.ini :: a little padding to read the debug info ECHO. )
At the end you'll want to add something to overwrite the original file, something like:
copy /y new.ini test.ini