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: - Private Sub cmdDeleteVideo_Click()
-
-
'deletes folder directory and all contents
-
Dim FileSys, strDir As String
-
Set FileSys = CreateObject("Scripting.FileSystemObject")
-
strDir = "C:\Users\Michael Hill\Desktop\V\Preserved\Video123"
-
-
If FileSys.FolderExists(strDir) Then
-
If FileDateTime(strDir) < Now() - 30 Then
-
FileSys.DeleteFolder strDir
-
End If
-
End If
-
-
End Sub
This code will only delete the folder directory "Video123" from the parent path, which is a test location.
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: -
Dim strBasePath As String
-
Dim strFolder As String
-
Dim strFolderToFind As String
-
-
strBasePath = "C:\Users\Michael Hill\Desktop\V\Preserved\"
-
strFolderToFind = "Video12*"
-
-
strFolder = Dir(strBasePath, vbDirectory)
-
-
Do While strFolder <> "" ' Start the loop.
-
'Ignore the current directory and the encompassing directory.
-
If strFolder <> "." And strFolder <> ".." Then
-
'Use bitwise comparison to make sure strFolderToFind is a directory.
-
If (GetAttr(strBasePath & strFolder) And vbDirectory) = vbDirectory Then 'a Folder
-
If strFolder Like strFolderToFind Then '1st Criteria met
-
If FileDateTime(strBasePath & strFolder) < Now() - 30 Then '2nd criteria met
-
'Debug.Print strBasePath & strFolder
-
Kill strBasePath & strFolder & "\*" 'Must Delete Files first
-
RmDir strBasePath & strFolder
-
End If
-
End If
-
End If
-
End If
-
strFolder = Dir ' Get next entry.
-
Loop
-
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: - Check for the existence of any Files prior to executing Kill.
- Preface the Code Block with:
- It probably should be converted to a Function with the Base Path and Criteria passed to it as Arguments.
- What I like about the Code Logic is that there are no External References like File Scripting Runtime, it is all intrinsic.
4 6910
Here is some Code that I threw together that will search for a Folder within a Base Path, and if found, Delete it. -
Dim strBasePath As String
-
Dim strFolder As String
-
Dim strFolderToFind As String
-
-
strBasePath = "C:\Test\"
-
strFolderToFind = "Video123"
-
-
strFolder = Dir(strBasePath, vbDirectory)
-
-
Do While strFolder <> "" ' Start the loop.
-
'Ignore the current directory and the encompassing directory.
-
If strFolder <> "." And strFolder <> ".." Then
-
'Use bitwise comparison to make sure strFolderToFind is a directory.
-
If (GetAttr(strBasePath & strFolder) And vbDirectory) = vbDirectory Then
-
If strFolder = strFolderToFind Then
-
RmDir strBasePath & strFolder
-
End If
-
End If
-
End If
-
strFolder = Dir ' Get next entry.
-
Loop
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.
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: -
Dim strBasePath As String
-
Dim strFolder As String
-
Dim strFolderToFind As String
-
-
strBasePath = "C:\Users\Michael Hill\Desktop\V\Preserved\"
-
strFolderToFind = "Video12*"
-
-
strFolder = Dir(strBasePath, vbDirectory)
-
-
Do While strFolder <> "" ' Start the loop.
-
'Ignore the current directory and the encompassing directory.
-
If strFolder <> "." And strFolder <> ".." Then
-
'Use bitwise comparison to make sure strFolderToFind is a directory.
-
If (GetAttr(strBasePath & strFolder) And vbDirectory) = vbDirectory Then 'a Folder
-
If strFolder Like strFolderToFind Then '1st Criteria met
-
If FileDateTime(strBasePath & strFolder) < Now() - 30 Then '2nd criteria met
-
'Debug.Print strBasePath & strFolder
-
Kill strBasePath & strFolder & "\*" 'Must Delete Files first
-
RmDir strBasePath & strFolder
-
End If
-
End If
-
End If
-
End If
-
strFolder = Dir ' Get next entry.
-
Loop
-
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: - Check for the existence of any Files prior to executing Kill.
- Preface the Code Block with:
- It probably should be converted to a Function with the Base Path and Criteria passed to it as Arguments.
- What I like about the Code Logic is that there are no External References like File Scripting Runtime, it is all intrinsic.
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. - Private Sub cmdDeleteVideo_Click()
-
-
Dim FileSys, strBasePath, strFolder As String
-
Set FileSys = CreateObject("Scripting.FileSystemObject"): strBasePath = "C:\Users\Michael Hill\Desktop\V\Preserved\": strFolder = dir(strBasePath, vbDirectory)
-
-
Do While strFolder <> ""
-
If strFolder <> "." And strFolder <> ".." Then
-
If (GetAttr(strBasePath & strFolder) And vbDirectory) = vbDirectory Then
-
If FileSys.folderexists(strBasePath & strFolder) Then
-
If FileDateTime(strBasePath & strFolder) < Now() -30 Then
-
If strFolder Like "*" Then
-
If strFolder Like "*[!0-9]*" Then
-
Else
-
FileSys.deletefolder strBasePath & strFolder
-
End If
-
If strFolder Like "* - Copy" Then
-
FileSys.deletefolder strBasePath & strFolder
-
ElseIf strFolder Like "*p" Then
-
FileSys.deletefolder strBasePath & strFolder
-
End If
-
End If
-
End If
-
End If
-
End If
-
End If
-
strFolder = dir
-
Loop
-
-
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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(...)...
|
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...
|
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...
|
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)
...
|
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
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
| |