Sign in to follow this  
Followers 0
Bertman

Replace LINE in textfile

10 posts in this topic

Hello,

I can replace a string in a text file, but i want to replace a complete line if a word is in that line.

I have to work like thit i think:

1. Search for string

2. Replace that whole line with nothing

3. Search next line

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

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

[center]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.[/center]

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

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


[center]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.[/center]

Share this post


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

Share this post


Link to post
Share on other sites

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.

[center]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.[/center]

Share this post


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 !

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

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

[center]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.[/center]

Share this post


Link to post
Share on other sites

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


[center]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.[/center]

Share this post


Link to post
Share on other sites

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.


[center]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.[/center]

Share this post


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
Sign in to follow this  
Followers 0