We help IT Professionals succeed at work.

auto delete files by file extension location and date range script

mwwebb asked
Im pretty new to window scripting, but im a decent VB coder. Im trying to create a .vbs script that i can link to task manager and run periodically. (once a week maybe) I need a script that can automatically delete log files from a specific directory on my server, with a specific file type and have it delete files that are older then 3 weeks or 21 days. The automatic deletion part of this project can be accomplished by using the script and linking to task manager.

Attached ive included a snippet of code ive found that is supposed to be able to do this. Being new to scripting i havent been able to figure out where to mod this code to make it do what i need. This snippet doesnt actually delete the files, its supposed to just show me the file names in an 'OkOnly' box. I dont want to actually have it delete the files prior to knowing it runs correctly.  

1.) The parts i cant figure out so far, is how do i single out the directory i want to script to analyze.
2.) The part about the date range is static, i need dynamic. This snippet has a variable for date (strDate) and the formula is CreationDate < strDate.  I need something more like, Creation date < date-21 (current date, i think the syntax is 'date').

Thanks in advance.
strDate = "20031102000000.000000+000"
strComputer = "."
Set objWMIService = GetObject _
    ("winmgmts:\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery _
    ("Select * From CIM_DataFile Where CreationDate < '" & strDate & "'" & _
        " AND Extension = 'doc'")
For Each objFile in colFiles
    Wscript.Echo objFile.Name

Open in new window

Watch Question

You might be better off using this article as a starting point:

Replace "C:\Scripts" with the desired directory and set the date range in the dtmDate variable.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.