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

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

WSH: Delete files in folder by date

Reply to Message Icon

Name: skisalomon77
Date: February 28, 2008 at 12:20:50 Pacific
OS: XP Pro
CPU/Ram: Intel/2GB
Product: 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



Sponsored Link
Ads by Google

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

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?


0

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

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)


0

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

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.


0

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

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!


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







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


Sponsored links

Ads by Google


Results for: WSH: Delete files in folder by date

batchRenaming files in Folder tree www.computing.net/answers/programming/batchrenaming-files-in-folder-tree-/18621.html

Batch to Delete files in a folder www.computing.net/answers/programming/batch-to-delete-files-in-a-folder/16299.html

Find oldest file in folder www.computing.net/answers/programming/find-oldest-file-in-folder/14755.html