/build/static/layout/Breadcrumb_cap_w.png

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


'* 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)

Posted by: andemats 12 years ago
2nd Degree Black Belt
0
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
Posted by: danananura 12 years ago
Yellow Belt
0
Thanks andemats

I have added a Left function as follows, but that didnt worked :(


If (Left(Cstr(oFolder), 3)) = "201" Then

If oFolder.DateLastModified < (Date() - iDaysOld) Then
oFolder.Delete(True)
End If
Posted by: andemats 12 years ago
2nd Degree Black Belt
0
You've got two "If's" but only one "End If" what I can see.
You might have the second End If in the rest of the code.

What errors does the script give?
Posted by: danananura 12 years ago
Yellow Belt
0
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

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
Red Belt
0
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.: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
2nd Degree Black Belt
0
You might want to remove the "On Error Resume Next" to get some errors.

EDIT: Ah! Good, Ian found this and sorted it out.
Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.
 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