Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

[Regular Expressions] Limit The Search 2

Status
Not open for further replies.

Dalie

Programmer
Feb 16, 2009
17
NL
I use the following script to find folders with a specific name on my PC. However this script searches the whole computer every time. I am quite new to regular expressions. I'd like to modify the script in a way it limits the search to a folder, lets say: c:\temp

I understand the code except the bold lines, maybe someone could explain these lines in plain language... and point me in the direction for finding the solution.
Code:
Set objRegEx = CreateObject("VBScript.RegExp")

objRegEx.Global = True   
objRegEx.IgnoreCase = True
objRegEx.Pattern = "ABCD"
[b]
strComputer = "." 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colFolders = objWMIService.ExecQuery("Select * From Win32_Directory")
[/b]
For Each objFolder in colFolders
    strFolder = objFolder.FileName
    Set colMatches = objRegEx.Execute(strFolder)

    If colMatches.Count > 0 Then
        Wscript.Echo objFolder.Name 
    End If
Next
 
Thanks but as soon as I enter your line of code the script doesn't show any results... (I created the folder c:\temp with several folders which contains names abcd). Without the line it does show these folders I created...
 
strongm, I always thought that the syntax you provided would just return the C:\Temp folder?

Dalie - How about something like:
Code:
Set colFolders = objWMIService.ExecQuery("Select * From Win32_Directory where path = '\\temp\\' and drive = 'C:'")
Hope this helps

HarleyQuinn
---------------------------------
The most overlooked advantage to owning a computer is that if they foul up there's no law against wacking them around a little. - Joe Martin

Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
To search subdirectory starting from a base directory, use associator query on win32_subdirectory. This is a hand-held article.
It is a recursive search with superior efficiency.

For a specific subdirectory in mind, stop the recursion once it is found, and return with the find.
 
Thanks HarleyQuinn that totally fixed the problem!
 
Glad I could help [smile]

HarleyQuinn
---------------------------------
The most overlooked advantage to owning a computer is that if they foul up there's no law against wacking them around a little. - Joe Martin

Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
I was foolishly addressing

> to find folders with a specific name

rather than

>search to a folder, lets say: c:\temp

Given that I now understand we want to search a specific folder for (sub)folders that match a specific pattern, I'd extend the query further to eliminate necessity to do a pattern match in the later loop (which also means we can get rid of all the RegExp stuff):
Code:
[blue]Set colFolders = objWMIService.ExecQuery("Select * From Win32_Directory where path = '\\temp\\' and drive = 'C:' and name LIKE '%ABCD%'")[/blue]
which means that the loop can become something like:
Code:
[blue]For Each objFolder In colFolders
        Wscript.Echo objFolder.Name
Next[/blue]

In fact the entire code could be replaced with (if we were feeling a little obtuse):
Code:
[blue]strComputer = "."
For Each objFolder In GetObject("winmgmts:\\" & strComputer & "\root\cimv2").ExecQuery("Select * From Win32_Directory where path = '\\temp\\' and drive = 'C:' and name LIKE '%ABCD%'")
        Wscript.echo objFolder.Name
Next[/blue]


 
strongm said:
which also means we can get rid of all the RegExp stuff
I never thought I'd see you say that [wink]

Though in this case, I agree that's a viable option. [smile]

I've often been concerned about using LIKE in the ExecQuery as sometimes it can be a nightmare if you've not further limited search area in the querystring (obviously in your example that's not a problem though [wink]).

HarleyQuinn
---------------------------------
The most overlooked advantage to owning a computer is that if they foul up there's no law against wacking them around a little. - Joe Martin

Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
>I never thought I'd see you say that

Yeah, I wondered whether I'd get picked up on that ... :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top