Jump to content

This site uses cookies. By continuing to browse the site you are agreeing to our use of cookies. Find out more here. X
X


Photo

Delete Folder Contents


  • Please log in to reply
14 replies to this topic

#1 hankjrfan

hankjrfan

    Wayfarer

  • Active Members
  • Pip
  • 50 posts

Posted 02 November 2005 - 04:53 PM

I decided to post this function that I wrote not because I think that it is great or inovative, but because in the support forum a common question is "How do I delete the contents of a folder?"

Func _DirRemoveContents($folder)     Local $search, $file     If StringRight($folder, 1) <> "\" Then $folder = $folder & "\"     If NOT FileExists($folder) Then Return 0     FileSetAttrib($folder & "*","-RSH")     FileDelete($folder & "*.*")     $search = FileFindFirstFile($folder & "*")     If $search = -1 Then Return 0     While 1         $file = FileFindNextFile($search)         If @error Then ExitLoop         If StringRight($file, 1) = "." Then ContinueLoop         DirRemove($folder & $file, 1)     WEnd     Return FileClose($search) EndFunc


That is how I do it. I know you could just just do somthing like DirRemove($folder) then DirCreate($folder), but there are times when this is not a good idea.

Well maybe someone new to autoit will find this useful.

***UPDATED CODE***
With the help of SlimShady.

Edited by hankjrfan, 05 November 2005 - 01:09 AM.








#2 svennie

svennie

    Wayfarer

  • Active Members
  • Pip
  • 66 posts

Posted 03 November 2005 - 02:26 PM

Hmm, nice code but the same thing can be done with just 1 line of code:
FileDelete("C:/the dir you want to remove the contents from/*")

The wildcard (*) says everthing that can be placed there must be removed, so you can also remove all files with one extension:
FileDelete("C:/the dir you want to delete all txt files from/*.txt")

Edited by svennie, 03 November 2005 - 06:32 PM.

Sorry for my English, I'm Dutch... =DMedia UDFINet Adv UDF

#3 hankjrfan

hankjrfan

    Wayfarer

  • Active Members
  • Pip
  • 50 posts

Posted 04 November 2005 - 12:17 AM

Hmm, nice code but the same thing can be done with just 1 line of code:

FileDelete("C:/the dir you want to remove the contents from/*")

The wildcard (*) says everthing that can be placed there must be removed, so you can also remove all files with one extension:
FileDelete("C:/the dir you want to delete all txt files from/*.txt")

But FileDelete will not delete subfolders or will it?

#4 svennie

svennie

    Wayfarer

  • Active Members
  • Pip
  • 66 posts

Posted 04 November 2005 - 08:15 AM

But FileDelete will not delete subfolders or will it?

Hmm, there you have a point but when you only want to remove the files this is faster.
Sorry for my English, I'm Dutch... =DMedia UDFINet Adv UDF

#5 SlimShady

SlimShady

    AutoIt lover

  • Active Members
  • PipPipPipPipPipPip
  • 2,383 posts

Posted 04 November 2005 - 12:14 PM

Hmm, there you have a point but when you only want to remove the files this is faster.

Updated.
Func _DirRemoveContents($folder)     Local $search,$file     If StringRight($folder,1) <> "\" Then $folder = $folder & "\"     If Not FileExists($folder) Then Return 0     FileDelete($folder & '*')     $search = FileFindFirstFile($folder & "*.*")       If $search = -1 Then Return 0     While 1         $file = FileFindNextFile($search)         If @error Then ExitLoop         If StringRight($file,1) = "." Then ContinueLoop         If FileGetAttrib($folder & $file) = "D" Then DirRemove($folder & $file, 1)     WEnd     FileClose($search)     Return 1 EndFunc


#6 MHz

MHz

    Just simple

  • MVPs
  • 5,725 posts

Posted 04 November 2005 - 12:37 PM

Nice, but it carries a problem if the directories have other attributes.
$folder = @HomeDrive & '\test' If Not FileExists($folder) Then     DirCreate($folder)     FileSetAttrib($folder, 'H')     MsgBox(0, 'Show HD Attribute', FileGetAttrib($folder))     DirRemove($folder) EndIf


#7 SlimShady

SlimShady

    AutoIt lover

  • Active Members
  • PipPipPipPipPipPip
  • 2,383 posts

Posted 04 November 2005 - 02:58 PM

Nice, but it carries a problem if the directories have other attributes.

