Jump to content

Parsing Text file


 Share

Recommended Posts

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 *

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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?
Link to comment
Share on other sites

  • Moderators

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

#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:(

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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 *

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...