Jump to content


Photo

Scheduled File/Folder deletion


  • Please log in to reply
3 replies to this topic

#1 fragman

fragman
  • Members
  • 1591 posts

Posted 14 January 2012 - 09:22 PM

Here's a function that can be used to schedule file/folder deletion for the next reboot. This is useful for compiled uninstaller scripts that need to delete themselves (and possibly other uses as well).

ScheduleDeletion(FileOrFolder)
{
	static MOVEFILE_DELAY_UNTIL_REBOOT := 4
	if(!FileExist(FileOrFolder))
		return
	
	;MoveFileEx requires that the directories which are scheduled for deletion are empty, so the files in them need to be deleted first.
	;For this we do a recursed call of this function that first schedules all files and folders at the deepest level.
	Loop %FileOrFolder%\*.*, 1, 1
	{
		if(InStr(FileExist(A_LoopFileLongPath), "D"))
			ScheduleDeletion(A_LoopFileLongPath)
		else
			DllCall("MoveFileEx", "STR", A_LoopFileLongPath, "PTR", 0, "UINT", MOVEFILE_DELAY_UNTIL_REBOOT)
	}
	DllCall("MoveFileEx", "STR", FileOrFolder, "PTR", 0, "UINT", MOVEFILE_DELAY_UNTIL_REBOOT)
}


#2 Guests

  • Guests

Posted 15 January 2012 - 12:56 PM

Thanks fragman i always wondered how installation programs clean up after a reboot.


For ahk basic only small change required.

ScheduleDeletion(FileOrFolder)
{
   static MOVEFILE_DELAY_UNTIL_REBOOT := 4
   if(!FileExist(FileOrFolder))
      return
   
   ;MoveFileEx requires that the directories which are scheduled for deletion are empty, so the files in them need to be deleted first.
   ;For this we do a recursed call of this function that first schedules all files and folders at the deepest level.
   Loop %FileOrFolder%\*.*, 1, 1
   {
      if(InStr(FileExist(A_LoopFileLongPath), "D"))
      {
         ScheduleDeletion(A_LoopFileLongPath)
         msgbox %A_LoopFilelongpath%
         }
      else
         DllCall("MoveFileEx", "STR", A_LoopFileLongPath, "Uint", 0, "Uint", MOVEFILE_DELAY_UNTIL_REBOOT)
   }
   DllCall("MoveFileEx", "STR", FileOrFolder, "Uint", 0, "Uint", MOVEFILE_DELAY_UNTIL_REBOOT)
}


#3 fragman

fragman
  • Members
  • 1591 posts

Posted 15 January 2012 - 01:14 PM

You forgot a MsgBox in the code ;)

#4 lain

lain
  • Members
  • 184 posts

Posted 15 January 2012 - 03:46 PM

Thanks......... :oops:

I replaced ccleaner with your code (i only used it to empty temp folders with locked files)


ScheduleDeletion(FileOrFolder)
{
   static MOVEFILE_DELAY_UNTIL_REBOOT := 4
   if(!FileExist(FileOrFolder))
      return
   
   ;MoveFileEx requires that the directories which are scheduled for deletion are empty, so the files in them need to be deleted first.
   ;For this we do a recursed call of this function that first schedules all files and folders at the deepest level.
   Loop %FileOrFolder%\*.*, 1, 1
   {
      if(InStr(FileExist(A_LoopFileLongPath), "D"))
        ScheduleDeletion(A_LoopFileLongPath)
      else
        DllCall("MoveFileEx", "STR", A_LoopFileLongPath, "Uint", 0, "Uint", MOVEFILE_DELAY_UNTIL_REBOOT)
   }
   DllCall("MoveFileEx", "STR", FileOrFolder, "Uint", 0, "Uint", MOVEFILE_DELAY_UNTIL_REBOOT)
}