Jump to content

Deleting folders


Recommended Posts

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!

Link to comment
Share on other sites

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

  • Moderators

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

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

  • Moderators

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

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

  • Moderators

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.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

  • Developers

Doubt that answer does you any favours....

This was for forever0donotknowme

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

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,
 

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