SPONSORS:






User Tag List

Thanks Thanks:  0
Likes Likes:  0
Dislikes Dislikes:  0
Page 1 of 2 12 LastLast
Results 1 to 10 of 14
  1. #1
    Senior Member
    Join Date
    Jan 2000
    Location
    USA, CA
    Posts
    1,998
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    Re: Search folders using VBScript

    Function FindFiles(StartFolder, FileNamePrefix)
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set f = fso.GetFolder(StartFolder)
    Set fls = f.Files

    For Each f1 In fls
    If (InStr(f1.Name, FileNamePrefix)>0) Then
    FindFiles = FindFiles & f1.Path & vbNewLine
    End If
    Next

    Set f = Nothing
    Set fso = Nothing
    End Function

    fls = FindFiles("C:\\", "test")
    msgbox fls

  2. #2
    Member
    Join Date
    Apr 2004
    Posts
    122
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    Re: Search folders using VBScript

    Actually I was trying to search for folders using the wild cards and not files. Can I do the similar type of search for folders instead of files?
    I would like to find all the folders which start with "test". There are lots of folders in C: drive like test1 test2, test3 ..and so on. I would like to find all of them.

    Thanks a lot.

  3. #3
    Member
    Join Date
    Apr 2004
    Posts
    122
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    Search folders using VBScript

    How can I search for a folders using wild card in VBScript?
    In the example below, I would like to search for the entire folder in C: drive starting with "Test" and not only "Test1". So, how can I use a search criteria something like this "Test*" instead of "Test1"?

    Thanks.

    Dim s
    Set fso=CreateObject("Scripting.FileSystemObject")
    Set f = fso.GetFolder("c:\")
    s=f.path + "Test1"

    If (fso.FolderExists(s) ) Then
    msgbox "Folder exists!!"
    else msgbox "Folder does not exists"
    End If

  4. #4
    Member
    Join Date
    Oct 2002
    Location
    NJ, USA
    Posts
    65
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    Re: Search folders using VBScript

    There is no direct solution for this. That is, you cannot use wildcard character to reterive all the matching folders. However, this can be done by writing a function to reterive all the folders in C: drive and the use Regular Expression to look for a pattern.

    I will try to get some script for it if you want.

  5. #5
    Member
    Join Date
    Apr 2004
    Posts
    122
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    Re: Search folders using VBScript

    Thanks Partha. Could you please send me some example scripts? Thanks again.

  6. #6
    Senior Member
    Join Date
    Feb 2005
    Location
    Northern Ireland
    Posts
    529
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    Re: Search folders using VBScript

    Try this:

    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colFolders = objWMIService.ExecQuery _
    ("Select * from Win32_Directory where name like '%c:\\Test%'")
    For Each objFolder in colFolders
    MsgBox objFolder.Name
    Next

    Regards,
    Andy
    I'm not a complete idiot - some parts are missing.

  7. #7
    Senior Member
    Join Date
    Feb 2005
    Location
    Northern Ireland
    Posts
    529
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    Re: Search folders using VBScript

    If you only want to search for folders on the C:\ drive then use:

    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colFolders = objWMIService.ExecQuery _
    ("Select * from Win32_Directory where name like '%c:\\Test%'")
    For Each objFolder in colFolders
    if left(objFolder.name, 7) = "C:\Test"
    MsgBox objFolder.Name
    end if
    Next
    I'm not a complete idiot - some parts are missing.

  8. #8
    Senior Member
    Join Date
    Jan 2000
    Location
    USA, CA
    Posts
    1,998
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    Re: Search folders using VBScript

    In my example:
    this statement:
    If (InStr(f1.Name, FileNamePrefix)>0) Then

    will filter all files in the directory C:
    and will find ALL files, that starts with
    FileNamePrefix, provided in the second parameter.
    So, if you give as parameter - "test" ,
    you will get all files started with "test":
    test1, test10, testWhatever and so on.
    It will be one String, that can be parsed then in any way to retrieve single value form it.
    Or: you can insert script logic directly after this line - to make some functionality, based on searching result.

  9. #9
    Member
    Join Date
    Apr 2004
    Posts
    122
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    Re: Search folders using VBScript

    Thank you all guys. I will try these examples and will let you know if I have any problem.

    Thank you again. You guys are very helpful.

  10. #10
    Member
    Join Date
    Apr 2004
    Posts
    122
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    Re: Search folders using VBScript

    Andy,
    I am unable to run the below code because it throws some errror message on line 6. I am unable to figure out the problem. Could you or somebody can help?

    Thanks.

    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colFolders = objWMIService.ExecQuery _
    ("Select * from Win32_Directory where name like '%c:\\Test%'")
    For Each objFolder in colFolders
    if left(objFolder.name, 7) = "C:\Test" then
    MsgBox objFolder.Name
    end if
    Next

 

 
Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Search Engine Optimisation provided by DragonByte SEO v2.0.36 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Resources saved on this page: MySQL 10.00%
vBulletin Optimisation provided by vB Optimise v2.6.4 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
User Alert System provided by Advanced User Tagging v3.2.8 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
vBNominate (Lite) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Feedback Buttons provided by Advanced Post Thanks / Like (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Username Changing provided by Username Change (Free) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
BetaSoft Inc.
Digital Point modules: Sphinx-based search
All times are GMT -8. The time now is 02:16 PM.

Copyright BetaSoft Inc.