473,554 Members | 2,169 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Deleting subfolders older than 30 days in the main Folder

14 New Member
Dear Experts, please I need help. A vba codes that can delete subfolders older than 30 days in the main Folder. I do not have much experience on vba. Please help me.

Thank you
Dec 26 '15 #1
12 1853
215 New Member
welcome to Bytes.com
you can't call for the directly code as solution.
we come here to ask how to do that -- not how you guy do that ==

to delete folders in vba you can use:
Expand|Select|Wrap|Line Numbers
  1. kill "fullpathhere"
to rename or move folder/files you can use
Expand|Select|Wrap|Line Numbers
  1. Name firstPathName SecondPathName
if the second name is different from first one, file/folder will rename to that name.

you can get information of subfolder, file inside folder with
Expand|Select|Wrap|Line Numbers
  1. Dir("Path")
to know whichone is "older than 30 days"
go search google the property Time modify of folder
then you can use
Expand|Select|Wrap|Line Numbers
  1. dim DeathDate as integer
  3. DeathDate = Day(Now()) - day(Time modify of folder) 
  4. if deathDate >=30 then
  5.   'code to kill/rename/move subfolder
  6. end if
and you have to loop all thing inside main folder.
Expand|Select|Wrap|Line Numbers
  1. for i = 1 to MaxItemInFolder
  2.   'Code to determine which subfolder can be delete having deathDate >=30
  3. Next
Dec 26 '15 #2
32,564 Recognized Expert Moderator MVP
Good answer, but I believe Kill() will fail if used on any folder that isn't empty.
Dec 27 '15 #3
215 New Member
I think rondebruin have better example here in this link
Delete files and folders
this code copy from that link that clear everything in main folder without condition >=30 days
Expand|Select|Wrap|Line Numbers
  1. Sub Clear_All_Files_And_SubFolders_In_Folder()
  2. 'Delete all files and subfolders
  3. 'Be sure that no file is open in the folder
  4.     Dim FSO As Object
  5.     Dim MyPath As String
  7.     Set FSO = CreateObject("scripting.filesystemobject")
  9.     MyPath = "C:\Users\Ron\Test"  '<< Change
  11.     If Right(MyPath, 1) = "\" Then
  12.         MyPath = Left(MyPath, Len(MyPath) - 1)
  13.     End If
  15.     If FSO.FolderExists(MyPath) = False Then
  16.         MsgBox MyPath & " doesn't exist"
  17.         Exit Sub
  18.     End If
  20.     On Error Resume Next
  21.     'Delete files
  22.     FSO.deletefile MyPath & "\*.*", True
  23.     'Delete subfolders
  24.     FSO.deletefolder MyPath & "\*.*", True
  25.     On Error GoTo 0
  27. End Sub
Hishow still need to modify this code with some additional code that I suggest in post #2
Dec 27 '15 #4
5,501 Recognized Expert Moderator Expert
actually, if OP had searched here on Bytes the following answer by our own expert ADezii would have turned up:
Find and Delete Directory Folders>Post#4
This code can be easily modified to meet Hishow's requirements and to quote ADezii
What I like about the Code Logic is that there are no External References like File Scripting Runtime, it is all intrinsic.
Dec 27 '15 #5
14 New Member
Thank you all for helping me. I have tried this from hvsummer even before this request. And also the reference made by zmbd view weeks ago. I didn't get results. Though I would keep trying.
Dec 29 '15 #6
215 New Member
@Hishow: can you post your currently code ?
and btw, what error and why you can't get your dream results ?
please give us more detail, we can help you to solve that (most of case, we can solve all problem without limit except limit from hardware or software itself)
Dec 29 '15 #7
14 New Member
Got it. Thank you so much for all your contributions. And I must say another thank you to zmbd for taking me back to ADezii post 5. My subfolders look like these, Ade2015-12-20, Ade2015-12-21... So I changed line 6 of "Video12*" to "Ade*". And I used my own directory. And it worked perfectly.

May Almighty Allah continue to increase the wisdom and knowledge of all of you.
Dec 29 '15 #8
14 New Member
Like this:

Expand|Select|Wrap|Line Numbers
  1. Dim strBasePath As String
  2. Dim strFolder As String
  3. Dim strFolderToFind As String
  5. strBasePath = "C:\Users\Public\SEP\"
  6. strFolderToFind = "Ade*"
  8. strFolder = Dir(strBasePath, vbDirectory)
  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
Dec 29 '15 #9
14 New Member
It's actually post 4 of ADezii not post 5. Thank you all for your concern. And also thanks to hvsummer for your much concern.

I am happy.
Dec 29 '15 #10

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

Similar topics

by: Paul | last post by:
I'm trying to make get my app to delete all the files in a specified folder and all the files within the folders of the specified folder. e.g. Folder 1 contains three files (File1, File2, File3) and two folders (Subfolder 1, Subfolder 2). .......I need to delete File1, File2, File3. Subfolder 1 contains FileA. .......Need to delete FileA....
by: Meshuggah | last post by:
Hey All, Could somebody please tell me how to delete all files inside a folder using Visual Basic .NET 2003? (I know this may be basic, but forgive me, im newbie) Thanks in advance, 'Meshuggah'
by: Meshuggah | last post by:
Hey All, Could somebody please tell me how to delete all files inside a folder using Visual Basic .NET 2003? (I know this may be basic, but forgive me, im newbie) Thanks in advance, 'Meshuggah'
by: Zerin | last post by:
Hi guys, I started "autopurging" discussion many days ago.As I couldn't do it,so I decided to change my database into SQL Server.And so posted this problem here. Plain and simple ------ I have a database named TANI and it has a table named "TANI" as well. Table TANI has 3 fields only ------ 1. CSV FILES that holds a link...
by: gomzi | last post by:
I have some images in a folder. i wish to delete them through vb. could anyone plz tell me as to how i should go about it.
by: rahuldhammy | last post by:
I am implementing a personalization(customizing the contents of the web site according to the user choice)in asp.net web site.For this i have to delete modify some files in my App_Themes folder.I am trying to do this with my c# code.But i always get some exception while deleting the files from the App-Themes folder.Even if the files are deleted it...
by: deve8ore | last post by:
Hi.... I'm getting closer to understanding the wildcard function in VBA, however we have a vendor that will supply us many files, and unfortunately will place them in different folders with no uniformity. I'd like to have the capability to find the file based on name, whether it's in a main folder, or one of the subfolders. I.e. - File...
by: deve8ore | last post by:
Hello, We have a vendor that will supply us many files, and unfortunately will place them in different folders with no uniformity (within Windows Explorer). I'd like to have the capability to find and open the file based on name, whether it's in a main folder, or one of the subfolders. I could use a wildcard based on the file I'm trying to...
by: Arulmanoj | last post by:
Hi, I don't know whether it is the right place to ask this question. But I am facing a problem. I need write a batch file which will delete files which are 5 days older from current system date in the given folder and in it's sub folders also. I have written any batch file till now. Another question I have is, I have a VB Script which do the...
by: ricky411 | last post by:
Hi columns:msg_t,usr_id,process_name
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.