# [SOLVED] Batch file to edit contents of .ini file



## joker56123

Hello. I have to upgrade some software and I want to edit a line of text in an .ini file right before the upgrade. Here is the file name sungard62.ini and here are the contents...

[GMS]
odbcDSN=QGMGTMS_62

just need to change the odbcDSN=NewDSN

Thanks!


----------



## TheOutcaste

*Re: Batch file to edit contents of .ini file*

Welcome to TSF!

This is my generic "change a line of text" for .inf and .ini files
It preserves the original file by renaming it with a .old extension
It will remove all blank lines, but will insert a blank line before any line that starts with [ (unless it's the first line in the file)
I get the length of the specified search line in case the old line in the file has trailing spaces. If more than one line starts with the old line text, it will be changed as well.
Example, if the file has these lines:
*test=line1
test=line1 again*
and you set *_OldLine* to *test=line1*, both lines will be changed.
If that might be a problem, change this line:
*If /I "!_tmp:~0,%_Len%!"=="%_OldLine%" (*
to this:
*If /I "!_tmp!"=="%_OldLine%" (*
Just keep in mind that with this, if the old line in the file has trailing spaces, it won't be changed unless you include them in the *_OldLine* variable


Code:


@Echo Off
SetLocal EnableDelayedExpansion
:: Edit the following three lines as needed.
:: Specifiy the full path to the file, or the current directory will be used
Set _PathtoFile=C:\Test1\Sungard62.ini
Set _OldLine=odbcDSN=QGMGTMS_62
Set _NewLine=odbcDSN=NewDSN
:: End of Search parameters
Call :_Parse "%_PathtoFile%"
Set _Len=0
Set _Str=%_OldLine%
Set _Str=%_Str:"=.%987654321
:_Loop
If NOT "%_Str:~18%"=="" Set _Str=%_Str:~9%& Set /A _Len+=9& Goto _Loop
Set _Num=%_Str:~9,1%
Set /A _Len=_Len+_Num
PushD %_FilePath%
If Exist %_FileName%.new Del %_FileName%.new
If Exist %_FileName%.old Del %_FileName%.old
Set _LineNo=0
For /F "Tokens=* Eol=" %%I In (%_FileName%%_FileExt%) Do (
Set _tmp=%%I
Set /A _LineNo+=1
If /I "!_tmp:~0,%_Len%!"=="%_OldLine%" (
>>%_FileName%.new Echo %_NewLine%
) Else (
If !_LineNo! GTR 1 If "!_tmp:~0,1!"=="[" Echo.>>%_FileName%.new
SetLocal DisableDelayedExpansion
>>%_FileName%.new Echo %%I
EndLocal
))
Ren %_FileName%%_FileExt% %_FileName%.old
Ren %_FileName%.new %_FileName%.ini
PopD
Goto :EOF
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Subroutines
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:_Parse
Set _FilePath=%~dp1
Set _FileName=%~n1
Set _FileExt=%~x1
Goto :EOF

Jerry


----------



## joker56123

*Re: Batch file to edit contents of .ini file*

Seemed to work perfect! Thanks for your help Jerry.


----------



## TheOutcaste

*Re: Batch file to edit contents of .ini file*

You're Welcome!

If your issue has been resolved you can mark this thread Solved by using the Thread Tools at the Top Right of this thread (above the first post) :grin:

Jerry


----------



## joker56123

*Re: Batch file to edit contents of .ini file*

Yes, It is solved thanks. I went to Thread Tools and there is no option to mark this thread Solved. Any ideas??


----------



## TheOutcaste

*Re: Batch file to edit contents of .ini file*

Odd, when you click on Thread Tools it should have an option for Mark this thread as Solved.
Sometimes it seems to go on vacation though, so don't worry about it.


----------



## Rinzwind

*Re: Batch file to edit contents of .ini file*

To edit or read ini files from batch scripts you can also user IniMe, available for free from http://wizardsoft.nl/content/Software/Tools
Return value of Path in mysettings.ini
inime.exe "%appdata%\mysettings.ini" /nath
Write value of Path in mysettings.ini
inime.exe "%appdata%\mysettings.ini" /create "/s:Section One" /nath /v:NewValue


----------

