• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1302
  • Last Modified:

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.
0
brianbailey
Asked:
brianbailey
  • 11
  • 10
1 Solution
 
sirbountyCommented:
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
0
 
brianbaileyAuthor Commented:
I'm pretty sure I don't have access to this.  Any other ideas?
0
 
sirbountyCommented:
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..
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
brianbaileyAuthor Commented:
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.
0
 
brianbaileyAuthor Commented:
Would it be easier to run a batch file that would change a drive's mapping?
0
 
sirbountyCommented:
Okay - so when would you need M versus N?
0
 
brianbaileyAuthor Commented:
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.
0
 
sirbountyCommented:
We can do it either way...
0
 
brianbaileyAuthor Commented:
since accessing my cdrom over a drive mapping may slow things down, I would prefer that the drive letter changes in the INI file.
0
 
sirbountyCommented:
Okay...here ya go.
To make this complete - remove the ECHO that is in all caps...it will remove the original and replace it with the 'new' one...

---------changedrive.bat-------
@echo off
set ctr=0
if [%1]==[] goto usage
set drv=%1
for /f "delims=" %%a in (data.ini) do call :process %%a
type databld.ini|find /i "Drive1=%drv%:"
if %errorlevel%==0 goto cr8ini
cls
Echo. An error has occurred.
Echo.
goto usage
:cr8ini
ECHO erase data.ini
ECHO ren databld.ini data.ini
goto :eof
:usage
cls
Echo.  You need to specify the drive letter
Echo.  Example:  CHANGEDRIVE M
Echo.
goto :eof
:chgDrv
echo Drive1=%drv%:>> databld.ini
goto :eof
:process
set /a ctr=%ctr%+1
if %ctr%==6 goto chgDrv
echo %1>>databld.ini
--------------------------
Let me know if any problems with it...
0
 
brianbaileyAuthor Commented:
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.
0
 
sirbountyCommented:
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?
0
 
brianbaileyAuthor Commented:
No error.  Screen comes up and disappears, nothing changed.  XP.
0
 
sirbountyCommented:
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...
0
 
brianbaileyAuthor Commented:
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.
0
 
sirbountyCommented:
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?
0
 
brianbaileyAuthor Commented:
Just for myself right now.  I want to be able to click on it, yes, and have it do its thing.
0
 
sirbountyCommented:
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%
--------------------------
0
 
sirbountyCommented:
brianbailey - this work for you?
0
 
brianbaileyAuthor Commented:
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.
0
 
sirbountyCommented:
Remove that START NOTEPAD line and it won't come up again...
0
 
bastian74Commented:
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
)

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 11
  • 10
Tackle projects and never again get stuck behind a technical roadblock.
Join Now