Link to home
Start Free TrialLog in
Avatar of mlcktmguy
mlcktmguyFlag for United States of America

asked on

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.
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mlcktmguy

ASKER

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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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
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
"Just delete the whole folder and then recreate it:"

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

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

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.
Post a request in Community Support to have the question reopened:
https://www.experts-exchange.com/Community_Support/General/

Once open again, here are the instructions on how to Split points:
https://www.experts-exchange.com/help.jsp#hi69
Points doubled for the purpose of splitting