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
12 1836
@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:
to rename or move folder/files you can use -
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
to know whichone is "older than 30 days"
go search google the property Time modify of folder
then you can use -
dim DeathDate as integer
-
-
DeathDate = Day(Now()) - day(Time modify of folder)
-
if deathDate >=30 then
-
'code to kill/rename/move subfolder
-
end if
-
and you have to loop all thing inside main folder. -
for i = 1 to MaxItemInFolder
-
'Code to determine which subfolder can be delete having deathDate >=30
-
Next
-
NeoPa 32,556
Expert Mod 16PB
Good answer, but I believe Kill() will fail if used on any folder that isn't empty.
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 - Sub Clear_All_Files_And_SubFolders_In_Folder()
-
'Delete all files and subfolders
-
'Be sure that no file is open in the folder
-
Dim FSO As Object
-
Dim MyPath As String
-
-
Set FSO = CreateObject("scripting.filesystemobject")
-
-
MyPath = "C:\Users\Ron\Test" '<< Change
-
-
If Right(MyPath, 1) = "\" Then
-
MyPath = Left(MyPath, Len(MyPath) - 1)
-
End If
-
-
If FSO.FolderExists(MyPath) = False Then
-
MsgBox MyPath & " doesn't exist"
-
Exit Sub
-
End If
-
-
On Error Resume Next
-
'Delete files
-
FSO.deletefile MyPath & "\*.*", True
-
'Delete subfolders
-
FSO.deletefolder MyPath & "\*.*", True
-
On Error GoTo 0
-
-
End Sub
Hishow still need to modify this code with some additional code that I suggest in post #2 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.
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.
@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)
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.
Like this: -
Dim strBasePath As String
-
Dim strFolder As String
-
Dim strFolderToFind As String
-
-
strBasePath = "C:\Users\Public\SEP\"
-
strFolderToFind = "Ade*"
-
-
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
-
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.
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.
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 ;)
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 };-)
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)...
|
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,
...
|
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,
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: ricky411 |
last post by:
Hi
columns:msg_t,usr_id,process_name
|
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: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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,...
| |