Solved

Search subfolders with a VBScript

Posted on 2008-10-09
3
2,083 Views
Last Modified: 2012-08-13
I have the following script that works very well to pull out the basic info from one directory.

Now the problem is that I need to go after a folder like C:\Program Files\Business Objects and all its sub folders.

I have two apps that are apparently cross linking on their version Crystal -- I need to get the raft of subdirectories and the files and versions.
strComputer = "10.xx.x.xxx"
 
strOutputFile = "./" & Split(WScript.ScriptName, ".")(0) & "_" & Hour(Now) & "_" & Minute(Now) & ".txt"
 
Set objFileSystem = CreateObject("Scripting.fileSystemObject")
Set objOutputFile = objFileSystem.CreateTextFile(strOutputFile, TRUE)
 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
 
Set colFileList = objWMIService.ExecQuery _
    ("ASSOCIATORS OF {Win32_Directory.Name='C:\Windows\System32'} Where " _
        & "ResultClass = CIM_DataFile")
 
strmsg = "File_Name" & vbTAB & "File_Type" & vbTAB & "File_Size" & vbTAB & "File_Version" & vbTAB & _
       "Manufacturer" & vbTAB & "Readable_Attr" & vbTAB & "Writeable_Attr" & vbTAB & "Hidden_Attr" & vbTAB & _
       "System_Attr" & vbTAB & "Archive_Attr" & vbTAB & "Encrypted_Attr" & vbTAB & "Compressed_Attr" & vbTAB & _
       "Created_Date" & vbTAB & "Installed_Date" & vbTAB & "Last_Accessed" & vbTAB & "Last_Modified"
objOutputFile.WriteLine (strmsg)
strmsg = ""
 
'I=0
For Each objFile In colFileList
    If (objFile.Extension = "dll" or  objFile.Extension = "exe")   Then
        strmsg=""
        strmsg = strmsg &objfile.name
        strmsg = strmsg &  vbTAB & objfile.filetype
        strmsg = strmsg &  vbTAB & objfile.filesize
        strmsg = strmsg &  vbTAB & objfile.version
        strmsg = strmsg &  vbTAB & objfile.manufacturer
        strmsg = strmsg &  vbTAB & objfile.readable
        strmsg = strmsg &  vbTAB & objfile.writeable
        strmsg = strmsg &  vbTAB & objfile.hidden
        strmsg = strmsg &  vbTAB & objfile.system
        strmsg = strmsg &  vbTAB & objfile.archive
        strmsg = strmsg &  vbTAB & objfile.encrypted
        strmsg = strmsg &  vbTAB & objfile.compressed
        strmsg = strmsg &  vbTAB & mid(objfile.CreationDate,5,2) & "/" &_
                mid(objfile.CreationDate,7,2) & "/" & mid(objfile.CreationDate,1,4)
        strmsg = strmsg & vbTAB & mid(objfile.InstallDate,5,2) & "/" &_
                mid(objfile.InstallDate,7,2) & "/" & mid(objfile.InstallDate,1,4)
        strmsg = strmsg & vbTAB & mid(objfile.LastAccessed,5,2) & "/" &_
                mid(objfile.LastAccessed,7,2) & "/" & mid(objfile.LastAccessed,1,4)
        strmsg = strmsg & vbTAB & mid(objfile.LastModified,5,2) & "/" &_
                mid(objfile.LastModified,7,2) & "/" & mid(objfile.LastModified,1,4)
        objOutputFile.WriteLine ( strmsg)
    End If
Next
 
objOutputFile.Close
Set objFileSystem = Nothing
 
msgbox "done"

Open in new window

0
Comment
Question by:Jim P.
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 50

Accepted Solution

by:
Dave Brett earned 500 total points
ID: 22685722
Attached is recursive code below

change
strFolderName = "C:\excelfiles"
to your path

Pls note that I have turned off the test for file extensions, currently all files are returned
 'If (objFile.Extension = "dll" or  objFile.Extension = "exe")   Then

Cheers

Dave
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
strOutputFile = "./" & Split(WScript.ScriptName, ".")(0) & "_" & Hour(Now) & "_" & Minute(Now) & ".txt"
Set objFileSystem = CreateObject("Scripting.fileSystemObject")
Set objOutputFile = objFileSystem.CreateTextFile(strOutputFile, True)
 
strFolderName = "C:\excelfiles"
Set colSubfolders = objWMIService.ExecQuery _
                    ("Associators of {Win32_Directory.Name='" & strFolderName & "'} " _
                   & "Where AssocClass = Win32_Subdirectory " _
                   & "ResultRole = PartComponent")
 
strmsg = "File_Name" & vbTab & "File_Type" & vbTab & "File_Size" & vbTab & "File_Version" & vbTab & _
         "Manufacturer" & vbTab & "Readable_Attr" & vbTab & "Writeable_Attr" & vbTab & "Hidden_Attr" & vbTab & _
         "System_Attr" & vbTab & "Archive_Attr" & vbTab & "Encrypted_Attr" & vbTab & "Compressed_Attr" & vbTab & _
         "Created_Date" & vbTab & "Installed_Date" & vbTab & "Last_Accessed" & vbTab & "Last_Modified"
 
objOutputFile.WriteLine (strmsg)
strmsg = ""
 