$folder = @HomeDrive & '\test' If Not FileExists($folder) Then     DirCreate($folder)     FileSetAttrib($folder, 'H')     MsgBox(0, 'Show HD Attribute', FileGetAttrib($folder))     DirRemove($folder) EndIf

I knew we forgot something B)

Func _DirRemoveContents($folder)     Local $search,$file     If StringRight($folder,1) <> "\" Then $folder = $folder & "\"     If Not FileExists($folder) Then Return 0     FileDelete($folder & '*.*') ;// Delete all files     $search = FileFindFirstFile($folder & "*") ;// Find folders     If $search = -1 Then Return 0     While 1         $file = FileFindNextFile($search)         If @error Then ExitLoop         If StringRight($file,1) = "." Then ContinueLoop         If StringInStr(FileGetAttrib($folder & $file), "D") Then DirRemove($folder & $file, 1)     WEnd     Return FileClose($search) EndFunc

Edited by SlimShady, 04 November 2005 - 03:00 PM.


#8 hankjrfan

hankjrfan

    Wayfarer

  • Active Members
  • Pip
  • 50 posts

Posted 04 November 2005 - 04:19 PM

I knew we forgot something B)

Func _DirRemoveContents($folder)     Local $search,$file     If StringRight($folder,1) <> "\" Then $folder = $folder & "\"     If Not FileExists($folder) Then Return 0     FileDelete($folder & '*.*');// Delete all files     $search = FileFindFirstFile($folder & "*");// Find folders     If $search = -1 Then Return 0     While 1         $file = FileFindNextFile($search)         If @error Then ExitLoop         If StringRight($file,1) = "." Then ContinueLoop         If StringInStr(FileGetAttrib($folder & $file), "D") Then DirRemove($folder & $file, 1)     WEnd     Return FileClose($search) EndFunc

Thanks for your additions SlimShady. I never thought of running FileDelete before starting the loop. That should speed things up quite a bit in some situations and I guess I have never tried to delete the contents of a folder that had a hidden subdirectory. Also could we not just do this:

Func _DirRemoveContents($folder)     Local $search,$file     If StringRight($folder,1) <> "\" Then $folder = $folder & "\"     If Not FileExists($folder) Then Return 0     FileDelete($folder & '*.*');// Delete all files     $search = FileFindFirstFile($folder & "*");// Find folders     If $search = -1 Then Return 0     While 1         $file = FileFindNextFile($search)         If @error Then ExitLoop         If StringRight($file,1) = "." Then ContinueLoop         DirRemove($folder & $file, 1)     WEnd     Return FileClose($search) EndFunc


Do we still have to check the attributes? If we have already ran FileDelete then all that should be left are folders right?

Edited by hankjrfan, 04 November 2005 - 04:21 PM.


#9 SlimShady

SlimShady

    AutoIt lover

  • Active Members
  • PipPipPipPipPipPip
  • 2,383 posts

Posted 04 November 2005 - 05:30 PM

Do we still have to check the attributes? If we have already ran FileDelete then all that should be left are folders right?

very true.
updated once again. more error checking.
Func _DirRemoveContents($folder)     Local $search, $file     If StringRight($folder, 1) <> "\" Then $folder = $folder & "\"     If NOT FileExists($folder) Then Return 0     If NOT FileDelete($folder & '*.*') Then Return 0     $search = FileFindFirstFile($folder & "*")  ;// Find folders     If $search = -1 Then Return 0     While 1         $file = FileFindNextFile($search)         If @error Then ExitLoop         If StringRight($file, 1) = "." Then ContinueLoop         If NOT DirRemove($folder & $file, 1) Then ExitLoop     WEnd     Return FileClose($search) EndFunc

Edited by SlimShady, 04 November 2005 - 05:31 PM.


#10 hankjrfan

hankjrfan

    Wayfarer

  • Active Members
  • Pip
  • 50 posts

Posted 04 November 2005 - 05:54 PM

Thenks again SlimShady I do believe that we know have a fully working model.

It would be nice if some people would test it more in a variety of situations just to be sure.

#11 hankjrfan

hankjrfan

    Wayfarer

  • Active Members
  • Pip
  • 50 posts

Posted 05 November 2005 - 12:35 AM

After Some thought I have decided to make some changes.

The following line would return 0 when the files were not found. So this would be a problem if there were subdirectories but no files.
If NOT FileDelete($folder & '*.*') Then Return 0


So i think we should just stick with this:
FileDelete($folder & '*.*')




And this line. It would exit if there were a problem deleteing one subfolder, but I do not think that that is a reason not to continue to try to delete the rest.
If NOT DirRemove($folder & $file, 1) Then ExitLoop


