Jump to content

Writing to a file


Recommended Posts

Ok I am in a bit of a jam here. Either I don't understand or I'm making a stupid mistake. I'm trying to open a file an scan through it. Once I found what I'm looking for I need to modify that line and continu scanning. Then I need to rescan the file to find another thing in there and if I don't find it I need to add it to the top. Right now what it seems to do is open the file scan tell me it's doing the changes but if I open the file nothing has changed. Here is my code.

Dim $found = False
    Dim $line
    
    $fileInfo = FileOpen($PATHLISTEPC & $FILELISTEPC, 0)
    FileWriteLine($fileLog, "Opening " & $PATHLISTEPC & $FILELISTEPC)

    If $fileInfo = -1 Then
        FileWriteLine($fileLog, "Not able to open file!")
        Exit
    EndIf
    
    $i = 1
    
; ##################################################################
; Looking for a duplicate TB, if any found the put it in comment
; ##################################################################
    While 1
        FileWriteLine($fileLog, "Looking for duplicate NomWin: " & $NomWin)
        $line = FileReadLine($fileInfo)
        If @error = -1 Then ExitLoop
        If StringInStr($line, $NomWin) Then
            FileWriteLine($fileLog, "Duplicate found " & $line & " " & $NomWin)
            If Not StringInStr($line, ";") Then
                FileWriteLine($fileLog, "Adding the; at the start of the line")
                _FileWriteToLine($PATHLISTEPC & $FILELISTEPC, $i, ";" & $line, 1)
                FileWriteLine($fileLog, "; added")
            EndIf
        EndIf
        $i = $i + 1
    WEnd
    
    FileClose($fileInfo)
    
    $fileInfo = FileOpen($PATHLISTEPC & $FILELISTEPC, 0)

    If $fileInfo = -1 Then
        FileWriteLine($fileLog, "Not able to open file for MAC insertion!")
        Exit
    EndIf

    $i = 1

; ######################################################
; Looking for MAC, if found change it for the new TB
; ######################################################
    While 1
        $line = FileReadLine($fileInfo)
        If @error = -1 Then ExitLoop
        If StringInStr($line, $MAC) Then
            FileWriteLine($fileLog, "MAC found MAC: " & $line & " " & $MAC)
            _FileWriteToLine($PATHLISTEPC & $FILELISTEPC, $i, $MAC & " = " & $NomWin, 1)
            $found = True
            ExitLoop
        EndIf
        $i = $i + 1
    WEnd
    
    If Not $found Then
        FileWriteLine($fileLog, "MAC not found in file adding at the top")
        _FileWriteToLine($PATHLISTEPC & $FILELISTEPC, 1, $MAC & " = " & $NomWin)
    EndIf

    FileClose($fileInfo)

Thank you for your help it's greatly appreciated.

Edited by AllSystemGo
Link to comment
Share on other sites

Advice: Use $text = FileRead('filename.txt') to read whole file into memory instead of FileReadLine() which is slower.

Then use StringSplit($text, @CRLF, 1) to make array from your text.

Then use For NEXT loop to go through array and find/modify what you need

Finally write your modified array to file - you can use FileWriteFromArray() for examle.

Link to comment
Share on other sites

Thank you for the heads up zedna. Does it explain why it's not working for me??? The _FileWriteToLine isn't writing anything!!! Could it be that I'm opening the file for read only at the FileOpen statement then I try and use the _FileWriteToLine ???

BTW this is what I'm doing right now, let me know if you think it's right, I'm a bit confused:

$fileInfo = FileOpen($PATHLISTEPC & $FILELISTEPC, 1)
    _FileWriteLog(@ScriptDir & "\Repart.log", "Opening " & $PATHLISTEPC & $FILELISTEPC)

    If $fileInfo = -1 Then
        _FileWriteLog(@ScriptDir & "\Repart.log", "Not able to open file!")
        Exit
    EndIf
    
    $i = 1
    
; ##################################################################
; Looking for a duplicate TB, if any found the put it in comment
; ##################################################################
    While 1
        _FileWriteLog(@ScriptDir & "\Repart.log", "Looking for duplicate NomWin: " & $NomWin)
        $text = FileRead($fileInfo)
        If @error = -1 Then ExitLoop
        $avarray = StringSplit($text, @CRLF, 1)
        
        For $element In $avarray
            If StringInStr($element, $NomWin) Then
                $element = ";" & $element
            EndIf
        Next
        
        _FileWriteFromArray($fileInfo, $avarray)
Edited by AllSystemGo
Link to comment
Share on other sites

Anyways I figured that it was because the file was open read-only. So before the writing I close the file and reopen it.

I tried the FileRead that you tried to show me but I can't seem to make it work properly.

Thank you for the help

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