umass Posted September 23, 2011 Posted September 23, 2011 Hey all, I am trying to write a script that connects to a number of computers and counts the number of file types (i.e. .GIF, .JPEG, .MP3, etc...) that are under a specific directory such as My Documents. I have looked around, but thought I'd come here and expand my resources for this project, since time is of the essence. If anyone could point me in the right direction, or to the right book/link, I'd appreciate it a bunch.
ChrisH Posted September 23, 2011 Posted September 23, 2011 You want to do a Vbscript with a wmi query looking specifically file the files types or extensions. The queries look a bit like a SQL query. There is probably a nice way to do it in Powershell as well but I don't know that off hand. 1
umass Posted September 23, 2011 Author Posted September 23, 2011 Hey Chris, I had a feeling that this task would require me to involve the WMI stuff, but here is a code that might be useful for this. All I need to do is to specify the UNC for the server through the strDirectory parameter. (found at: VBScript Function to Manage Windows Networks - Scripts for Administrators) Option Explicit Const retvalUnknown = 1 ' /////////////////////////////////////////////////////////////////////////////// Function CheckMp3Files( strDirectory ) Dim numMp3Files, objFso, objFolder Set objFso = CreateObject("Scripting.FileSystemObject") Set objFolder = objFso.GetFolder("c:\temp") numMp3Files = CountMp3Files( objFolder ) If( numMp3Files > 0 ) Then EXPLANATION = numMp3Files & " MP3 files detected" CheckMp3Files = False Else EXPLANATION = "No MP3 files detected" CheckMp3Files = True End If End Function ' /////////////////////////////////////////////////////////////////////////////// Function CountMp3Files( objFolder ) Dim objSubFolders, objSubFolder Dim objFiles, objFile Dim iFileNum iFileNum = 0 CountMp3Files = 0 Set objFiles = objFolder.Files If objFiles.Count <> 0 Then For Each objFile In objFiles If( InStr( UCase( objFile.Name ), ".MP3" ) <> 0 ) Then iFileNum = iFileNum + 1 End If Next End If Set objSubFolders = objFolder.SubFolders If objSubFolders.Count <> 0 Then For Each objSubFolder In objSubFolders iFileNum = iFileNum + CountMp3Files( objSubFolder ) Next End If CountMp3Files = iFileNum End Function What do you think?
ChrisH Posted September 23, 2011 Posted September 23, 2011 That does the same sort of thing but a WMI query looks a bit like this: strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" _ & strComputer & "\root\cimv2") Set colFiles = objWMIService. _ ExecQuery("Select * from CIM_DataFile where Extension = 'mp3'") For Each objFile in colFiles Wscript.Echo "File Name: " & objFile.Name & "." & objFile.Extension Wscript.Echo "Path: " & objFile.Path Next They are usually chosen between because of speed. Your example is better in some circumstances than mine although I can't think of a good example.
umass Posted September 23, 2011 Author Posted September 23, 2011 Yea. I was just talking to my boss and he pointed me in the exact same direction as you. I'm about to leave work so won't be dealing with this until Tuesday, so I'll get back to you as soon as I get something. Thanks a lot and have a good weekend. That does the same sort of thing but a WMI query looks a bit like this: strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" _ & strComputer & "\root\cimv2") Set colFiles = objWMIService. _ ExecQuery("Select * from CIM_DataFile where Extension = 'mp3'") For Each objFile in colFiles Wscript.Echo "File Name: " & objFile.Name & "." & objFile.Extension Wscript.Echo "Path: " & objFile.Path Next They are usually chosen between because of speed. Your example is better in some circumstances than mine although I can't think of a good example.
tumbleweed Posted September 23, 2011 Posted September 23, 2011 Have a look a the Gnuwin32 package - a port of unix goodies that windows is sadly lacking in. the 'find' command-line utility could be what you need.
umass Posted October 7, 2011 Author Posted October 7, 2011 I have finalized the script and everything is working fine. If any of you ever run into problems related to this post, feel free to contact me and I'll show you what I've got. Thank you for all your support btw.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now