So I think we should just use this.
DirRemove($folder & $file, 1)


#12 busysignal

busysignal

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 349 posts

Posted 05 November 2005 - 04:49 AM

@hankjrfan, thanks for the post. I have an immediate need for this..

If you want to really shorten what you are trying to do. Try using this function I just came across..

FileRecycle
--------------------------------------------------------------------------------
Sends a file or directory to the recycle bin.

Syntax: FileRecycle ( "source" )
Parameters: source The source path of the file(s) or directory to Recycle. Wildcards are supported.
Return Value:
Success: Returns 1.
Failure: Returns 0 (typically meaning the file is in use or does not exist).

Remarks:
See FileFindFirstFile for a discussion of wildcards.
To remove a directory, simply give the path without a trailing backslash.

Related: FileDelete, FileRecycleEmpty
Example: FileRecycle("C:\*.tmp")


Why do it one at a time when you can just dump the whole thing. :o

Cheers.. B)

Edited by busysignal, 05 November 2005 - 05:00 AM.


#13 MadBoy

MadBoy

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 829 posts

Posted 18 October 2007 - 10:31 AM

I know this is an old post but i have a enhanced that function a bit since the function in this post would stop when errors occur. Mine doesn't..

#Include <File.au3> _DirRemoveContents(@TempDir) Func _DirRemoveContents($folder)     Local $list_of_contents, $status     $list_of_contents = _FileListToArray($folder)     If StringRight($folder, 1) <> "\"  Then $folder = $folder & "\"     If @error = 1 Then Return 3 ; No Files\Folders Found     For $a = 1 To $list_of_contents[0]         FileSetAttrib($folder & "\" & $list_of_contents[$a], "-RSH")         If StringInStr(FileGetAttrib ($folder & $list_of_contents[$a]), "D") Then             $status = DirRemove($folder & $list_of_contents[$a], 1)         Else             $status = FileDelete($folder & $list_of_contents[$a])         EndIf     Next     EndFunc   ;==>_DirRemoveContents


Cheers

#14 Emiel Wieldraaijer

Emiel Wieldraaijer

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 703 posts

Posted 21 October 2007 - 02:33 PM

@MadBoy

Even this script doesn't work perfect..

try you script on the internet explorer cache folder

get the size of the folder

afterwards

use the followingcode

Runwait(@ComSpec & " /c " & "Del " & '"' & @UserProfileDir & '\Local Settings\Temporary Internet Files\Content.IE5\*.*" /Q/S/F', "", @SW_HIDE)

and see the real result

More files are deleted AutoIT has no real alternative to the Del /Q/S/F command

Best regards,

Emiel
Best regards,Emiel WieldraaijerPosted Image

#15 MadBoy

MadBoy

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 829 posts

Posted 22 October 2007 - 08:49 AM

@MadBoy

Even this script doesn't work perfect..

try you script on the internet explorer cache folder

get the size of the folder

afterwards

use the followingcode

Runwait(@ComSpec & " /c " & "Del " & '"' & @UserProfileDir & '\Local Settings\Temporary Internet Files\Content.IE5\*.*" /Q/S/F', "", @SW_HIDE)

and see the real result

More files are deleted AutoIT has no real alternative to the Del /Q/S/F command

Best regards,

Emiel

Try this one then? I've added removal of -A attribute and as you notice all the files in Content.IE5 have that attribute. Should work <_<

AutoIt         
#Include <File.au3> _DirRemoveContents(@UserProfileDir & '\Local Settings\Temporary Internet Files\Content.IE5') Func _DirRemoveContents($folder)     Local $list_of_contents, $status     $list_of_contents = _FileListToArray($folder)     If IsArray($list_of_contents) Then         If StringRight($folder, 1) <> "\"  Then $folder = $folder & "\"         If @error = 1 Then Return 1 ; No Files\Folders Found         For $a = 1 To $list_of_contents[0]             FileSetAttrib($folder & "\" & $list_of_contents[$a], "-RASH")             If StringInStr(FileGetAttrib($folder & $list_of_contents[$a]), "D") Then                 $status = DirRemove($folder & $list_of_contents[$a], 1)             Else                 $status = FileDelete($folder & $list_of_contents[$a])             EndIf         Next     Else         Return 2 ; Directory doesn't exists     EndIf EndFunc   ;==>_DirRemoveContents

Edited by MadBoy, 22 October 2007 - 09:04 AM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users