Erase Everything in a Directory

In my VB6.0 code I have a work directory that I use called "\Tempwork".  Prior to each use I want to make sure that the directory is ocmpletely clear of all contents.

All contents means all files and all subdirectories of all attributes (visble, hidden, readonly etc..) .  I know I can use Kill to get rid of the files.  How do I get rid of the subdirectories(if any)?  Since this is an internal operation I want to get rid of all of these things without bothering the user to verify anything.
LVL 1
mlcktmguyAsked:
Who is Participating?
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Using FSO:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/jsmthdeletefolder.asp

    Sub DeleteFolder(ByVal folder As String)
       Dim fso As Object
       Set fso = CreateObject("Scripting.FileSystemObject")
       fso.DeleteFolder(folder, True)
    End Sub
0
 
mlcktmguyAuthor Commented:
Looks good but I need to extend the concept a bit to accomplish my objective.  I won't know the folder names (if any) each time I ccome in.  How could I modify this logic to do it in an ad hoc manner?  Identify all of the sub-folders in folder \Bogus and then delete any that exist.
0
 
seppertCommented:
This procedure will find all sub folders in a parent folder and delete them:
Sub DeleteSubFolders(ByVal folder As String)
        Dim fso, f, f1, s, sf
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set f = fso.GetFolder(folder)
        Set sf = f.SubFolders
        For Each f1 In sf
            s = folder & "\" & f1.Name 'Append sub folder name to parent folder name
            fso.DeleteFolder (s) 'Delete sub folder
        Next
End Sub

Example of use:
If your sub folders are in "C:\ParentFolder", then run this code line to delete them:

DeleteSubFolders ("C:\ParentFolder")


0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
jahboiteCommented:
Sub DeleteAllInFolder()
   
    Dim f As object
    Dim fso As Object
    Dim tempFolder As String

    tempFolder = "C:\Tempwork"     'use the full path name here

    Set fso = CreateObject("scripting.filesystemobject")
    Set f = fso.getfolder(tempFolder)
    For Each file In f.Files
         file.Delete
    Next

    For Each folder In f.subfolders
        fso.DeleteFolder (folder)
    Next

End Sub
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Just delete the whole folder and then recreate it:

    ...
        Dim path As String
        path = "c:\Bogus"
        DeleteFolder path
        MkDir path


   Sub DeleteFolder(ByVal folder As String)
       Dim fso As Object
       Set fso = CreateObject("Scripting.FileSystemObject")
       fso.DeleteFolder(folder, True)
    End Sub
0
 
ArkCommented:
Private Declare Function SHFileOperation Lib "shell32.dll" (ByRef lpFileOp As SHFILEOPSTRUCT) As Long

Private Const ERROR_SUCCESS = 0&

Private Const FO_MOVE = &H1
Private Const FO_COPY = &H2
Private Const FO_DELETE = &H3
Private Const FO_RENAME = &H4

Private Const FOF_MULTIDESTFILES = &H1
Private Const FOF_CONFIRMMOUSE = &H2
Private Const FOF_SILENT = &H4
Private Const FOF_RENAMEONCOLLISION = &H8
Private Const FOF_NOCONFIRMATION = &H10
Private Const FOF_WANTMAPPINGHANDLE = &H20
Private Const FOF_ALLOWUNDO = &H40
Private Const FOF_FILESONLY = &H80
Private Const FOF_SIMPLEPROGRESS = &H100
Private Const FOF_NOCONFIRMMKDIR = &H200

Private Type SHFILEOPSTRUCT
        hwnd As Long
        wFunc As Long
        pFrom As String
        pTo As String
        fFlags As Integer
        fAnyOperationsAborted As Long
        hNameMappings As Long
        lpszProgressTitle As String ' only used if FOF_SIMPLEPROGRESS
End Type

Public Sub Recycle(ByVal strPath As String)
    Dim CFileStruct As SHFILEOPSTRUCT

    With CFileStruct
        .hwnd = 0 '
        .fFlags = FOF_ALLOWUNDO '+ FOF_NOCONFIRMATION
        .pFrom = strPath
        .wFunc = FO_DELETE
    End With

    If SHFileOperation(CFileStruct) <> ERROR_SUCCESS Then
        'An error occurred.
    End If
End Sub

Private Sub Command1_Click()
   Recycle "g:\ark\test"
End Sub
0
 
jahboiteCommented:
"Just delete the whole folder and then recreate it:"

It's a winner and so astonishingly simple!  Every day's a school day...
0
 
ArkCommented:
To deltete All in foldewr except folder use code above as

Private Sub Command1_Click()
   Recycle "c:\bogus\*.*"
End Sub

0
 
mlcktmguyAuthor Commented:
First of all, I made a mistake.  i wanted to split the points between the first two responders but awarded all of the points to the 2nd answer.  How can I have EE re-opne the question to rectify this situation?

Secondly, I accepted this method rather than deleting and recreating the \Tempwork directory because I can't assume that my user has authority to add a directory.  The app runs in some very restrictive environments.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Post a request in Community Support to have the question reopened:
http://www.experts-exchange.com/Community_Support/General/

Once open again, here are the instructions on how to Split points:
http://www.experts-exchange.com/help.jsp#hi69
0
 
mlcktmguyAuthor Commented:
Points doubled for the purpose of splitting
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.