+ Post New Thread
Results 1 to 7 of 7
Scripts Thread, VBScript: Access Computers on a network and count specific files types in Coding and Web Development; Hey all, I am trying to write a script that connects to a number of computers and counts the number ...
  1. #1

    Join Date
    Sep 2011
    Posts
    16
    Thank Post
    1
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    VBScript: Access Computers on a network and count specific files types

    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.

  2. #2
    ChrisH's Avatar
    Join Date
    Jun 2005
    Location
    East Lancs
    Posts
    4,993
    Thank Post
    120
    Thanked 286 Times in 263 Posts
    Rep Power
    107
    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.

  3. Thanks to ChrisH from:

    umass (23rd September 2011)

  4. #3

    Join Date
    Sep 2011
    Posts
    16
    Thank Post
    1
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    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?

  5. #4
    ChrisH's Avatar
    Join Date
    Jun 2005
    Location
    East Lancs
    Posts
    4,993
    Thank Post
    120
    Thanked 286 Times in 263 Posts
    Rep Power
    107
    That does the same sort of thing but a WMI query looks a bit like this:
    Code:
    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.

  6. #5

    Join Date
    Sep 2011
    Posts
    16
    Thank Post
    1
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    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.

    Quote Originally Posted by ChrisH View Post
    That does the same sort of thing but a WMI query looks a bit like this:
    Code:
    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.

  7. #6

    Join Date
    Dec 2007
    Location
    cumbria
    Posts
    182
    Thank Post
    7
    Thanked 43 Times in 39 Posts
    Rep Power
    25
    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.

  8. #7

    Join Date
    Sep 2011
    Posts
    16
    Thank Post
    1
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    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.

SHARE:
+ Post New Thread

Similar Threads

  1. Replies: 5
    Last Post: 21st June 2011, 02:02 PM
  2. Home Computers on the Network
    By Stuart_C in forum School ICT Policies
    Replies: 5
    Last Post: 3rd September 2009, 06:41 PM
  3. Setup for dual boot XP and Vista on a network workstation
    By speckytecky in forum General Chat
    Replies: 4
    Last Post: 4th July 2008, 12:06 PM
  4. Looking at printers and computers on a Workgroup
    By TechSupp in forum Wireless Networks
    Replies: 3
    Last Post: 1st January 2008, 03:18 PM
  5. Remote access effect on SLAs and uptimes
    By ITWombat in forum School ICT Policies
    Replies: 3
    Last Post: 2nd October 2006, 08:33 AM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •