Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
hi,
is there any way to find a specific line in a txt/ini/inf file, alter that line from the batch file and save the edited file?
example:
the file blabla.ini contains this line somewhere:
DefaultFOV=90.000000I want to locate that line, and change it to
DefaultFOV=110.00000when the batch file terminates, blabla.ini has to be saved, containing that new information!
if it cannot be saved, maybe it's possible to copy blabla.ini's content up until that specific line into a new file, then add the new line, then copy the remaining content from blabla.ini, then delete blabla.ini and rename the newly made file to blabla.ini?

if you can download gawk from here:http://gnuwin32.sourceforge.net/packages/gawk.htm
BEGIN{FS="="}
$1 ~ /DefaultFOV/{
$2=110.00000
}{print}
save the above as script.awk and on command line
c:\test> gawk -f script.awk file

@echo off > newfile
setLocal EnableDelayedExpansionfor /f "tokens=* delims= " %%a in (myfile) do (
echo %%a | find "DefaultFOV=90.000000" > nul
if !errorlevel! equ 0 (
echo DefaultFOV=110.00000 >> newfile
) else (
echo %%a >> newfile
)
)
=====================================
If at first you don't succeed, you're about average.M2

@mech, why don't you find the pattern in the file instead.?
for .... in (find "DefaultFOV=90.000000" file)
do (
....
)

gd,
you lost me
=====================================
If at first you don't succeed, you're about average.M2

@mech, in your example, you are going through the file and then piping each line to find. If the file has 1 million lines, your example will execute find 1 million times. What i am proposing is using find itself to iterate the file, because the find command takes in a file as input and it would be faster.

thx guys!
@mech:
it works!@gd:
haven't tested gawk yet, the idea is to be able to run this script from an imagefile/cd/dvd, so it can't depend on any non-standard software
so is it possible to speed up the script like ghostdog suggested?

you just have to bring gawk.exe into your DVD. Its a standalone program very small in size and no installation is needed.

ghostdog is right about FIND slowing things down. This version avoids FIND.
::==========================
@echo off > newfile
setLocal EnableDelayedExpansionfor /f "tokens=* delims= " %%a in (myfile) do (
if "%%a"=="DefaultFOV=90.000000" (
echo DefaultFOV=110.00000 >> newfile
) else (
echo %%a >> newfile
)
)
=====================================
If at first you don't succeed, you're about average.M2

wow ok, that definately speeds the script up A LOT!
I'm gonna go with Mech's script, that way I don't have to add anything else^^
@gd:
thx for suggesting Gawk anyway dude, I'm sure it'll come in handy!

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |