Jump to content

FileDelete not working


VeeDub
 Share

Recommended Posts

Hello,

The following script is meant to delete all files in C:\Users\<username>\AppData\Local\Temp. There are a couple of exceptions and that logic works OK.

Unfortunately the FileDelete always returns Status <> 1 (delete failed) and I don't understand why.

The OS where the script is running is W7 x64. I mention this because I wonder whether there is something funny going on due to the OS.

In the While loop I needed to add a check for a blank file name on the 2nd line of the loop because otherwise the FileFindFirstFile returns a never-ending list of blank entries after all of the real file & directory entries are returned (and I have not seen that behaviour before).

As for why the FileDelete does not work, I'm guessing I've done something silly which I keep missing. It is not a NTFS permissions issue nor is it a RunAs issue.

;  Obtain list of user directories
$search = FileFindFirstFile("C:\Users\*.*")
;  Check if the search was successful
If $search = -1 Then
  MsgBox(0, "Error", "No files/directories matched the search pattern")
  Exit
EndIf
While 1
  $file = FileFindNextFile($search)
  If $file = "" Then ExitLoop
  ConsoleWrite("Directory: " & $file & @CRLF)
  If @error Then ExitLoop
  If $file = "Default" Then
   ConsoleWrite("Directory: " & $file & " skipped" & @CRLF)
   ContinueLoop
  EndIf
  If $file = "Default User" Then
   ConsoleWrite("Directory: " & $file & " skipped" & @CRLF)
   ContinueLoop
  EndIf
  If $file = "All Users" Then
   ConsoleWrite("Directory: " & $file & " skipped" & @CRLF)
   ContinueLoop
  EndIf
  If $file = "Public" Then
   ConsoleWrite("Directory: " & $file & " skipped" & @CRLF)
   ContinueLoop
  EndIf
  If Not StringInStr(FileGetAttrib("C:\Users\" & $file),"D") Then
   ConsoleWrite("File: " & $file & " skipped" & @CRLF)
   ContinueLoop
  EndIf
;  Process directory
  If FileExists("C:\Users\" & $file & "\AppData\Local\Temp") Then
   $Status = FileDelete("C:\Users\" & $file & "\AppData\Local\Temp\*.*")
   ConsoleWrite("C:\Users\" & $file & "\AppData\Local\Temp\*.*" & @CRLF)
   If $Status = 1 Then
    ConsoleWrite("Clean Temp successful : " & $file & @CRLF)
   Else
    ConsoleWrite("*** Clean Temp failed *** : " & $file & @CRLF)
   EndIf
  Else
   ConsoleWrite("C:\Users\" & $file & "\AppData\Local\Temp : not found" & @CRLF)
  EndIf
WEnd
;   Close the search handle
    FileClose($search)

Appreciate suggestions on how to troubleshoot, or where I have stuffed up.

Thanks

VW

Link to comment
Share on other sites

Well I worked this out.

It turns out that if FileDelete is unable to delete a file then the operation stops, so using FileDelete with a wildcard *.* is not advisable. This behaviour was logged as a bug (a view that I agree with) but the dev's in their infinite wisdom disagreed ... hence we have this poorly documented feature.

FWIW IMO if the FileDelete behaviour is not a bug, then the behaviour when an error is encountered ought to be documented in the Help. The current example in the Help is a load of bollocks.

So for the record if you want to delete all files in a directory, and you want the FileDelete operation to continue even if a file is encountered that cannot be deleted (file in use, permissions issue etc.) then you need to use FileFindNextFile to select the files to be deleted and delete the files individually.

If you're reading this post after searching the forum about FileDelete, I hope you haven't wasted too much time already.

VW

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