Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

vbscript to search for folders

  • 11-01-2013 1: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