Computing.Net > Forums > Programming > WSH: Delete files in folder by date

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

WSH: Delete files in folder by date

Reply to Message Icon

Original Message
Name: skisalomon77
Date: February 28, 2008 at 12:20:50 Pacific
Subject: WSH: Delete files in folder by date
OS: XP Pro
CPU/Ram: Intel/2GB
Model/Manufacturer: Dell
Comment:

I need to delete files from the C:\Company\Temp directory that are older than 120 days. I have to perform this on about 125 PCs. The VBScript keeps bombing on line 27, character 1, where my objFile is setup. When I remove this line it tells me I need to use objFile. I'm missing something and need a little help.

dtmDate = Date - 120

strDay = Day(dtmDate)

If Len(strDay) < 2 Then
strDay = "0" & strDay
End If

strMonth = Month(dtmDate)

If Len(strMonth) < 2 Then
strMonth = "0" & strMonth
End If

strYear = Year(dtmDate)

strTargetDate = strYear & strMonth & strDay

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set FileList = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='C:\Company\Temp'} Where " _
& "ResultClass = CIM_DataFile")

For Each objFile in FileList
strDate = Left(objFile.CreationDate, 8)
If strDate < strTargetDate Then
objFile.Delete
End If
Next


Report Offensive Message For Removal


Response Number 1
Name: klint
Date: February 28, 2008 at 14:46:37 Pacific
Reply: (edit)

It looks all right to me. What error message are you getting?

When you say line 27, do you mean the line that contains the For Each statement?


Report Offensive Follow Up For Removal

Response Number 2
Name: skisalomon77
Date: February 29, 2008 at 07:23:40 Pacific
Reply: (edit)

Hey Klint...

Line 27 is

For Each objFile In FileList

The error is as follows:

Script: Obviously the path of my script
Line: 27
Char: 1
Error: 0x80041002
Code: 80041002
Source: (null)


Report Offensive Follow Up For Removal

Response Number 3
Name: klint
Date: February 29, 2008 at 09:17:20 Pacific
Reply: (edit)

If you just google for "80041002" you will find that this error code means object not found. In your case it relates to FileList, not objFile. There must be something wrong with your Set FileList = ... statement. It could be that the directory C:\Company\Temp doesn't exist. Try it first with C:\ and see if you get the same error.


Report Offensive Follow Up For Removal

Response Number 4
Name: skisalomon77
Date: February 29, 2008 at 11:59:06 Pacific
Reply: (edit)

You were right! I made my directory c:\ and the script worked fine. C:\Company\Temp didn't exist on the PC I was testing with. Man do I feel stupid LOL. The simplest things will get you everytime.

Thanks for your help Klint!


Report Offensive Follow Up For Removal







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home








Do you have your own blog?

Yes
No
I did before
I will soon


View Results

Poll Finishes In 4 Days.
Discuss in The Lounge
Poll History




Data Recovery Software