VBscript and WMI: VBscript to find files with specific extensions and creation date on remote servers

Posted March 21st, 2011 in files and tagged , , , , , , , by dirk adamsky

I recently got the question to find all Symantec BESR snaphot files older than 2 weeks (the retention period) on the company servers. This so that we could clear up some space on the snapshot volumes.
I decided to make a general purpose script of it.
You can use it to find old logfiles, etc.

What the script does:

  • create an array with all server you want to check (replace srv001, etc with your server names)
  • create a variable with a date of 2 weeks ago (Date -14)(can easily be changed in 30, 60 days ago)
  • change the variable date to the UTC date of 14 days ago (because WMI works with UTC dates)
  • for each server in the array
  • make a wmi connection
  • query for files with specific extensions and a creation date older than 14 days ago
  • in this example i took .txt and .csv extensions, change them in whatever filetype you want

The script is tested in an win2003 environment.

Follow the next steps to run the script (admin rights needed):

  • copy and paste the script in your favorite text editor
  • replace the strings ‘srv001′ and ‘srv002′ with the name of your exchange server
  • replace the extensions ‘txt’ and ‘csv with the extensions you need
  • save the script (for example c:tempfindoldfileswithspecificextensions.vbs)(or something shorter..)
  • open a command prompt
  • go to “c:temp”
  • give “cscript findoldfileswithspecificextensions.vbs” (without quotes) and enter

When you want the output in a file please give this command:

“cscript findoldfileswithspecificextensions.vbs > findoldfileswithspecificextensions.txt” (again without the quotes)

The script:

' Name : findoldfileswithspecificextensions.vbs
' Description : VBscript to find files with specific extensions and creation date on remote servers
' Author : dirk adamsky - deludi bv
' Version : 1.00
' Date : 21-03-2011
' Level: intermediate

arrServers = Array("srv001","srv002")
strDate = Date - 14
Set objDateToUtcDate = CreateObject("WbemScripting.SWbemDateTime")

For Each Server in arrServers
	Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\" & Server & "rootcimv2")
	Set colFiles = objWMIService.ExecQuery("Select * from CIM_DataFile where (Extension = 'txt' or Extension = 'csv') and CreationDate < '" & objDateToUtcDate & "'")
	For Each objFile in colFiles
		Wscript.Echo objFile.Name
	Set colFiles = Nothing
	Set objWMIService = Nothing
Set objDateToUtcDate = Nothing

When you have problems/questions please post a reply or give a ‘star’ rating.

Happy scripting.

Best regards,

Dirk Adamsky – Deludi BV

One Response so far.

  1. DEVESH SHUKLA says:

    Thanks mate,

    I modified some steps and created a new one which i required. This script is taking input for multiple machine’s from a text file and providing result in a text file. I need your help to fix a bug in my script. My script is skiping those machine which don’t have access from that account which executed the script. I need error detection method which clearly adding a line againt failure access machine instead of skiping it with blank result.

    ‘ Name : FileSearch.vbs
    ‘ Description : VBscript to find files with specific extensions on remote servers

    On Error Resume Next

    Set fso = CreateObject(“Scripting.FileSystemObject”)
    Set objInputFile = fso.OpenTextFile(“serverlist.txt”, 1, True)
    Set objOutputFile = fso.OpenTextFile(“Results.txt”, 2, True)

    Do While objInputFile.AtEndOfLine True

    strComputer = objInputFile.ReadLine

    If Ping(strComputer) Then
    ‘strComputer = “.”
    Set objWMIService = GetObject(“winmgmts:” _
    & “{impersonationLevel=impersonate}!\\” & strComputer & “\root\cimv2″)

    Set colFiles = objWMIService.ExecQuery(“Select * from CIM_Datafile where (Extension = ‘txt’ or Extension = ‘ini’)”)

    For Each objFile in colFiles
    objOutputFile.WriteLine strComputer & vbTab & objFile.Name
    ObjOutputFile.WriteLine “Server Name: ” & strComputer & ” No_PING”

    End If
    ObjOutputFile.WriteLine “==================”
    Function Ping(Target)
    Dim results

    On Error Resume Next

    Set shell = CreateObject(“WScript.Shell”)

    Set exec = shell.Exec(“ping -n 1 -w 2000 ” & Target)
    results = LCase(exec.StdOut.ReadAll)

    Ping = (InStr(results, “reply from”) > 0)
    End Function

Leave a Reply