Solved

Batch File to edit text in an .ini file.

Posted on 2004-04-30
22
1,229 Views
Last Modified: 2012-08-14
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
Comment
Question by:brianbailey
  • 11
  • 10
22 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 10962434
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
 

Author Comment

by:brianbailey
ID: 10962524
I'm pretty sure I don't have access to this.  Any other ideas?
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 10962555
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
 

Author Comment

by:brianbailey
ID: 10962619
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
 

Author Comment

by:brianbailey
ID: 10962677
Would it be easier to run a batch file that would change a drive's mapping?
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 10962691
Okay - so when would you need M versus N?
0
 

Author Comment

by:brianbailey
ID: 10962753
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
 
LVL 67

Expert Comment

by:sirbounty
ID: 10962765
We can do it either way...
0
 

Author Comment

by:brianbailey
ID: 10962777
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
 
LVL 67

Accepted Solution

by:
sirbounty earned 125 total points
ID: 10963046
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
 

Author Comment

by:brianbailey
ID: 10963756
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 67

Expert Comment

by:sirbounty
ID: 10963776
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
 

Author Comment

by:brianbailey
ID: 10963797
No error.  Screen comes up and disappears, nothing changed.  XP.
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 10963807
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
 

Author Comment

by:brianbailey
ID: 10963854
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
 
LVL 67

Expert Comment

by:sirbounty
ID: 10963913
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
 

Author Comment

by:brianbailey
ID: 10963978
Just for myself right now.  I want to be able to click on it, yes, and have it do its thing.
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 10964904
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
 
LVL 67

Expert Comment

by:sirbounty
ID: 10977441
brianbailey - this work for you?
0
 

Author Comment

by:brianbailey
ID: 10977881
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
 
LVL 67

Expert Comment

by:sirbounty
ID: 10979607
Remove that START NOTEPAD line and it won't come up again...
0
 

Expert Comment

by:bastian74
ID: 11072509
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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now