Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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 IfstrMonth = Month(dtmDate)
If Len(strMonth) < 2 Then
strMonth = "0" & strMonth
End IfstrYear = 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

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?

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)

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.

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!

![]() |
![]() |
![]() |

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