Require VBScript to delete folder based on folder name
Hello friends,
I have a folder name test which is under D:\ (D:\Test). It has several subfolder like logs, sample, generator, 20110101, 20110102, 20120109 etc…).
I need a VBscript which will delete only the folder which are having folder name starting with 2011 and 2012 and also these folders must be older than 30 days.
I have a script which will delete all the subfolders which have older 30 days. I need to modify the script below so that only subfolders with name starting 2011 or 2012 are deleted
Could you guys please help me to this modify. I am new to VBscripting
I have a folder name test which is under D:\ (D:\Test). It has several subfolder like logs, sample, generator, 20110101, 20110102, 20120109 etc…).
I need a VBscript which will delete only the folder which are having folder name starting with 2011 and 2012 and also these folders must be older than 30 days.
I have a script which will delete all the subfolders which have older 30 days. I need to modify the script below so that only subfolders with name starting 2011 or 2012 are deleted
'* Script Name: DeleteOldFiles.vbs
'* Purpose: Delete folders older than x days
'Account used to run script needs delete permissions to folder & files.
'Set the following variables
FolderPath = "D:\Test"
NumberOfDays = 30 'anything older than this many days will be removed
'Set objects & error catching
Dim fso
Dim objSubfolder
Set fso = CreateObject("Scripting.FileSystemObject")
Set objFolder = fso.GetFolder(FolderPath)
'DELETE all subfolders in FolderPath Path older than x days
For Each objSubfolder In objFolder.Subfolders
If DateDiff("d", objSubfolder.DateCreated,Now) > NumberOfDays Then
objSubfolder.Delete True
End if
Next
Could you guys please help me to this modify. I am new to VBscripting
0 Comments
[ + ] Show comments
Answers (6)
Please log in to answer
Posted by:
andemats
12 years ago
I'm not a scripting ace, but I think I'd go for the "Left" function to sort out the folders you want to delete.
http://www.devguru.com/technologies/vbscript/quickref/left.html
http://www.devguru.com/technologies/vbscript/quickref/left.html
Posted by:
danananura
12 years ago
Posted by:
andemats
12 years ago
Posted by:
danananura
12 years ago
Thanks for the reply
Following is the script I have tried. It does'nt give me any error. But the script is not deleting the folders as required
Following is the script I have tried. It does'nt give me any error. But the script is not deleting the folders as required
Option Explicit
On Error Resume Next
Dim oFSO, oFolder, sDirectoryPath
Dim oFileCollection, oFile, sDir
Dim iDaysOld
' Specify Directory Path From Where You want to clear the old files
sDirectoryPath = "XXX"
' Specify Number of Days Old File to Delete
iDaysOld = 30
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(sDirectoryPath)
Set oFileCollection = oFolder.Files
For each oFile in oFileCollection
If (Left(Cstr(oFolder), 3)) = "201" Then
If oFolder.DateLastModified < (Date() - iDaysOld) Then
oFolder.Delete(True)
End If
End If
Next
Set oFSO = Nothing
Set oFolder = Nothing
Set oFileCollection = Nothing
Set oFile = Nothing
Posted by:
anonymous_9363
12 years ago
First of all, you need to clarify exactly what you want the script to do because you refer to deleting folders but you're looping through the Files collection, then checking the .DateLastModified property of the containing folder! That will obviously always be the same for every file in the collection.
Also:
- you (and others) will find your code easier to maintain if you indent it, e.g.:
- If you're going to turn off error handling (On Error Resume Next), then your code needs to check for any errors returned (If Err.Number <> 0 Then...). Otherwise, the code will do exactly what you have asked - it just Resumes execution at the Next statement.
Also:
- you (and others) will find your code easier to maintain if you indent it, e.g.:
For each oFile in oFileCollection
If (Left(oFolder.Path), 3) = "201" Then
If oFolder.DateLastModified < (Date() - iDaysOld) Then
oFolder.Delete(True)
End If
End If
Next
- I always use DateDiff for comparing dates (If DateDiff("d", oFolder.DateLastModified, Date) >= iDaysOld Then)- If you're going to turn off error handling (On Error Resume Next), then your code needs to check for any errors returned (If Err.Number <> 0 Then...). Otherwise, the code will do exactly what you have asked - it just Resumes execution at the Next statement.
Posted by:
andemats
12 years ago
Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.
so that the conversation will remain readable.