By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,165 Members | 849 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,165 IT Pros & Developers. It's quick & easy.

Find and Delete Directory Folders

P: 18
I have a folder structure where surveillance video is temporarily saved, then purged after 30 days (currently manually). Each "Video Case" is assigned a "Case Number" by my DB and the Folders in the structure are named with this same "Case Number." All documents and videos relating to the "Video Case" is saved in the same folder.

I'd like to automate this process with a cmdButton on my frmMenu. I've found VBA code where others have done the same with files of a particular extension such as *.jpg or *.doc; and others that have showed how to delete folders including contents but I'm struggling with how to find the folders name in order to delete it.

I'm sure I need to do a Loop of some kind, within which I can test whether the folder name returned meets my parameters (GetDateCreated) but I don't know how to do the looping.

It's not much, but here is my start:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdDeleteVideo_Click()
  2.  
  3. 'deletes folder directory and all contents
  4. Dim FileSys, strDir As String
  5. Set FileSys = CreateObject("Scripting.FileSystemObject")
  6. strDir = "C:\Users\Michael Hill\Desktop\V\Preserved\Video123"
  7.  
  8.     If FileSys.FolderExists(strDir) Then
  9.         If FileDateTime(strDir) < Now() - 30 Then
  10.             FileSys.DeleteFolder strDir
  11.         End If
  12.     End If
  13.  
  14. End Sub
This code will only delete the folder directory "Video123" from the parent path, which is a test location.
Nov 18 '13 #1

✓ answered by ADezii

The following example will DELETE all Sub-Folders under your Base Path of 'C:\Users\Michael Hill\Desktop\V\Preserved\' which are at least 30 days old and have Video12 prefixed in their Name (Video123, Video124,...Video129). This should definitely point you in the right direction. Pay close attention to Code Lines: 5, 6, 15-21:
Expand|Select|Wrap|Line Numbers
  1. Dim strBasePath As String
  2. Dim strFolder As String
  3. Dim strFolderToFind As String
  4.  
  5. strBasePath = "C:\Users\Michael Hill\Desktop\V\Preserved\"
  6. strFolderToFind = "Video12*"
  7.  
  8. strFolder = Dir(strBasePath, vbDirectory)
  9.  
  10. Do While strFolder <> ""    ' Start the loop.
  11.   'Ignore the current directory and the encompassing directory.
  12.     If strFolder <> "." And strFolder <> ".." Then
  13.      'Use bitwise comparison to make sure strFolderToFind is a directory.
  14.        If (GetAttr(strBasePath & strFolder) And vbDirectory) = vbDirectory Then  'a Folder
  15.          If strFolder Like strFolderToFind Then         '1st Criteria met
  16.            If FileDateTime(strBasePath & strFolder) < Now() - 30 Then    '2nd criteria met
  17.              'Debug.Print strBasePath & strFolder
  18.              Kill strBasePath & strFolder & "\*"    'Must Delete Files first
  19.              RmDir strBasePath & strFolder
  20.            End If
  21.          End If
  22.        End If
  23.     End If
  24.     strFolder = Dir    ' Get next entry.
  25. Loop
  26.  
P.S. - Be advised that the Folder Deletion(s) will be automatic with no warning. Should any Folder be Empty, and Error will be generated when executing the KILL Statement. Either:
  1. Check for the existence of any Files prior to executing Kill.
  2. Preface the Code Block with:
    Expand|Select|Wrap|Line Numbers
    1. On Error Resume Next
  3. It probably should be converted to a Function with the Base Path and Criteria passed to it as Arguments.
  4. What I like about the Code Logic is that there are no External References like File Scripting Runtime, it is all intrinsic.

Share this Question
Share on Google+
4 Replies


ADezii
Expert 5K+
P: 8,638
Here is some Code that I threw together that will search for a Folder within a Base Path, and if found, Delete it.
Expand|Select|Wrap|Line Numbers
  1. Dim strBasePath As String
  2. Dim strFolder As String
  3. Dim strFolderToFind As String
  4.  
  5. strBasePath = "C:\Test\"
  6. strFolderToFind = "Video123"
  7.  
  8. strFolder = Dir(strBasePath, vbDirectory)
  9.  
  10. Do While strFolder <> ""    ' Start the loop.
  11.   'Ignore the current directory and the encompassing directory.
  12.     If strFolder <> "." And strFolder <> ".." Then
  13.      'Use bitwise comparison to make sure strFolderToFind is a directory.
  14.        If (GetAttr(strBasePath & strFolder) And vbDirectory) = vbDirectory Then
  15.          If strFolder = strFolderToFind Then
  16.            RmDir strBasePath & strFolder
  17.          End If
  18.        End If
  19.     End If
  20.     strFolder = Dir    ' Get next entry.
  21. Loop
Nov 18 '13 #2

P: 18
Thank you for your reply.

To clarify, my base directory is "C:\Users\Michael Hill\Desktop\V\Preserved" (for testing purposes). There are many sub-folders within this directory, each of which contain surveillance video clips (.avi format/file extension, as well as other video types) not to mention any other documents pertaining to the video (word document form requesting the video, scanned documents in .pdf format....)

