VBScript delete folders older than specified days
-
Tuesday, May 15, 2012 9:38 AM
I have a script that works fine with Windows XP but I can't get it to work with Windows 7. It keeps saying 'Delete_JavaCache.vbs(35, 5) (null): The specified network name is no longer available'.
I check and the folders are still there.
Dim i
Dim fso
Dim f
Dim f1
Dim sf
Dim BasePath
Dim CalcResult
Dim fNameArray()
Dim ApplicationData
Dim objshell
Dim Wshshell
Dim oShell
Dim fNameSet objShell = CreateObject("Shell.Application")
Set oShell = CreateObject("WScript.Shell")
ApplicationData = oShell.ExpandEnvironmentStrings("%APPDATA%")
BasePath = ApplicationData & "\Sun\Java\Deployment\cache\6.0"Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(BasePath)
Set sf = f.SubFolders
For Each f1 in sf
CalcResult = DateDiff("d",f1.DateCreated,Now)
if CalcResult > 1 then
ReDim preserve fNameArray(i)
fNameArray(i) = f1.Name
i = i + 1
end if
NextFor Each fName in fNameArray
FSO.DeleteFolder(BasePath & "\" & fName)
Next
All Replies
-
Tuesday, May 15, 2012 10:02 AMIt helps respondents when posters highlight the line that causes the problem so that they do not need to count lines. You report that line 35 causes a problem. Your posted code has only 32 lines . . .
-
Tuesday, May 15, 2012 10:11 AM
Sorry I excluded the comment at the top so line number should be 33 which is :
FSO.DeleteFolder(BasePath & "\" & fName)
Thanks
-
Tuesday, May 15, 2012 11:18 AM
Since you are using Windows 7 you could give this PowerShell script a try:
Delete files older than x-days - Cleanup Script
Jaap Brasser
http://www.jaapbrasser.com -
Tuesday, May 15, 2012 11:55 AM
Sorry I excluded the comment at the top so line number should be 33 which is :
FSO.DeleteFolder(BasePath & "\" & fName)
Thanks
Change your code like so in order to see what's going on. You must then run the script from a console, using cscript.exe.
wscript.echo BasePath & "\" & fName
FSO.DeleteFolder(BasePath & "\" & fName) -
Tuesday, May 15, 2012 1:21 PMModerator
That's a good troubleshooting technique. However, I note that the OPs approach is overly complicated. There really is no need to build the array of folder names and than execute a second loop. I'd do it something like this, instead ...
For Each f1 in sf CalcResult = DateDiff("d",f1.DateCreated,Now) if CalcResult > 1 then f1.delete true
Next
Tom Lavedas
- Edited by Tom LavedasModerator Tuesday, May 15, 2012 1:22 PM to correct delete syntax
-
Tuesday, May 15, 2012 1:33 PM
That's a good troubleshooting technique. However, I note that the OPs approach is overly complicated. There really is no need to build the array of folder names and than execute a second loop. I'd do it something like this, instead ...
For Each f1 in sf CalcResult = DateDiff("d",f1.DateCreated,Now) if CalcResult > 1 then f1.delete true
Next
Tom Lavedas
So would I! I would, in fact, go one step further:
For Each f1 in sf
if DateDiff("d",f1.DateCreated,Now) > 1 then f1.delete true
Next
But then it's a matter of taste how far one wants to drive simplification.
- Edited by OberwaldMicrosoft Community Contributor Tuesday, May 15, 2012 2:34 PM
- Marked As Answer by Bill_StewartMicrosoft Community Contributor, Moderator Thursday, May 17, 2012 5:20 PM
-
Tuesday, May 15, 2012 2:19 PMThank you. Changing the script to the simpler steps allowed it to work in Windows XP and 7, whereas before it would only run in XP.

