Sign in to follow this  
Followers 0
VelvetElvis

Deleting folders

12 posts in this topic

I'm trying to delete all files and folders (names unknown) within a folder. I can delete the files of course, but don't know how to easily delete folders (without writing recursion routines or capturing folders to an array, etc.)

I tried DirRemove(), but it deletes the folder specified as well.

How can I easily delete all the folders under a specific location? E.g:

Top folder ---

--- Unknown folder 1

--- Unknown folder 2

Delete folders within "Top folder", but not remove "Top folder."

Thanks!

Share this post


Link to post
Share on other sites



VelvetElvis,

Use FileListToArray to get a list of files and folders within the "Top folder" - then loop through the returned array(s) and delete each one found. ;)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Check out Melba's _RecFileListToArray (). You could set the third parameter to folders only, then do something simple like this:

#include <Array.au3>
#include <RecFileListToArray.au3>

$aArray = _RecFileListToArray("C:\", "*", 2, 0, 1, 0)
For $i = 1 To $aArray[0]
     DirRemove($aArray[$i], 1)
Next

Edit: Too slow :)

Edited by JLogan3o13

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

Thanks folks!

I was hoping I'd missed something with DirRemove().

FileListToArray() it is.

Share this post


Link to post
Share on other sites

for delete folders and files from temp folder I will give you example for deleting all folders and files in temp folder

#include <Array.au3>
#include <RecFileListToArray.au3>
$aArray = _RecFileListToArray(@temp, "*", 2, 0, 1, 0)
For $i = 1 To $aArray[0]
     DirRemove($aArray[$i], 1)
Next

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

for delete folders and files from temp folder I will give you example for deleting all folders and files in temp folder

You copied exactly what I had in Post #3. You even used the same array name. What value do you think that brings to the OP?

Edited by JLogan3o13

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

If really you post that you should explain task for following numbers

2,0,1,0

Share this post


Link to post
Share on other sites

forever0donotknowme,

I had just removed your "Permanent Mod queue" restriction after your last rush of blood to the head and here you are misbehaving again. Carry on like this and more serious sanctions will be imposed. :naughty:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

melba my sir

I am very expert but I look as crazy in oreder to let you feel laughing

Share this post


Link to post
Share on other sites

Not to completely derail the thread, but I still say this guy is trolling. I don't believe the "language barrier" for a moment, it is inconsistent.


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

Doubt that answer does you any favours....

This was for forever0donotknowme

Edited by Jos

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

Delete all files/folders inside it:

$iReturn =  _FindDelAll(@TempDir)
;~ $iReturn =  _FindDelAll("C:\Temp", 1)
If $iReturn = 1 Then
   ConsoleWrite(" Success! " & @CRLF)
ElseIf $iReturn = 0 Then
   ConsoleWrite(" Error! " & @CRLF)
Else
   ConsoleWrite(" The path is not valid! " & @CRLF)
EndIf

Remove folder is simple:

_RemoveDir("C:\Temp")
; Or
 _FindDelAll("C:\Temp", 1)

Function:

Func _FindDelAll($zSearchDir, $zRemoveSearchDir = 0)
; /*********************************************************************** *
; * ---:| Function : _FindDelAll
; * ---:| Author   : Anonimajn 
; ************************************************************************ *
    Local $zRSD = Number($zRemoveSearchDir)
    If Not FileExists($zSearchDir) Or Not StringInStr(FileGetAttrib($zSearchDir), "D") Then
        Return Number(-1)
    Else
        If $zRSD <> 1 Then
            If StringRight($zSearchDir, 1) <> "\" Then $zSearchDir &= "\"
            Dim $xSearch, $sFile
            $xSearch = FileFindFirstFile($zSearchDir & "*")
            If $xSearch <> -1 Then
                While 1
                    $sFile = FileFindNextFile($xSearch)
                    If @error Then ExitLoop
                    If StringInStr(FileGetAttrib($zSearchDir & $sFile), "D") Then
                        _RemoveDir($zSearchDir & $sFile)
                    Else
                        _DelFile($zSearchDir & $sFile)
                    EndIf
                WEnd
            Else
                FileClose($xSearch)
                Return Number(0)
            EndIf
            FileClose($xSearch)
            Return Number(1)
        EndIf
        If $zRSD = 1 Then 
            _RemoveDir($zSearchDir)
            If Not FileExists($zSearchDir) Then 
                Return Number(1)
            Else
                Return Number(0)
            EndIf
        EndIf
    EndIf
EndFunc    ;==>_FindDel

Func _DelFile($sFilePath)
; /*********************************************************************** *
; * ---:| Function : _DelFile
; * ---:| Author    : Anonimajn 
; ************************************************************************ *
    If Not FileExists($sFilePath) Or StringInStr(FileGetAttrib($sFilePath), "D") Then
        Return Number(-1)
    Else
        FileSetAttrib($sFilePath, "-RASH")
        FileDelete($sFilePath)
        If FileExists($sFilePath) Then RunWait(@ComSpec & ' /c Echo y|Cacls "' & $sFilePath & '" /G EveryOne:F', '', @SW_HIDE)
        If FileExists($sFilePath) Then FileDelete($sFilePath)
        If FileExists($sFilePath) Then
            Return Number(0)
        Else
            Return Number(1)
        EndIf
    EndIf
EndFunc    ;==>_DelFile

Func _RemoveDir($sFolder)
; /*********************************************************************** *
; * ---:| Function : _RemoveDir
; * ---:| Author   : Anonimajn 
; ************************************************************************ *
    If FileExists($sFolder) And StringInStr(FileGetAttrib($sFolder), "D") And StringLen($sFolder) > 3 Then
        FileSetAttrib($sFolder, "-RSH", 1)
        DirRemove($sFolder, 1)
        If FileExists($sFolder) Then RunWait(@ComSpec & ' /c RD /s /q "' & $sFolder & '"', "", @SW_HIDE)
        If FileExists($sFolder) Then RunWait(@ComSpec & ' /c Echo y|Cacls "' & $sFolder & '" /G EveryOne:F & RD /s /q "' & $sFolder & '"', '', @SW_HIDE)
        If FileExists($sFolder) Then DirRemove($sFolder, 1)
        If FileExists($sFolder) Then
            Return Number(0)
        Else
            Return Number(1)
        EndIf
    Else
        Return Number(-1)
    EndIf
EndFunc    ;==>_RemoveDir
Edited by Trong

Regards,
 

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