Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to find files by create date and extension mask server array

Status
Not open for further replies.

Frozzle

IS-IT--Management
May 12, 2011
3
US
Hi, I am trying to find files using an array of server names
I want to display only files with create date of today
I want to only retrieve files with extensions dat or log

I am just a novice, so please provide generous how's. This is what I have put together so far.

arrServers = Array("10.32.44.192","10.32.44.193","10.32.44.194") ' server array
strDate = Date - 0 'file date selection
Set objDateToUtcDate = CreateObject("WbemScripting.SWbemDateTime") 'set date object
objDateToUtcDate.SetVarDate(strDate) 'set date var
For Each Server in arrServers ' for loop, 1 iteration per server
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & Server & "\root\cimv2") 'Set COM impersonation allowing access
Set colFiles = objWMIService.ExecQuery("Select * from CIM_DataFile where (Extension = 'log' or Extension = 'dat') and CreationDate < '" & objDateToUtcDate & "'") ' searching by the masks/date
For Each objFile in colFiles
Wscript.Echo objFile.Name 'show me the money
Next
Set colFiles = Nothing 'cleanup
Set objWMIService = Nothing 'cleanup
Next
Set objDateToUtcDate = Nothing 'cleanup
 
I have not seen this method before. Does it not work? What is your question?

-Geates

"I hope I can feel and see the change - stop the bleed inside a feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
This code works well when searching the local machine. It's slower when accessing remote machines. And may crawl trying to iterate entire remote file systems.

Code:
arrServers = Array("10.32.44.192","10.32.44.193","10.32.44.194") 

set objFSO = CreateObject("Scripting.FileSystemObject")
set objShell = CreateObject("WScript.Shell")

function searchDir(strDir, strExt)
	set objFolder = objFSO.GetFolder(strDir)
	for each objSubFolder in objFolder.SubFolders
		strResults = strResults & searchDir(objSubFolder.Path, strQuery)
	next
	
	for each objFile in objFolder.Files
		[green]'Does the objFile.Name have the correct file extension[/green]
		if (right(objFile.Name, 4) = strExt) then 
			[green]'Is there a difference in days between the dates of objFile and now?[/green]
			if (datediff("d", objFile.DateCreated, now) = 0) then
				strResults = strResults & objFile.Name & vbNewLine
			end if
		end if
	next
	searchDir = strResults
end function

for i = 0 to ubound(arrServers) - 1
	strLocation = "\\" & arrServers(i) & "\c$"
	strFiles = searchDir(strLocation, ".vbs")
	set objOutput = objFSO.OpenTextFile("C:\temp\" & arrServers(i) & ".txt", 2, true, 0)
	objOutput.WriteLine strFiles
	objOutput.Close
next

-Geates


"I hope I can feel and see the change - stop the bleed inside a feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Thanks Geates, this is super. To answer your comment questions, I am want to retrieve all filenames with extensions of 3 chars, the one above I suggested 4 for .html files in testing but in live I needed three.

There is no difference in days between the objFile and now in my scenario, it is designed this way so I can copy it to multiple versions used that are used on weekdays, then Monday's when I need to list files from Saturday and Sunday.

Thank you again.
 
Not sure if you asked a question or made comments. I'll assume it's a question.

I am want to retrieve all filenames with extensions of 3 chars, the one above I suggested 4 for .html files in testing but in live I needed three.

make the qualifier 'dynamic' so you can't run into issues with different length extensions.
Code:
if (right(objFile.Name, [green]len(strExt)[/green]) = strExt) then

-Geates

"I hope I can feel and see the change - stop the bleed inside a feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Or you could use the GetExtensionName method to return the extension: objFSO.GetExtensionName(objFile).

Either method you use, I would suggest converting to all uppercase or all lowercase before comparing the file extension text. Something like:
Code:
If LCase(objFSO.GetExtensionName(objFile)) = "txt" then
'stuff for txt files
End If

It has been a while, but in the past I have seen programs/scripts that create files with uppercase extensions such as .LOG. If you only compare file extensions directly to your string literal of .log, it would be missed.
 
I totally agree with jges regarding case.

-Geates

"I hope I can feel and see the change - stop the bleed inside a feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top