arrFolderPath = Split(strFolderName, "\")
strNewPath = ""
For i = 1 To UBound(arrFolderPath)
    strNewPath = strNewPath & "\\" & arrFolderPath(i)
Next
strPath = strNewPath & "\\"
 
Set colFiles = objWMIService.ExecQuery _
               ("Select * from CIM_DataFile where Path = '" & strPath & "'")
 
For Each objfile In colFiles
    'If (objFile.Extension = "dll" or  objFile.Extension = "exe")   Then
    strmsg = ""
    strmsg = strmsg & objfile.Name
    strmsg = strmsg & vbTab & objfile.FileType
    strmsg = strmsg & vbTab & objfile.filesize
    strmsg = strmsg & vbTab & objfile.Version
    strmsg = strmsg & vbTab & objfile.manufacturer
    strmsg = strmsg & vbTab & objfile.readable
    strmsg = strmsg & vbTab & objfile.writeable
    strmsg = strmsg & vbTab & objfile.Hidden
    strmsg = strmsg & vbTab & objfile.system
    strmsg = strmsg & vbTab & objfile.archive
    strmsg = strmsg & vbTab & objfile.encrypted
    strmsg = strmsg & vbTab & objfile.compressed
    strmsg = strmsg & vbTab & Mid(objfile.CreationDate, 5, 2) & "/" & _
    Mid(objfile.CreationDate, 7, 2) & "/" & Mid(objfile.CreationDate, 1, 4)
    strmsg = strmsg & vbTab & Mid(objfile.InstallDate, 5, 2) & "/" & _
    Mid(objfile.InstallDate, 7, 2) & "/" & Mid(objfile.InstallDate, 1, 4)
    strmsg = strmsg & vbTab & Mid(objfile.LastAccessed, 5, 2) & "/" & _
    Mid(objfile.LastAccessed, 7, 2) & "/" & Mid(objfile.LastAccessed, 1, 4)
    strmsg = strmsg & vbTab & Mid(objfile.LastModified, 5, 2) & "/" & _
    Mid(objfile.LastModified, 7, 2) & "/" & Mid(objfile.LastModified, 1, 4)
    objOutputFile.WriteLine (strmsg)
    ' End If
Next
 
 
 
For Each objFolder In colSubfolders
    GetSubFolders strFolderName
Next
 
objOutputFile.Close
Set objFileSystem = Nothing
MsgBox "done"
 
Sub GetSubFolders(strFolderName)
    Set colSubfolders2 = objWMIService.ExecQuery _
                         ("Associators of {Win32_Directory.Name='" & strFolderName & "'} " _
                        & "Where AssocClass = Win32_Subdirectory " _
                        & "ResultRole = PartComponent")
 
    For Each objFolder2 In colSubfolders2
        strFolderName = objFolder2.Name
        arrFolderPath = Split(strFolderName, "\")
        strNewPath = ""
        For i = 1 To UBound(arrFolderPath)
            strNewPath = strNewPath & "\\" & arrFolderPath(i)
        Next
        strPath = strNewPath & "\\"
 
        Set colFiles = objWMIService.ExecQuery _
                       ("Select * from CIM_DataFile where Path = '" & strPath & "'")
 
        For Each objfile In colFiles
            'If (objFile.Extension = "dll" or  objFile.Extension = "exe")   Then
            strmsg = ""
            strmsg = strmsg & objfile.Name
            strmsg = strmsg & vbTab & objfile.FileType
            strmsg = strmsg & vbTab & objfile.filesize
            strmsg = strmsg & vbTab & objfile.Version
            strmsg = strmsg & vbTab & objfile.manufacturer
            strmsg = strmsg & vbTab & objfile.readable
            strmsg = strmsg & vbTab & objfile.writeable
            strmsg = strmsg & vbTab & objfile.Hidden
            strmsg = strmsg & vbTab & objfile.system
            strmsg = strmsg & vbTab & objfile.archive
            strmsg = strmsg & vbTab & objfile.encrypted
            strmsg = strmsg & vbTab & objfile.compressed
            strmsg = strmsg & vbTab & Mid(objfile.CreationDate, 5, 2) & "/" & _
            Mid(objfile.CreationDate, 7, 2) & "/" & Mid(objfile.CreationDate, 1, 4)
            strmsg = strmsg & vbTab & Mid(objfile.InstallDate, 5, 2) & "/" & _
            Mid(objfile.InstallDate, 7, 2) & "/" & Mid(objfile.InstallDate, 1, 4)
            strmsg = strmsg & vbTab & Mid(objfile.LastAccessed, 5, 2) & "/" & _
            Mid(objfile.LastAccessed, 7, 2) & "/" & Mid(objfile.LastAccessed, 1, 4)
            strmsg = strmsg & vbTab & Mid(objfile.LastModified, 5, 2) & "/" & _
            Mid(objfile.LastModified, 7, 2) & "/" & Mid(objfile.LastModified, 1, 4)
            objOutputFile.WriteLine (strmsg)
            ' End If
        Next
        GetSubFolders strFolderName
    Next
End Sub

Open in new window

0
 
LVL 38

Author Closing Comment

by:Jim P.
ID: 31504754
That works great. Thanks!
0
 
LVL 50

Expert Comment

by:Dave Brett
ID: 22686997
Thanks for the grade :)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question