Jump to content
Sign in to follow this  
Greenyoda

Parsing Text file

Recommended Posts

Greenyoda

Is their a command to Read a line from a text file and search the line for a word? Then maybe delete the line?

Share this post


Link to post
Share on other sites
stampy

look some of these up in the help file. It should get you started.

FileRead

FileReadLine

_FileReadToArray

StringInStr

Share this post


Link to post
Share on other sites
/dev/null

Is their a command to Read a line from a text file and search the line for a word? Then maybe delete the line?

Take a look at the code of _ReplaceStringInFile() (in file.au3 / include dir). That will get you started.

Cheers

Kurt


__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Share this post


Link to post
Share on other sites
Uten

Or maybe use StringRegExp if you want to parse the entier file. Can be a chalange thought :whistle:

EDIT:Try to chofe (??) up some code and I'm sure you will get help correcting it if you need some help with that.

Edited by Uten

Share this post


Link to post
Share on other sites
Greenyoda

Ok I worked on it some, Got a whole lot of code done. Then I realized that replace string would be better. So now I have:

#include <file.au3>

;Variables

$search = "found"

$replace = ""

$file = FileOpenDialog ( "findme", "c:/", "" )

; Check if file opened for reading OK

If $file = -1 Then

MsgBox(0, "Error", "Unable to open file.")

Exit

EndIf

;read the file

_ReplaceStringInFile($file, $search, $replace, 0, 1)

Msgbox( 0, "Deleted", "done boss" )

Is their anyway to Have it Delete the line?

Share this post


Link to post
Share on other sites
SmOke_N

Look at _FileWriteToLine() since you're already using the File.au3 file, puting "" in the $sText parameter, and using 1 as the $fOverWrite parameter will delete the line.


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.

Share this post


Link to post
Share on other sites
Rad

EDIT:Try to chofe (??) up some code and I'm sure you will get help correcting it if you need some help with that.

chofe = cough?

hehe

Share this post


Link to post
Share on other sites
Greenyoda

Hmm I tried but their is still something Wrong. can someone look over the code and point out errors?

#include <file.au3>

;Variables

$search = "found"

$file = FileOpenDialog ( "findme", "c:/", "" )

; Check if file opened for reading OK

If $file = -1 Then

MsgBox(0, "Error", "Unable to open file.")

Exit

EndIf

;read the file

While 1

$x = 1

FileReadLine( $file, $x )

$isittheir = StringInStr( "found", "found" )

if $isittheir > 0 Then

_FileWriteToLine($file, $x, 1)

$x = $x + 1

ElseIf @error = -1 Then

FileClose ( $file )

MsgBox( 0, "Done", "We are done" )

EndIf

WEnd

Share this post


Link to post
Share on other sites
stampy

The FileReadLine wasn't actually going into a variable and the StringInst was searching for "found" inside of "found". Try this.

#include <file.au3>
;Variables
$search = "found"
$file = FileOpenDialog ( "findme", "c:/", "" )
; Check if file opened for reading OK
If $file = -1 Then
MsgBox(0, "Error", "Unable to open file.")
Exit
EndIf
;read the file
While 1
$x = 1
$line = FileReadLine( $file, $x )
$isittheir = StringInStr( $line, "found" )
if $isittheir > 0 Then
_FileWriteToLine($file, $x, 1)
$x = $x + 1
ElseIf @error = -1 Then
FileClose ( $file )
MsgBox( 0, "Done", "We are done" )
EndIf
WEnd

Share this post


Link to post
Share on other sites
stampy

The fileWriteToLine need some text sent to it.

_FileWriteToLine($sFile, $iLine, $sText[, $fOverWrite])

If you wish to delete the line all together send a blank sting.

_FileWriteToLine($file, $x,"", 1)

Give that a shot.

Share this post


Link to post
Share on other sites
Greenyoda

#include <file.au3>
;Variables
$search = "found"
$file = FileOpenDialog ( "findme", "c:/", "" )
; Check if file opened for reading OK
If $file = -1 Then
MsgBox(0, "Error", "Unable to open file.")
Exit
EndIf
$x = 1
$numbers = 0
;read the file
While 1
$line = FileReadLine( $file, $x )
$isittheir = StringInStr( $line, $search )
if $isittheir > 0 Then
    _FileWriteToLine($file, $x, "", 1)
    TrayTip("The number of lines deleted",$numbers , 5)
    $x = $x + 1
    $numbers = $numbers + 1
ElseIf $line = -1 Then
FileClose ( $file )
MsgBox( 0, "Done", "We are done" )
EndIf
WEnd

I changed it out but it didnt seem to work:(

Share this post


Link to post
Share on other sites
xcal

Try this out:

#include <File.au3>

Dim $array

$file = FileOpenDialog('Choose Your File', 'c:\', 'All Files (*.*)')
$find = InputBox('What To Find', 'Type in below what to search for.' & @CRLF & _
        'Lines that match will be removed.', 'Text To Match', '', 200, 140)
$found = 0
$lines = ''

If $file <> '' And $find <> '' Then
    _FileReadToArray($file, $array)
    For $i = 1 To UBound($array) - 1
        If StringInStr($array[$i], $find) Then
            $array[$i] = ''
            $found += 1
            $lines &= $i & ', '
        EndIf
    Next
    _FileWriteFromArray($file, $array, 1)
    MsgBox(64, 'Done', 'Total lines found = ' & $found & @CRLF & _
            'Removed line number(s) = ' & StringTrimRight($lines, 2))
Else
    MsgBox(48, 'Error', 'A file was not picked or what to find was cancelled/empty!')
EndIf

-edit 1 small change

-edit 2 wrong msgbox icon ;)

-edit 3 nothing worth noting... just picking at it :lmao:

Edited by xcal

Share this post


Link to post
Share on other sites
Greenyoda

Thanks a bunch Xcal!!!

Works Perfectly:)

Any way to make it delete the spaces?

Share this post


Link to post
Share on other sites
/dev/null

Any way to make it delete the spaces?

Spaces in general? --> StringReplace()

Empty lines? --> look at the code of _FileWriteFromArray (file.au3). Copy that code and modify it. Don't write a line if it's empty(StringLen($a_Array[$x]) = 0).

Cheers

Kurt


__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

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  

×