Jump to content

What am I doing wrong..?


Recommended Posts

Hey guys, I made some modifications in my program because the function below seemed to skip running once in a while.. And now it wont run at all. The file seems accessible, but the first tile read of $sS seems to return nothing. What am I missing? I have attached the file that I am working on. The function is just supposed to go through every line and delete the last /'s at the end of the lines. 

Func ProcessAndCleanFiles($filetocleanup) ; Basically deletes the /'s at the end of a sample analytes
    $filetocleanup = "X:\MacroShare\IC3A.txt"


    If _PathIsWritable(@SystemDir) = False Then MsgBox(0, 0, "You're not running as an admin.")
    If _PathIsWritable($filetocleanup) = False Then MsgBox(0, 0, "You don't have access to IC3 files!")

    MsgBox(0, '', _PathIsWritable("X:\MacroShare\IC3A.txt"))
    MsgBox(0, 0, "This file's existence is: " & FileExists("X:\MacroShare\IC3A.txt"))

    Local $sS = FileOpen($filetocleanup)
    If @error Then
        Sleep(500)
        Local $sS = FileOpen($filetocleanup)
        MsgBox("ERROR", "ERROR @ LINE # " & @ScriptLineNumber, 10)
        ConsoleWrite("Error @ " & @ScriptLineNumber & @CRLF)
    EndIf

    Sleep(100)
    $Whattowrite = FileRead($sS)
    If @error Then MsgBox(0, "Error", "Fileclose error @ Line: " & @ScriptLineNumber)
    _ArrayDisplay($Whattowrite)
    MsgBox(0, "", $Whattowrite)

    Sleep(100)
    $Whattowrite = StringRegExpReplace($Whattowrite, "(?m)(.*)/(\s.*$)", "$1$2") ; RegExp
    If @error Then
        Sleep(500)
        MsgBox(0, 0, "ERROR FIRST", "ERROR @ LINE #  FIRST" & @ScriptLineNumber)
        Sleep(1000)
        $Whattowrite = StringRegExpReplace(FileRead($filetocleanup), "(?m)(.*)/(\s.*$)", "$1$2") ; RegExp
        MsgBox(0, 0, "ERROR FIRST", "ERROR @ LINE #  FIRST" & @ScriptLineNumber)
        ConsoleWrite("Error @ " & @ScriptLineNumber & @CRLF)
    EndIf
    Sleep(200) ; ERROR PRONE!
    Local $hFileOpen = FileOpen($filetocleanup, 2)
    If @error Then
        Sleep(500)
        Local $hFileOpen = FileOpen($filetocleanup, 2)
        MsgBox(0, 0, "ERROR FIRST", "ERROR @ LINE #  FIRST" & @ScriptLineNumber)
        ConsoleWrite("Error @ " & @ScriptLineNumber & @CRLF)
    EndIf
    Sleep(100)
    If $hFileOpen = -1 Then
        MsgBox(0, "", "An error occurred when reading the file.")
        Return False
    EndIf
    Sleep(100)
    FileWrite($filetocleanup, $Whattowrite)
    Sleep(100)
    Local $aFile, $aArray
    _FileReadToArray($filetocleanup, $aFile, $FRTA_NOCOUNT)
    $aArray = _ArrayUnique($aFile)
    _ArrayDelete($aArray, 0)
    _FileWriteFromArray($filetocleanup, $aArray)
    FileClose($sS)
    If @error Then MsgBox(0, "Error", "Fileclose error @ Line: " & @ScriptLineNumber)
    FileClose($hFileOpen)
    If @error Then MsgBox(0, "Error", "Fileclose error @ Line: " & @ScriptLineNumber)
;~  MsgBox(0, 0, "running1")

EndFunc   ;==>ProcessAndCleanFiles

IC3.txt

Link to comment
Share on other sites

Just looking at the code, it appears that you have 2 file handles open to the same file.  One is readonly and the other is set to overwrite.  I would close the readonly handle before opening the file in overwrite mode.

Link to comment
Share on other sites

56 minutes ago, TheXman said:

Just looking at the code, it appears that you have 2 file handles open to the same file.  One is readonly and the other is set to overwrite.  I would close the readonly handle before opening the file in overwrite mode.

That's a great point, thanks, but something is still wrong. I'm missing something small and stupid somewhere, I just can't see it.

Link to comment
Share on other sites

@BatMan22

Also, according to the help file, FileOpen() returns -1 if an error occurs, not @error.

Local $sS = FileOpen($filetocleanup)
If @error Then
    Sleep(500)
    Local $sS = FileOpen($filetocleanup)
    MsgBox("ERROR", "ERROR @ LINE # " & @ScriptLineNumber, 10)
    ConsoleWrite("Error @ " & @ScriptLineNumber & @CRLF)
