# Solved: Replace text in a text file



## REALOldNick (Feb 19, 2008)

Ok. I want to alter a line of text in an ini file.

The only trouble is, I don't know what all of the existing text says, because part of it could be changed to anything previously.

filesize=xxx.00

"filesize=" is constant. XXX.00 can change.

I have tried Find to get a line number, but cannot find anything about how to alter a whole line number.

the change.com utility has the same problem. You have to enter the whole string as a search parameter.

I tried an iterative search, but it seemed very slow. The file is about 100 lines long.

Any help greatly appreciated. maybe it's time to start looking at PowerShell, Python etc? Am I straining the batch file?

Nick


----------



## Elvandil (Aug 1, 2003)

You might find this post from the Expert's Exchange useful:

"If you don't mind external utilities, download sed from http://www.cornerstonemag.com/sed/
Exact link http://www.student.northpark.edu/pemente/sed/gsed407x.zip

sed -e "s/Files=40/Files=xx>/ig" %Windir%\System32\Config.NT > %Temp%\Config.NT
del %Windir%\System32\Config.NT
move %Temp%\Config.NT %Windir%\System32\Config.NT

You might want to put all that in a batch file."


----------



## TheOutcaste (Aug 8, 2007)

Not as compact as using *sed*, but can be done with just batch statements.

This will prompt for the new value, rename the current .ini file to .old, make the change, and save the new file.
This will change _*all*_ lines that start with *filesize=*; it does not check that the data to the right of the = is in the form *xxx.00*. If you need to check that (for instance there are other filesize statements) that can be added easily.
Edit the _Filepath and _Filename lines as needed


```
@Echo Off[COLOR=Blue]
Set _Filepath=C:\Test1
Set _Filename=Test[/COLOR]
Set _FileTemp=%temp%\temp.txt
If Exist "%_FileTemp%" Del "%_FileTemp%"
Set /P _NewSize="Enter new size (xxx.00)
For /F "Usebackq Tokens=1* Delims==" %%I In ("%_Filepath%\%_Filename%.ini") Do (
If /I %%I == filesize (
  >>"%_FileTemp%" Echo.%%I=%_NewSize%
  ) Else (
  If "%%J"=="" (
    >>"%_FileTemp%" Echo.%%I
    ) Else (
    >>"%_FileTemp%" Echo.%%I=%%J
  )
))
If Exist "%_Filepath%\%_Filename%.old" Del "%_Filepath%\%_Filename%.old"
Ren "%_Filepath%\%_Filename%.ini" "%_Filename%.old"
Move "%_FileTemp%" %_Filename%.ini
```
HTH

Jerry


----------



## REALOldNick (Feb 19, 2008)

Crikey Jerry! Thanks for that.

I will kick it over and see if it starts.

Thank you for the heads up on the fact that it will look for all instances as well. That's OK in this case, because there is only one.

Nick


----------



## REALOldNick (Feb 19, 2008)

Woah! Sweeet! Beouuuuudiful!

Plugged it in, changed a few things. Made a few mistakes. Got it going.

But boy! The old batch file language is an arcane thing when you start delving.

All of that meant "My hat's off to you my friend!" Smileys don't work....:thumbsup: ^10


----------



## TheOutcaste (Aug 8, 2007)

Thanks, and You're Welcome!:up:

If your issue has been resolved you (and ONLY you) can mark this thread Solved by using the Mark Solved button at the Top Left of this thread (above the first post) 










Jerry


----------



## REALOldNick (Feb 19, 2008)

HaH! I will, because you have..... But the last time I set a solved on a thread, I had more replies than you could believe!


----------

