Sign in to follow this  
Followers 0
Michiel78

Replace within textfile

5 posts in this topic

At the moment I am working on a piece of code that is part of a bigger script. This piece of code is supposed to check in a textlogfile (csv style) whether or not an IDnumber already exists.

The csv textfile looks like:

IDnumber,process_status,upload_status,email_status,date

000103,Processed,Not_Uploaded,Emailed,20-11-2010

000103,Not_Processed,Uploaded,Not_Emailed,22-11-2010

When the IDnumber already exists, the script will check if it's been processed. If it is, it will write a new line to the file, if it isn't it will change Not_Processed into Processed. When the IDnumber doesn't exist yet, it will too write a new line to the file, containing the IDnumber + Status (+ other status + date).

The problem is that I cannot get the piece of script to replace Not_Processed with Processed for some reason. The code below is how it looks like at the moment (with a simple StringReplace which prob doesn't work at all), but that's because working with _FileWritetoLine and _ReplaceStringInFile didn't workout either. Could anyone perhaps try to help me with fixing this problem?

#Include <File.au3>
$textfile = FileOpen("log.txt", 0)
Local $succes

If $textfile = -1 Then
    MsgBox(0, "Error", "Could not open the textfile.")
    Exit
EndIf

While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    If StringInStr($line, "000103") Then ;checks if IDnumber 000103 already exists
        If StringInStr($line, "Not_Processed") Then ;checks if 000103 has been processed
        StringReplace($line, "Not_Processed", "Processed") ;if it hasn't been > replace the status
        $succes = 1
        ExitLoop
        EndIf
    EndIf
WEnd

If Not $succes Then ;if the IDnumber doesn't exist yet OR if it does exist but it has status processed in all lines > write new line
FileWriteLine($textfile, "Processed")
EndIf
FileClose($textfile)

Share this post


Link to post
Share on other sites



A few things for you to consider:

- StringReplace, replaces what you specified in that very line you were processing (not on the file - the replacing is done only in memory and it will be "forgotten" the next cycle)

- FileWriteLine, writes a line "at the end" of the file, it won't replace something...

- _FileWriteToLine and _StringReplaceInFile should work in your case - no idea why are you saying that they don't work? example of the script which didn't work?

Everything else you might try it will be done similarly to these 2 functions. Well, if you want to reinvent the wheel:

- _FileReadToArray

- parse the whole array looking for the info you need

- if you find the info, replace it and use _ArrayAdd to add a new line at that position you want

- when all done, _FileWriteFromArray


SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

A few things for you to consider:

- StringReplace, replaces what you specified in that very line you were processing (not on the file - the replacing is done only in memory and it will be "forgotten" the next cycle)

- FileWriteLine, writes a line "at the end" of the file, it won't replace something...

- _FileWriteToLine and _StringReplaceInFile should work in your case - no idea why are you saying that they don't work? example of the script which didn't work?

Everything else you might try it will be done similarly to these 2 functions. Well, if you want to reinvent the wheel:

- _FileReadToArray

- parse the whole array looking for the info you need

- if you find the info, replace it and use _ArrayAdd to add a new line at that position you want

- when all done, _FileWriteFromArray

Well the problem I ran into with _ReplaceStringInFile is that it replaces any "Not_Processed" in the textfile and not necessarily the one from the specific IDnumber. _FileWriteToLine didn't work for me either, because you have to refer to the linenumber to overwrite and I don't how to "have the linenumber returned" by FileReadLine. Also I figured out that FileOpen with flag 1 will cause the script not to find anything, so I guess it has to be set to 1 after the line has been found?

I'm sure one of the functions you mentioned will work, but I'm still quite new to AutoIt and in this case reading the helpfile and experimenting didn't help solving the problem. Any ideas how to replace the Not_Processed with Processed only in the line that has the requested IDnumber in it?

Edited by Michiel78

Share this post


Link to post
Share on other sites

- you have the array

For $i=1 to $yourArrayNameHere[0]
    If StringInStr($yourArrayNameHere[$i], $YourIDNumberHere) And StringInStr($yourArrayNameHere[$i], "Not_Processed") Then
        $yourArrayNameHere[$i] = StringReplace($yourArrayNameHere[$i], "Not_Processed", "Processed")
        _ArrayInsert($yourArrayNameHere[$i], $i + 1, "whatever you want the new line to be")
    EndIf
Next

- do whatever you want using my example

- at the end, _FileWriteFromArray


SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Thank you very much enaiman! I've edited your code with _FileWriteToLine and modified some other things, which eventually did the trick!

The new code is:

#Include <File.au3>
Local $succes
Dim $logfile
$file = "log.txt"

    _FileReadToArray($file, $logfile)
    For $i=1 to $logfile[0]
    If StringInStr($logfile[$i], "000103") And StringInStr($logfile[$i], "Not_Processed") Then ;000103 as an example ID
    $newline = StringReplace($logfile[$i], "Not_Processed", "Processed")
    _FileWriteToLine($file, $i, $newline, 1)
    $succes = 1
    EndIf
    Next

If Not $succes Then
MsgBox(0, "Test", "Nothing has been found matching your criteria!")
EndIf
Edited by Michiel78

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