Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.
Hi all, please see this major site announcement: https://www.boards.ie/discussion/2058427594/boards-ie-2026

vbscript to search for folders

  • 11-01-2013 01:00PM
    #1
    Registered Users, Registered Users 2 Posts: 16


    Hi All,

    I'm trying to write a vbscript that will search for multiple occurrences of a folder on a remote server. The folder wont always have the same name but will obviously always have a keyword in the folder name. I have most of it working but the problems is:

    When I run the script, it seems to only search the C: drive. I haven't specified any drives so it should search all drives on the machine. This is done in the line: Set colFolders = objWMIService.ExecQuery("Select * from Win32_Directory where Name Like '%" & strCriteria & "%'")

    If I enter a word like 'Windows' or 'Program' it will find all of the obvious stuff on C:\ but nothing on my D: drive even though I specifically create a folder called 'D:\Program' for testing purposes.

    The code:

    Dim FileSys, newFolder, newFolderPath, strCriteria

    strCriteria = inputbox("Enter folder name you are searching for:")

    strComputer = "."
    newFolderPath = "\\dvv93r1\dest1\" & strCriteria
    'Wscript.Echo(newFolderPath)

    Set FileSys = CreateObject("Scripting.FileSystemObject")

    If Not FileSys.FolderExists(newFolderPath) Then
    FileSys.CreateFolder(newFolderPath)
    FileSys.CreateFolder(newFolderPath & "\analysis")
    FileSys.CreateFolder(newFolderPath & "\logs")
    End If
    outFile = newFolderPath & "\logs\log.txt"
    Set objFSO=CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.CreateTextFile(outFile,True)

    Set oShell = WScript.CreateObject("Wscript.Shell")
    Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colFolders = objWMIService.ExecQuery("Select * from Win32_Directory where Name Like '%" & strCriteria & "%'")

    For Each objFolder in colFolders
    'Wscript.Echo(objFolder.Name)
    objFile.Write objFolder.Name & vbCrLf

    sText = Right(objFolder.Name, Len(objFolder.Name) - 3)
    netPath = "\\" & strComputer & "\" & sText
    'WScript.echo netPath

    If FileSys.FolderExists(netPath) Then
    FileSys.CopyFolder netPath, newFolderPath & "\analysis"
    End If

    Next
    objFile.Close
    WScript.Echo("Operation is complete.")


    Any help would be greatly appreciated.

    Thanks in advance.

    J


Comments

  • Registered Users, Registered Users 2 Posts: 2,931 ✭✭✭Ginger


    Edited

    Initial idea was incorrect

    If you use a just select query with no criteria do you get all folders back

    Btw, your net path in the final part will be incorrect as the unc should be \\machine\drive$\folderpath unless the folder is shared with default names


  • Registered Users, Registered Users 2 Posts: 16 jbng98


    Hi Ginger,

    I have tried the script with just a Select * from Win32_Directory on line 32 and this returns all folders in C:, K: and U: (local disk, external usb disk and a network drive) so how come "Select * from Win32_Directory where Name Like '%" & strCriteria & "%'" is only returning results from C: drive I wonder?

    Also, you are right about my net path, I'll work on that too.

    J


Advertisement