[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1300
  • Last Modified:

auto delete files by file extension location and date range script

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
Next

Open in new window

0
mwwebb
Asked:
mwwebb
1 Solution
 
Shift-3Commented:
You might be better off using this article as a starting point:
http://www.microsoft.com/technet/scriptcenter/resources/qanda/nov06/hey1117.mspx

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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now