473,395 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Find and Delete Directory Folders

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.

4 6910
ADezii
8,834 Expert 8TB
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
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
8,834 Expert 8TB
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
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

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

Similar topics

3
by: Naresh Agarwal | last post by:
Hi I'm developing a server in Java. I need to read/write certain files in my server code, for which I need the exact location of files. In C++, normally this is achieved by having a system...
6
by: gl | last post by:
I'm trying to delete a directory that contains readonly files. Is there any easy way to do this? I get a System.UnauthorizedAccessException when a read only file is encountered. Is there a way to...
2
by: Cesar Ronchese | last post by:
Hello, I'm experiencing a very weird problem. I have a ASP.Net 2005 application (VB.Net) that creates some folders to store temporary files. example: Session_Start(...)...
0
by: Altman | last post by:
I am getting an error while trying to run a crystal report in asp.net 1.1 on a production server. On my test server I am only able to get the same error to occur by removing security rights on the...
11
by: frogman7 | last post by:
what i am trying to do is something similar to the search that windows does to find files and folders. I am putting a start directory and want to find all the file (including the files in the sub...
7
by: Anil Gupte | last post by:
Private Sub mnu2Exit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnu2Exit.Click Dim fDir As String = Path.GetDirectoryName(L3Global.VideoFileName) ...
3
by: bskrishnan | last post by:
I Need Code For Retrieving Folders And Sub Folders Usng Python. May Be According To A Specific Condition May Be Say Folder With Names Www Etc.. Please Help
3
by: Steph | last post by:
hello, i ve a probleme when deleting a directory and when i want create file immediatly after. 1) Directory.Delete(myPath, true); 2) TextWriter sw = new StreamWriter(myPath +"test.aspx"); i...
5
by: mvelasquez | last post by:
I'm working with crystal reports in my windows app. I have a pre-existing report that I will access dynamically in the application at run time. I set the parameters of the report through the...
0
TheSmileyCoder
by: TheSmileyCoder | last post by:
It can often be usefull to know what the path is to the users desktop, or his/her My Documents folder. The problem is that these folder paths can be changed by the user. However windows comes with...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.