Link to home
Start Free TrialLog in
Avatar of brianbailey
brianbailey

asked on

Batch File to edit text in an .ini file.

I know that there is probably an easy solution for this out there, but...

I have a file, we'll call it data.ini.  The 6th line in this file is: Drive1=D:.  This references my cdrom drive.  However, I want to find an easy way to chage this to either M: or N:, and I figured creating a batch file would be a good way to do it.  However, I can't seem to find a way to put commands in a batch file to edit text within a file.

I am a batch file novice, so please be gentle.
Avatar of sirbounty
sirbounty
Flag of United States of America image

See this PAQ: http:Q_20448343.html
If you have access to the NT resource kit - there's a utility called munge that will make this extremely simple! :D
Avatar of brianbailey
brianbailey

ASKER

I'm pretty sure I don't have access to this.  Any other ideas?
Sure, I can write a batch script for you.
Can you tell me the exact line you're looking for and under what conditions you'd want to change it and to what?
For instance, we can make a batch file called
changemydrive.bat
and when you want to alter that line type

 changemydrive D M

and it would do that for you..
Okay...basically it is a proprietary software program that accesses data from a cd.  I have discovered that if I copy a cd to a network drive, I can change the Drive1= to the mapped drive and it will treat it as if it is a cd rom.  Instead of swapping database cds in my D drive, I want to copy the cds to the network drive and give them static mapping.  I don't know what you mean by "exact line".  All I know is that the 6th line of the ini file is the line that would need to change to the indicated drive...from Drive1=D: to Drive1=M: or Drive1=N:, etc.
Would it be easier to run a batch file that would change a drive's mapping?
Okay - so when would you need M versus N?
When I neet it varies.  One moment I might need M, the next N, the next D.  I would pretty much have to indicate it somehow by specific batch files (one that changes to M, one to N, one to D).

Okay...say I indicated Drive1=M, is there a batch file I could write that would change the mapping of Drive M:?  That way I could just run a single batch file for each change.  Like, I could run DISK1.bat and it would change the mapping to one network location, or DISK2.bat to change to another.  That way, the text in the ini file would never need to change, just the mapping.
We can do it either way...
since accessing my cdrom over a drive mapping may slow things down, I would prefer that the drive letter changes in the INI file.
ASKER CERTIFIED SOLUTION
Avatar of sirbounty
sirbounty
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Didn't work.  I really don't have time to go more in depth with this.  I thought there was an easy way to do it.  You can have the points.
brianbailey - what do you mean?
I'd tested it here before I posted it and it worked for me.
Did you get an error?
What operating system are you using?
No error.  Screen comes up and disappears, nothing changed.  XP.
How did you run it?  This should really be easy enough to resolve (I'm also on XP).
The batch file must exist in the same folder as the data.ini file, or else I can hard code the folder, if needed...
okay...the file is called onesrc.ini and the folder it is in is C:/windows.  I changed all instances in the above code to onesrc.ini.  i wouldn't want it to be in the windows directory.
Okay, so if you need to use it - would you want it to be an icon to click on, or shelling out to a dos prompt okay?
How would this be run?  On each user's workstation as needed?
Just for myself right now.  I want to be able to click on it, yes, and have it do its thing.
Sorry about the delay.  I had three servers crash. :(
Anyway - here's the revised script.
You can click on the icon (or perform a start\run\changedrive.bat - if it's located in C:\windows\system32)
This assumes:
 -onesrc.ini will always be in %systemroot% (Typically C:\windows, but this will work if it's C:\winnt, etc)
 -that the user will know to enter only the drive letter needed.  In other words, the script runs and prompts for the drive letter needed.  The response should be in the form of the letter only.  I've placed no error checking for any invalid input here (So, M would be valid, M: would not)
 -that Drive1= will always be line 6...
 -that you want no backup copy made of the original file.

I have no ECHO commands to prevent this from overwriting your existing INI file, so I'd recommend a backup until you've tested it on your end (works on mine).

-------ChangeDrive.bat------------
@echo off
set src=%systemroot%\onesrc.ini
set dest=%systemroot%\databld.ini
echo.Current configuration:
type %src%|find /i "Drive1="
echo.
set /p drv=Which drive letter would you like to change it to?
for /f "delims=" %%a in (%src%) do call :process "%%a"
type %dest%|find /i "Drive1=%drv%:"
if %errorlevel%==0 goto cr8Ini
cls
Echo. An error has occurred.
Echo.
goto clrVar

:cr8Ini
erase %src%
ren %dest% onesrc.ini
goto clrVar

:drvChg
echo Drive1=%drv%:>>%dest%
goto :eof

:clrVar
set drv=
set dest=
::This next line can be removed - it loads the new version for your review
START NOTEPAD %src%
set src=
set ctr=
goto :eof

:process
set /a ctr=%ctr%+1
if %ctr%==6 goto drvChg
for /f "delims=" %%x in (%1) do echo %%~x>>%dest%
--------------------------
brianbailey - this work for you?
It worked!  Thank you!  It changes the drive and then it opens the onesrc.ini file, but I just go ahead and close it.  Thank you Thank you Thank you.
Remove that START NOTEPAD line and it won't come up again...
Here is a batch file I wrote to arbitrarily change any value in a ini file.  This should work so long as none of the values of the ini file have an equals sign in it. (Apart from the one that is supposed to be there)
This will only work on win2k / xp since it uses set /p
You could assign the variables in the batch file and it should work on NT
When you specify the Section dont leave out the brackets [Group]
As it is now, it is only echoing the ini file to the screen.  Simply add >> myfile.txt for it to be piped into a new file.  It should not be hard to modify this batch file to suit your needs.

@echo off
set src=C:mypath\my.ini
set /p section=Section:
set /p item=Item:
set /p value=Value:
echo.
echo --------------------------------------------------
echo.

for /f "tokens=1-3 delims==" %%a in (%src%) do (
  if "%%b"=="" echo y>flag.txt
  if "%%b"=="" del flag.txt
  if "%%b"=="" if "%%a"=="%section%" echo y>flag.txt
  if not "%%b"=="" (
    if exist flag.txt if "%%a"=="%item%" echo %%a=%value%
    if exist flag.txt if not "%%a"=="%item%" echo %%a=%%b
    if not exist flag.txt if "%%a"=="%item%" echo %%a=%%b
    if not exist flag.txt if not "%%a"=="%item%" echo %%a=%%b
  )
  if "%%b"=="" echo %%a
)