The example provided only listed one example ("Video123") but in reality there would be several: Video123, Video124, Video126... each of which represents the name of a sub-folder of the parent directory.

The code I had would already delete a specific folder of a known path. What I'm looking to do is search the base directory and delete each folder that is older than a specified period. I already know how to check the age of the folder being deleted. I have figured out how to work a Loop in other processing that remain within Access; I just don't understand yet how to apply that to searching for sub-folders within a specified base Windows directory... once I figure that part of this equation out, I think I'd be able to apply the logic to test whether the folder name returned meets the criteria to be deleted and then delete it or to continue on.

So, specifically what I need help with (or pointed in the right direction) is how to search the folder specified for sub-folders/sub-directories and then how to continue to the next folder. I'd like this to delete all folders that meet the criterias specified within a single execution, rather than a single deletion for each....which is where I believe the Loop (or something similar) is needed.

I'm using Windows 7 with MS Office Access 2007.

The code provided by ADezii is a start (Thank You) and I'll work with it some more tomorrow to see if I can get it to work.
Nov 18 '13 #3

ADezii
Expert 5K+
P: 8,638
The following example will DELETE all Sub-Folders under your Base Path of 'C:\Users\Michael Hill\Desktop\V\Preserved\' which are at least 30 days old and have Video12 prefixed in their Name (Video123, Video124,...Video129). This should definitely point you in the right direction. Pay close attention to Code Lines: 5, 6, 15-21:
Expand|Select|Wrap|Line Numbers
  1. Dim strBasePath As String
  2. Dim strFolder As String
  3. Dim strFolderToFind As String
  4.  
  5. strBasePath = "C:\Users\Michael Hill\Desktop\V\Preserved\"
  6. strFolderToFind = "Video12*"
  7.  
  8. strFolder = Dir(strBasePath, vbDirectory)
  9.  
  10. Do While strFolder <> ""    ' Start the loop.
  11.   'Ignore the current directory and the encompassing directory.
  12.     If strFolder <> "." And strFolder <> ".." Then
  13.      'Use bitwise comparison to make sure strFolderToFind is a directory.
  14.        If (GetAttr(strBasePath & strFolder) And vbDirectory) = vbDirectory Then  'a Folder
  15.          If strFolder Like strFolderToFind Then         '1st Criteria met
  16.            If FileDateTime(strBasePath & strFolder) < Now() - 30 Then    '2nd criteria met
  17.              'Debug.Print strBasePath & strFolder
  18.              Kill strBasePath & strFolder & "\*"    'Must Delete Files first
  19.              RmDir strBasePath & strFolder
  20.            End If
  21.          End If
  22.        End If
  23.     End If
  24.     strFolder = Dir    ' Get next entry.
  25. Loop
  26.  
P.S. - Be advised that the Folder Deletion(s) will be automatic with no warning. Should any Folder be Empty, and Error will be generated when executing the KILL Statement. Either:
  1. Check for the existence of any Files prior to executing Kill.
  2. Preface the Code Block with:
    Expand|Select|Wrap|Line Numbers
    1. On Error Resume Next
  3. It probably should be converted to a Function with the Base Path and Criteria passed to it as Arguments.
  4. What I like about the Code Logic is that there are no External References like File Scripting Runtime, it is all intrinsic.
Nov 18 '13 #4

P: 18
THANK YOU SO, SO MUCH!!!

Your original code was pointing me in the right direction, I just didn't understand it as well until your second response. This is likely primarily caused by my example not meeting my exact real situation. I modified the code slightly to get the results I was looking for; I changed what I was searching for to everything, then evaluated what was returned as I have a few specific requirements.


Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdDeleteVideo_Click()
  2.  
  3. Dim FileSys, strBasePath, strFolder As String
  4. Set FileSys = CreateObject("Scripting.FileSystemObject"): strBasePath = "C:\Users\Michael Hill\Desktop\V\Preserved\": strFolder = dir(strBasePath, vbDirectory)
  5.  
  6. Do While strFolder <> ""
  7.     If strFolder <> "." And strFolder <> ".." Then
  8.         If (GetAttr(strBasePath & strFolder) And vbDirectory) = vbDirectory Then
  9.             If FileSys.folderexists(strBasePath & strFolder) Then
  10.                 If FileDateTime(strBasePath & strFolder) < Now() -30 Then
  11.                     If strFolder Like "*" Then
  12.                         If strFolder Like "*[!0-9]*" Then
  13.                         Else
  14.                             FileSys.deletefolder strBasePath & strFolder
  15.                         End If
  16.                         If strFolder Like "* - Copy" Then
  17.                             FileSys.deletefolder strBasePath & strFolder
  18.                         ElseIf strFolder Like "*p" Then
  19.                             FileSys.deletefolder strBasePath & strFolder
  20.                         End If
  21.                      End If
  22.                 End If
  23.             End If
  24.         End If
  25.     End If
  26.         strFolder = dir
  27. Loop
  28.  
  29. End Sub
I used the FileSys.DeleteFolder instead of KILL, I found this to delete the folder and all contents even if it is found to be empty and without error generation.

I do still need to add an error handler.


Your help is very much appreciated.
Nov 18 '13 #5

Post your reply

Sign in to post your reply or Sign up for a free account.