Jump to content

Replacing a string in an ini/txt file


xilace
 Share

Recommended Posts

I have a feeling that i'm simply WAY over complicating this. What i'm trying to do is find a phrase in a txt/ini file, once found replace the entire line with what i want.

Example:

Look for "ResX=" (in this case i dont know what would be after the "ResX="), and replace the entire line with what i want such as "ResX=1024".

I tried doing it with the ini read/write section, but when i write it, it erases all the other keys in the section if i dont declare them as well. I have also tried the filewritetoline, but while i can find the character position in the file using stringinstr, i dont know what line its on to replace it.

This is just same random things i was trying and failing at.

$WillowEngineFile = (@UserProfileDir & "\Documents\My Games\Borderlands 2\WillowGame\Config\WillowEngine.ini")


$OriginaliniFile = FileRead($WillowEngineFile)
$resx = StringInStr($OriginaliniFile, "ResX=")
$resy = StringInStr($OriginaliniFile, "ResY=")
StringReplace($OriginaliniFile, "ResX=", "ResX=1024")
FileWrite($WillowEngineFile, $OriginaliniFile)
Link to comment
Share on other sites

While i would agree I'v gone over them as stated above several times, although when a section has lets say 20 keys and i only want to change 1 or 2, it erases the other 18 and will only show the ones i changed. Is there something i'm missing that allows me to save the other 18 that arent being changed?

Link to comment
Share on other sites

While i would agree I'v gone over them as stated above several times, although when a section has lets say 20 keys and i only want to change 1 or 2, it erases the other 18 and will only show the ones i changed. Is there something i'm missing that allows me to save the other 18 that arent being changed?

IniRead()

StringReplace()

IniWrite()

I didn't read your entire post at first. just use string replace on the old data and re-write it

Link to comment
Share on other sites

Example

- Replaces every ResX= with ResX= 1024 [Leaves rest of the File untouched]

Code

;The File
Local $nFile = @ScriptDir & 'Testing.txt'
;Read the File
Local $sRead = FileRead($nFile)

;The string to append with
Local $sWith = '1024'
;Our Pattern
Local $sPattern = '(?m)(?:^.*)(ResX=)(?:.*$)'
;Replace the Strings and get the result
Local $sReplaced = StringRegExpReplace($sRead, $sPattern, '123 '&$sWith&@CR)

ConsoleWrite($sReplaced)

;Again write it back to the file
Local $hFile = FileOpen($nFile,2)
FileWrite($hFile,$sReplaced)

The TextFile

dqwrvxcvwh
dqeSvnukResX=fweyb
ResX=qe4135ResX=sfggr
Resf=greyh
qqggResX=
Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...