473,396 Members | 1,858 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,396 software developers and data experts.

Deleting subfolders older than 30 days in the main Folder

14
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 1836
hvsummer
215 128KB
@Hishow:
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"
  2.  
to rename or move folder/files you can use
Expand|Select|Wrap|Line Numbers
  1. Name firstPathName SecondPathName
  2.  
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")
  2.  
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
  2.  
  3. DeathDate = Day(Now()) - day(Time modify of folder) 
  4. if deathDate >=30 then
  5.   'code to kill/rename/move subfolder
  6. end if
  7.  
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
  4.  
Dec 26 '15 #2
NeoPa
32,556 Expert Mod 16PB
Good answer, but I believe Kill() will fail if used on any folder that isn't empty.
Dec 27 '15 #3
hvsummer
215 128KB
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
  6.  
  7.     Set FSO = CreateObject("scripting.filesystemobject")
  8.  
  9.     MyPath = "C:\Users\Ron\Test"  '<< Change
  10.  
  11.     If Right(MyPath, 1) = "\" Then
  12.         MyPath = Left(MyPath, Len(MyPath) - 1)
  13.     End If
  14.  
  15.     If FSO.FolderExists(MyPath) = False Then
  16.         MsgBox MyPath & " doesn't exist"
  17.         Exit Sub
  18.     End If
  19.  
  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
  26.  
  27. End Sub
Hishow still need to modify this code with some additional code that I suggest in post #2
Dec 27 '15 #4
zmbd
5,501 Expert Mod 4TB
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
Hishow
14
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
hvsummer
215 128KB
@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
Hishow
14
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
Hishow
14
Like this:

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\Public\SEP\"
  6. strFolderToFind = "Ade*"
  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.  
Dec 29 '15 #9
Hishow
14
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
zmbd
5,501 Expert Mod 4TB
line 18/19 could be replaced by
RmDir strBasePath & strFolder & " /s /q"
for windows-xp and newer win-os installations; however, as written, the code is a bit more foolproof against those legacy installs.
Dec 29 '15 #11
hvsummer
215 128KB
that RmDIR() similar to DOS command that supprising me ==

didn't know some command like that still valid :D
even the FileDateTime()
and GetAttr()
we can even upgrade code to make it delete or not delete the hidden sub folder :D

it's not really I concern too much, actually I'm interesting in making new code to solve new problem, and will be happy to see new code born by anyone :D I like creative ;)
Dec 30 '15 #12
zmbd
5,501 Expert Mod 4TB
most of the old DOS/IBMDOS-IO commands are still valid.
Some like RD/RmDiR have additional functionality in the newer versions of the OS

batch-files good };-)
Dec 30 '15 #13

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

Similar topics

9
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)...
2
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, ...
5
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, ...
6
Zerin
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...
0
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.
0
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...
0
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...
6
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...
0
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...
3
by: ricky411 | last post by:
Hi columns:msg_t,usr_id,process_name
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.