Sign in to follow this  
Followers 0
pshankland

FileDelete() Problems

7 posts in this topic

Have got the following code:

While 1 
    If FileExists("P:\Ado\APS2\Seres-bb.pj") = 1 Then
        $MyFile = FileOpen("P:\Ado\APS2\Seres-bb.pj", 0)
        $strModelNumber = FileReadLine($MyFile, 1)  ; Reads the Model Number
        $strSoftwareVers = FileReadLine($MyFile, 2) ; Reads the Software Version
        $strSDSize = FileReadLine($MyFile, 3)       ; Reads the SD Card size
        FileClose($MyFile)
        $strTemp = FileDelete($MyFile)
        If $strTemp = 1 Then
            MsgBox(0, "Deleted", "")
        Else
            MsgBox(0, "NOT Deleted", "")
        EndIf
        ExitLoop
    Else
        Sleep (5000) ; retry
    EndIf
WEnd

I have put the MsgBox entries in simply for debugging as I cannot get the file to ever delete, and the debugging proves this.

Where have I gone wrong?

Thanks.

Share this post


Link to post
Share on other sites



Have you tested your FileClose?

While 1
    If FileExists("P:\Ado\APS2\Seres-bb.pj") = 1 Then
        $MyFile = FileOpen("P:\Ado\APS2\Seres-bb.pj", 0)
        $strModelNumber = FileReadLine($MyFile, 1)  ; Reads the Model Number
        $strSoftwareVers = FileReadLine($MyFile, 2) ; Reads the Software Version
        $strSDSize = FileReadLine($MyFile, 3)      ; Reads the SD Card size

        $strTemp = FileClose($MyFile)
        If $strTemp = 1 Then
            MsgBox(0, "Closed", "")
        Else
            MsgBox(0, "NOT Closed", "")
        EndIf
        
        FileDelete($MyFile)

        ExitLoop
    Else
        Sleep(5000) ; retry
    EndIf
WEnd
If the file closes and deletes with that test code, then perhaps there needed to be some time between FileClose and FileDelete... time provided by the msgbox above.


[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites

FileOpen returns the handle of the file but FileDelete need the path and file name as argument

Share this post


Link to post
Share on other sites

I think it's that filedelete needs the path not the handle. Try this...

While 1
    If FileExists("P:\Ado\APS2\Seres-bb.pj") = 1 Then
        $MyFile = FileOpen("P:\Ado\APS2\Seres-bb.pj", 0)
        $strModelNumber = FileReadLine($MyFile, 1)  ; Reads the Model Number
        $strSoftwareVers = FileReadLine($MyFile, 2) ; Reads the Software Version
        $strSDSize = FileReadLine($MyFile, 3)      ; Reads the SD Card size

        $strTemp = FileClose($MyFile)
        If $strTemp = 1 Then
            MsgBox(0, "Closed", "")
        Else
            MsgBox(0, "NOT Closed", "")
        EndIf
       
        FileDelete("P:\Ado\APS2\Seres-bb.pj")

        ExitLoop
    Else
        Sleep(5000) ; retry
    EndIf
WEnd
If the file closes and deletes with that test code, then perhaps there needed to be some time between FileClose and FileDelete... time provided by the msgbox above.
pshankland  Posted Today, 05:45 AM
    Have got the following code:

CODE: AutoIt
While 1 
    If FileExists("P:\Ado\APS2\Seres-bb.pj") = 1 Then
        $MyFile = FileOpen("P:\Ado\APS2\Seres-bb.pj", 0)
        $strModelNumber = FileReadLine($MyFile, 1)  ; Reads the Model Number
        $strSoftwareVers = FileReadLine($MyFile, 2) ; Reads the Software Version
        $strSDSize = FileReadLine($MyFile, 3)      ; Reads the SD Card size
        FileClose($MyFile)
        $strTemp = FileDelete($MyFile)
        If $strTemp = 1 Then
            MsgBox(0, "Deleted", "")
        Else
            MsgBox(0, "NOT Deleted", "")
        EndIf
        ExitLoop
    Else
        Sleep (5000) ; retry
    EndIf
WEnd

goodluck

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

FileOpen returns the handle of the file but FileDelete need the path and file name as argument

Doh - I totally missed that. Perhaps FileDelete should accept a handle... Edit: on second thought, that might not be possible. Edited by herewasplato

[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites

Thanks everyone for the help. The following worked:

While 1 
    If FileExists("P:\Ado\APS2\Seres-bb.pj") = 1 Then
        $MyFile = FileOpen("P:\Ado\APS2\Seres-bb.pj", 0)
        $strModelNumber = FileReadLine($MyFile, 1)  ; Reads the Model Number
        $strSoftwareVers = FileReadLine($MyFile, 2) ; Reads the Software Version
        $strSDSize = FileReadLine($MyFile, 3)       ; Reads the SD Card size
        $strTemp = FileClose($MyFile)
        If $strTemp = 1 Then
            MsgBox(0, "Closed", "")
        Else
            MsgBox(0, "NOT Closed", "")
        EndIf
        FileDelete("P:\Ado\APS2\Seres-bb.pj")
        ExitLoop
    Else
        Sleep (5000) ; retry
    EndIf
WEnd

It is a shame that you can't parse a variable to FileDelete(), but never mind.

Thanks.

Share this post


Link to post
Share on other sites

It is a shame that you can't parse a variable to FileDelete(), but never mind.

Ofcourse you can.

That is not an variable with a filepath string but a file handle. The only reason that your original code fails is you cannot FileDelete a handle.

$FullPath = "P:\Ado\APS2\Seres-bb.pj"
While 1 
    If FileExists($FullPath) = 1 Then
        $MyFile = FileOpen($FullPath, 0)
        $strModelNumber = FileReadLine($MyFile, 1)  ; Reads the Model Number
        $strSoftwareVers = FileReadLine($MyFile, 2) ; Reads the Software Version
        $strSDSize = FileReadLine($MyFile, 3)      ; Reads the SD Card size
        FileClose($MyFile)
        $strTemp = FileDelete($FullPath)
        If $strTemp = 1 Then
            MsgBox(0, "Deleted", "")
        Else
            MsgBox(0, "NOT Deleted", "")
        EndIf
        ExitLoop
    Else
        Sleep (5000) ; retry
    EndIf
WEnd

:whistle:

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