EndIf

So to correctly check for an error opening the file, your code would be:

Local $sS = FileOpen($filetocleanup)
If $sS = -1 Then
    Sleep(500)
    Local $sS = FileOpen($filetocleanup)
    MsgBox("ERROR", "ERROR @ LINE # " & @ScriptLineNumber, 10)
    ConsoleWrite("Error @ " & @ScriptLineNumber & @CRLF)
EndIf

Also, I don't know why you are trying to open the file again inside your error routine.  I also don't know what all of the sleep() that you have sprinkled throughout the script are for either.

The return from the FileOpen() function is a handle not a string.  Although it isn't an error to say "$sS = FileOpen(<some file path>)", it is suggested to use $h for handles.  Therefore, it would look something like "$hInputFile = FileOpen(<some file path>)".

Link to comment
Share on other sites

Although it is a bit of a mess, here is your code that works for me.  I commented out unnecessary lines and lines that would not compile because the references do not exist.

#include <Array.au3>
#include <File.au3>



ProcessAndCleanFiles("ic3.txt")

Func ProcessAndCleanFiles($filetocleanup) ; Basically deletes the /'s at the end of a sample analytes
;~     $filetocleanup = "X:\MacroShare\IC3A.txt"


;~     If _PathIsWritable(@SystemDir) = False Then MsgBox(0, 0, "You're not running as an admin.")
;~     If _PathIsWritable($filetocleanup) = False Then MsgBox(0, 0, "You don't have access to IC3 files!")

;~     MsgBox(0, '', _PathIsWritable("X:\MacroShare\IC3A.txt"))
;~     MsgBox(0, 0, "This file's existence is: " & FileExists("X:\MacroShare\IC3A.txt"))

    Local $sS = FileOpen($filetocleanup)
    If $sS = -1 Then
;~         Sleep(500)
;~         Local $sS = FileOpen($filetocleanup)
        MsgBox("ERROR", "ERROR @ LINE # " & @ScriptLineNumber, 10)
        ConsoleWrite("Error @ " & @ScriptLineNumber & @CRLF)
        Exit
    EndIf

;~     Sleep(100)
    $Whattowrite = FileRead($sS)
    If @error Then
        MsgBox(0, "Error", "Fileclose error @ Line: " & @ScriptLineNumber & "  @error: " & @error)
;~     _ArrayDisplay($Whattowrite)
        Exit
    EndIf
    MsgBox(0, "", $Whattowrite)


;~     Sleep(100)
    $Whattowrite = StringRegExpReplace($Whattowrite, "(?m)(.*)/(\s.*$)", "$1$2") ; RegExp
    If @error Then
;~         Sleep(500)
        MsgBox(0, 0, "ERROR FIRST", "ERROR @ LINE #  FIRST" & @ScriptLineNumber)
;~         Sleep(1000)
        $Whattowrite = StringRegExpReplace(FileRead($filetocleanup), "(?m)(.*)/(\s.*$)", "$1$2") ; RegExp
        MsgBox(0, 0, "ERROR FIRST", "ERROR @ LINE #  FIRST" & @ScriptLineNumber)
        ConsoleWrite("Error @ " & @ScriptLineNumber & @CRLF)
    EndIf
;~     Sleep(200) ; ERROR PRONE!
    FileClose($sS)

    Local $hFileOpen = FileOpen($filetocleanup, 2)
    If @error Then
;~         Sleep(500)
;~         Local $hFileOpen = FileOpen($filetocleanup, 2)
        MsgBox(0, 0, "ERROR FIRST", "ERROR @ LINE #  FIRST" & @ScriptLineNumber)
        ConsoleWrite("Error @ " & @ScriptLineNumber & @CRLF)
    EndIf
;~     Sleep(100)
    If $hFileOpen = -1 Then
        MsgBox(0, "", "An error occurred when reading the file.")
        Return False
    EndIf
;~     Sleep(100)
    FileWrite($filetocleanup, $Whattowrite)
;~     Sleep(100)
    Local $aFile, $aArray
    _FileReadToArray($filetocleanup, $aFile, $FRTA_NOCOUNT)
    $aArray = _ArrayUnique($aFile)
    _ArrayDelete($aArray, 0)
    _FileWriteFromArray($filetocleanup, $aArray)
;~     FileClose($sS)
;~     If @error Then MsgBox(0, "Error", "Fileclose error @ Line: " & @ScriptLineNumber)
    FileClose($hFileOpen)
    If @error Then MsgBox(0, "Error", "Fileclose error @ Line: " & @ScriptLineNumber)
;~  MsgBox(0, 0, "running1")

EndFunc   ;==>ProcessAndCleanFiles

 

