?
Solved

Erase Everything in a Directory

Posted on 2007-08-07
12
Medium Priority
?
181 Views
Last Modified: 2010-04-30
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.
0
Comment
Question by:mlcktmguy
  • 3
  • 3
  • 2
  • +2
11 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 19648975
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
 
LVL 1

Author Comment

by:mlcktmguy
ID: 19649035
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
 

Assisted Solution

by:seppert
seppert earned 500 total points
ID: 19649265
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 12

Expert Comment

by:jahboite
ID: 19649360
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 19650082
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
 
LVL 28

Expert Comment

by:Ark
ID: 19650103
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
 
LVL 12

Expert Comment

by:jahboite
ID: 19650116
"Just delete the whole folder and then recreate it:"

It's a winner and so astonishingly simple!  Every day's a school day...
0
 
LVL 28

Expert Comment

by:Ark
ID: 19650131
To deltete All in foldewr except folder use code above as

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

0
 
LVL 1

Author Comment

by:mlcktmguy
ID: 19651332
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 19651559
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
 
LVL 1

Author Comment

by:mlcktmguy
ID: 19654283
Points doubled for the purpose of splitting
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month16 days, 7 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question