Batch file to find 'n modify text in ini file

Microsoft Windows xp professional w/serv...
February 17, 2010 at 13:36:16
Specs: Windows XP
Hello, I'm quite 'green' in programming and I need your help. I need a batch file which could find specified text line in .ini file, be able to delete that line and type in the text by your choice.

(menu)

@echo off
echo.
set/p NAME="Please type in desired Steam User Name and press Enter: "
echo.
echo.
set/p NAME="Please type in desired Player Name and press Enter: "

(rev.ini)

need to find these to lines (below) in file, delete PLACEYOURNAMEHERE and replace the text by whatever you type in the menu (above)

SteamUser = PLACEYOURNAMEHERE

PlayerName = PLACEYOURNAMEHERE

Thank you in advance :)
Sorry for my English


See More: Batch file to find n modify text in ini file

Report •

#1
February 17, 2010 at 17:16:25
******* REPOSTED WITH CORRECTIONS *****
@echo off>new.ini & setlocal
for /f "tokens=*" %%a in ('dir /b /ad /s \gamecache') do set gamecache=%%a
if not defined %gamecache% (
echo.
set/p gamecache="Please type in desired path for game files and press Enter: "
echo.
set/p USER="Please type in desired Steam User Name and press Enter: "
echo.
echo.
set/p PLAYER="Please type in desired Player Name and press Enter: "
if not defined %gamecache% goto :eof
if not defined %user% goto :eof
if not defined %player% goto :eof

for /f "tokens=1* delims= " %%a in (rev.ini) do (
set label=%%a
if [%%b] neq [] (set data= %%b) else (set data=)
call :%%a 2>nul
call :output
)
:: safetied on for testing...
more new.ini
echo preparing to OVERWRITE steam.ini, hope you have backup:
pause >nul
move /y new.ini steam.ini
goto :goodbye

:SteamUser
set data= ^= %USER%
goto :eof
:PlayerName
set data= ^= %PLAYER%
goto :eof
:CachePath
set data= ^= %GAMECACHE%
goto :eof

:output
>> new.ini echo %label%%data%
goto :eof

:goodbye
echo goodbye, i'm deleting myself.
del %0.bat


Report •

#2
February 17, 2010 at 23:02:26
Dear nbrane, I really appreceate what you did, unfortunately your compiled script doesn't seem to work.When I try to type in something it creates a new.ini file instead of overwriting the original rev.ini with new changes made within the batch file

I'll try to visually show what I intend to do :)

This is the original rev.ini file from the top.
http://img210.imageshack.us/img210/...

These two circled words needs to be changed with the help of the batch file.
http://img525.imageshack.us/img525/...


Report •

#3
February 17, 2010 at 23:21:32
maybe we don't understand each other.
my script did not change "steam.ini", it only tries to echo
what it wants to do, so you can make sure it works.
:: safetied on for testing...
more new.ini
:: move /y new.ini steam.ini
goto :eof

here is what it does if not "safetied off":
move /y new.ini steam.ini
goto :eof

( i just made up a name: steam.ini, for whatever filename, replace as needed...)
see if that works, if not, hopefully other wiser heads can help in this issue!
good luck.


Report •

Related Solutions

#4
February 18, 2010 at 04:23:43
I replaced the names as needed and replacement of these two specified lines seem to work fine, but when the batch closes it adds bits of text (and those two needed lines) at the bottom of the rev.ini file

http://img38.imageshack.us/img38/38...


Report •

#5
February 18, 2010 at 04:28:52
Here's my attempt:

@echo off
setlocal enabledelayedexpansion

set input=test.txt
set output=out.txt
set text2searchfor=two, this is line 2

del /y %output%
for /F "tokens=*" %%f in ('type %input%') do (
set line=%%f
if "!line!"=="%text2searchfor%" (
set/p NAME=Please type in desired Steam User Name and press Enter:
echo !NAME!>> %output%
) else (
echo !line!>> %output%
)
)

move /Y %output% %input%
type %input%


Report •

#6
February 18, 2010 at 07:38:21
Your attempt didn't seem to change anything ;(( It didn't even popped the menu.I don't know wthat else to do....please help.

Report •

#7
February 18, 2010 at 08:10:51
i don't know why it is doing that, maybe if you post the contents of the scraps coming at end of the file.
(The image file doesn't tell me much and i can't scroll down)
just post the text that's occurring after the last line that should occur.


Report •

#8
February 18, 2010 at 10:19:22
Hey nbrane, I have a better idea. I've uploaded these files so you could take a good look what happened.

This is the original rev.ini file

http://www.mediafire.com/?gmwzjjytrmg

This is the compiled rev.ini file (I typed mmm for both commands) As you can see new text bits have been added where the original one ends, but instead of replacing PLACEYOURNAMEHERE lines with mmm it adds everything at the very end of the original rev.ini including
those bits.

http://www.mediafire.com/?uk5tny422y2

This is the batch file of yours i've modified

http://www.mediafire.com/?yhzyjq2tnkj

I hope you'll sort something out ;)


Report •

#9
February 18, 2010 at 14:53:14
hey, thanks! that saved us BOTH a lot of time and frustration.
see response #1: I re-posted it with all corrections made (rather than waste space posting again here...)
You prob'ly noticed blank lines are not re-iterated...


I also learned a strange and confusing attribute of IF statements, which i consider to be a true "bug".
Usually, you can substitute [], @, or any other non-special
char. to enclose a string, but not in a called subroutine. Then you HAVE to use dbl quotes.
the following will NOT work:

for /L %%a in (1 1 3) do (
set xx="testing"
:: this works here
if [%%a] equ [1] echo first loop
call :testif
)

:testif
:: but not here!
if [%xx%] neq [] echo not null

but this will work:
:testif
if "%xx%" neq "" echo not null

If any other forum-phantoms see this and know why, i would greatly appreciate some enlightenment.


Report •

#10
February 20, 2010 at 04:38:42
Wow! thank you very much, nbrane. Now everything works just fine. I just have couple more requests.

Request #1

Is it possible to add another command to this script?
It supposed to read for folder named gamecache and write the path to rev.ini's string CachePath = (this is where the path of gamecache folder should be written)

Request #2

Add a script which would delete batch file itself After the completion of the processes.

This is my final modified version of your script

@echo off>temp.ini & setlocal
echo.
echo.
echo.
set/p USER="Please type in Steam Account Name of your choice and press Enter: "
echo.
echo.
echo.
set/p PLAYER="Please type in Player Name of your choice and press Enter: "
for /f "tokens=1* delims= " %%a in (rev.ini) do (
set label=%%a
if [%%b] neq [] (set data= %%b) else (set data=)
call :%%a 2>nul
call :output
)
move /y temp.ini rev.ini

:SteamUser
set data= ^= %USER%
goto :eof
:PlayerName
set data= ^= %PLAYER%
goto :eof

:output
>> temp.ini echo %label%%data%
goto :eof


Report •

#11
February 20, 2010 at 12:27:22
see: revised post #1

Report •

Ask Question