Link to comment
Share on other sites

@BatMan22

Also, if/when you encounter errors, you usually want to exit or stop processing that piece of code.  Your code continues regardless of errors.

Edited by TheXman
Link to comment
Share on other sites

14 minutes ago, TheXman said:

Although it is a bit of a mess, here is your code that works for me.  I commented out unnecessary lines and lines that would not compile because the references do not exist.

#include <Array.au3>
#include <File.au3>



ProcessAndCleanFiles("ic3.txt")

Func ProcessAndCleanFiles($filetocleanup) ; Basically deletes the /'s at the end of a sample analytes
;~     $filetocleanup = "X:\MacroShare\IC3A.txt"


;~     If _PathIsWritable(@SystemDir) = False Then MsgBox(0, 0, "You're not running as an admin.")
;~     If _PathIsWritable($filetocleanup) = False Then MsgBox(0, 0, "You don't have access to IC3 files!")

;~     MsgBox(0, '', _PathIsWritable("X:\MacroShare\IC3A.txt"))
;~     MsgBox(0, 0, "This file's existence is: " & FileExists("X:\MacroShare\IC3A.txt"))

    Local $sS = FileOpen($filetocleanup)
    If $sS = -1 Then
;~         Sleep(500)
;~         Local $sS = FileOpen($filetocleanup)
        MsgBox("ERROR", "ERROR @ LINE # " & @ScriptLineNumber, 10)
        ConsoleWrite("Error @ " & @ScriptLineNumber & @CRLF)
        Exit
    EndIf

;~     Sleep(100)
    $Whattowrite = FileRead($sS)
    If @error Then
        MsgBox(0, "Error", "Fileclose error @ Line: " & @ScriptLineNumber & "  @error: " & @error)
;~     _ArrayDisplay($Whattowrite)
        Exit
    EndIf
    MsgBox(0, "", $Whattowrite)


;~     Sleep(100)
    $Whattowrite = StringRegExpReplace($Whattowrite, "(?m)(.*)/(\s.*$)", "$1$2") ; RegExp
    If @error Then
;~         Sleep(500)
        MsgBox(0, 0, "ERROR FIRST", "ERROR @ LINE #  FIRST" & @ScriptLineNumber)
;~         Sleep(1000)
        $Whattowrite = StringRegExpReplace(FileRead($filetocleanup), "(?m)(.*)/(\s.*$)", "$1$2") ; RegExp
        MsgBox(0, 0, "ERROR FIRST", "ERROR @ LINE #  FIRST" & @ScriptLineNumber)
        ConsoleWrite("Error @ " & @ScriptLineNumber & @CRLF)
    EndIf
;~     Sleep(200) ; ERROR PRONE!
    FileClose($sS)

    Local $hFileOpen = FileOpen($filetocleanup, 2)
    If @error Then
;~         Sleep(500)
;~         Local $hFileOpen = FileOpen($filetocleanup, 2)
        MsgBox(0, 0, "ERROR FIRST", "ERROR @ LINE #  FIRST" & @ScriptLineNumber)
        ConsoleWrite("Error @ " & @ScriptLineNumber & @CRLF)
    EndIf
;~     Sleep(100)
    If $hFileOpen = -1 Then
        MsgBox(0, "", "An error occurred when reading the file.")
        Return False
    EndIf
;~     Sleep(100)
    FileWrite($filetocleanup, $Whattowrite)
;~     Sleep(100)
    Local $aFile, $aArray
    _FileReadToArray($filetocleanup, $aFile, $FRTA_NOCOUNT)
    $aArray = _ArrayUnique($aFile)
    _ArrayDelete($aArray, 0)
    _FileWriteFromArray($filetocleanup, $aArray)
;~     FileClose($sS)
;~     If @error Then MsgBox(0, "Error", "Fileclose error @ Line: " & @ScriptLineNumber)
    FileClose($hFileOpen)
    If @error Then MsgBox(0, "Error", "Fileclose error @ Line: " & @ScriptLineNumber)
;~  MsgBox(0, 0, "running1")

EndFunc   ;==>ProcessAndCleanFiles

 

Thanks a ton dude... I added the sleeps in there because the program kept 'missing'.. and I thought that maybe the program was missing because the server was lagging or something. Sooo.. turns out my mistake was really simple and stupid. I was calling the wrong file, I realized it after I tried what you posted and noticed that it was still broken and wasn't working. So I tried to move the file to the script directory thinking that it was a file access issue... turns out, I was looking for IC3A.txt, and not ic3.txt.... It was trying to process an empty file the whole time, and working correctly. Also thanks for those error message notes, I've updated all that. 

Anyways, thanks for all your help, you rock :)

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