Jump to content

Replace LINE in textfile


Recommended Posts

  • Moderators

Local $hFile = 'MyFile.txt', $aFile = StringSplit(StringStripCR(FileRead($hFile)), @LF), $sHold
For $iCC = 1 To $aFile[0]
    If StringInStr($aFile[$iCC], 'String I am looking for') Then _
            $aFile[$iCC] = 'New Text In Place Of Current Line'
    $sHold &= $aFile[$iCC] & @CRLF
Next
FileClose(FileWrite(FileOpen($hFile, 2), StringTrimRight($sHold, StringLen(@CRLF))))

I wrote this in the Fast Reply box, so be sure to check for errors.

Edit:

Yep, found 1.

Edit2:

Ok, found 2 more, last time I do it in Fast Reply :P

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to post
Share on other sites

HI,

maybe this works also for you.

#include<File.au3>
;#include<Array.au3>

Dim $NewArray = StringReplaceLineInFileBy("c:\Downloads\AutoIt-Skripte\Entwicklung\test.txt", "4r")
;_ArrayDisplay($NewArray, "New")
_FileWriteFromArray("c:\Downloads\AutoIt-Skripte\Entwicklung\test4r.txt", $NewArray, 1)

Func StringReplaceLineInFileBy($file, $search, $replaceLineBy = '')
    Local $aRecords
    If Not _FileReadToArray($file, $aRecords) Then return -1
    For $x = 1 To $aRecords[0]
        If StringInStr($aRecords[$x], $search) <> 0 Then $aRecords[$x] = $replaceLineBy
    Next
    Return $aRecords
EndFunc   ;==>StringReplaceLineInFileBy

You can add _FileWriteFromArray() to write the file back.

Edit with this line e.g.: _FileWriteFromArray("c:\Downloads\AutoIt-Skripte\Entwicklung\test4r.txt", $NewArray, 1)

So long,

Mega

Edited by th.meger

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to post
Share on other sites
  • Moderators

Psst :P ... th.meger (Look at what I wrote... That's _FileReadToArray() just shortened without the need of File.au3 :nuke:

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to post
Share on other sites

Psst :nuke: ... th.meger (Look at what I wrote... That's _FileReadToArray() just shortened without the need of File.au3 :)

Hi SmOke_N,

but i put it into a little Func so it looks cooler. :P

Gary told me to use the include files and no try to reinvent the wheel. :D

So long,

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to post
Share on other sites
  • Moderators

Hi SmOke_N,

but i put it into a little Func so it looks cooler. :P

Gary told me to use the include files and no try to reinvent the wheel. :nuke:

So long,

Mega

I agree with Gary for the novices, but I'm not much for bloated code (yeah right!)... but I like to trim it up where and when I can... And you know I agree with a UDF, I was just giving you a hard time.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to post
Share on other sites

Thnx Guys,

You put me ion the right track. I had solved it with doing this:

$file = IniRead ( $INI_File, "General", "Target", "Bestaadnie")
 $countLine = 1
 While $countLine <= _FileCountLines( $file )
    $Count = 1
    While IniRead ( $INI_File, "DelLine", "Search" & $Count, "Bestaadnie" ) <> "Bestaadnie"
        If StringInStr(FileReadLine ( $file , $countLine ), IniRead ( $INI_File, "DelLine", "Search" & $Count, "Bestaadnie" )) Then
            $retval = _ReplaceStringInFile(IniRead ( $INI_File, "General", "Target", "Bestaadnie" ),FileReadLine ( $file , $countLine ),'')
        EndIf
        $Count = $Count + 1
    WEnd
    $countLine = $countLine  + 1
 WEnd

He will replace the line, but now I get a blank line. And i want no line !

Link to post
Share on other sites
  • Moderators

Thnx Guys,

You put me ion the right track. I had solved it with doing this:

$file = IniRead ( $INI_File, "General", "Target", "Bestaadnie")
 $countLine = 1
 While $countLine <= _FileCountLines( $file )
    $Count = 1
    While IniRead ( $INI_File, "DelLine", "Search" & $Count, "Bestaadnie" ) <> "Bestaadnie"
        If StringInStr(FileReadLine ( $file , $countLine ), IniRead ( $INI_File, "DelLine", "Search" & $Count, "Bestaadnie" )) Then
            $retval = _ReplaceStringInFile(IniRead ( $INI_File, "General", "Target", "Bestaadnie" ),FileReadLine ( $file , $countLine ),'')
        EndIf
        $Count = $Count + 1
    WEnd
    $countLine = $countLine  + 1
 WEnd

He will replace the line, but now I get a blank line. And i want no line !

You'll have to use _FileWriteToLine() and the delete line option then.

Edit:

Remarks

If _FileWriteToLine is called with $fOverWrite as 1 and $sText as "", it will delete the line.

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to post
Share on other sites
  • Moderators

Are you trying to replace something in an Ini or does the Ini have all the files you want to search?

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to post
Share on other sites
  • Moderators

I looked what you wrote over and over, and as confusing as it was, I think this is what you're looking for... it will ouput the file as OutPut.txt from where you're running the script from, so you can see if it does the trick:

Local $INI_File = 'MyIni.ini'
Local $file = IniRead($INI_File, "General", "Target", "Bestaadnie")
_IniDeleteFileLine($INI_File, 'DelLine', $file, @ScriptDir & '\OutPut.txt')

Func _IniDeleteFileLine($hIni, $sSection, $hFile, $hWrite)
    Local $aIniArray = IniReadSection($hIni, $sSection), $iFound = False
    Local $aFile = StringSplit(StringStripCR(FileRead($hFile)), @LF), $sHold
    For $iCC = 1 To $aFile[0]
        For $xCC = 1 To $aIniArray[0][0]
            If StringInStr($aFile[$iCC], $aIniArray[$xCC][1]) Then
                $iFound = True
                ExitLoop
            EndIf
        Next
        If Not $iFound Then $sHold &= $aFile[$iCC] & @CRLF
        $iFound = False
    Next
    FileClose(FileWrite(FileOpen($hWrite, 2), StringTrimRight($sHold, StringLen(@CRLF))))
    Return 1
EndFunc
Be sure to change the "MyIni.ini" to the actual path and name of your ini.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to post
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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...