Author Topic: VB Script - searching for a folder ...  (Read 4251 times)

0 Members and 1 Guest are viewing this topic.

Hangman

  • Swamp Rat
  • Posts: 566
VB Script - searching for a folder ...
« on: March 07, 2012, 11:19:35 AM »
I am not sure if this forum is usable for VB Script or if anyone uses it here.  I'm hoping so as this is where I'm going to post.

So, I have a job directory (W:\ACAD Jobs\2011 Projects\...) where I have several design firms (sub-folders) listed.
I have several sub-folders of the same design firm but different locations.  An example: (I will keep the names ficticious for embarassment sake)
    W:\ACAD Jobs\2011 Projects\Design Firm -Alabama
    W:\ACAD Jobs\2011 Projects\Design Firm -Washington
    W:\ACAD Jobs\2011 Projects\Design Firm -Arizona

I want to search for the name "Design Firm" and assign the full name to a variable for display purposes.  So, I need to search for "Design Firm" and have Var_A store "Design Firm -Alabama", Var_B store "Design Firm - Washington", and Var_C store "Design Firm -Arizona".

According to my trusty O'reilly Pocket reference, I should be able to use the \b switch to search for a word in a sentence (a wildcard if you will).
Quote
Regular Expression Character Reference
\b Matches a word boundary, that is, the position between a word and a space.
Can someone tell me what this means?

Also, If I have a search for "Design Firm", how would I put in the switch to find any anomolous folders such as "Design Firm -Arizona"?
Here's a piece of code I currently have (perhaps I am looking at this the wrong way. If so, please advise):
Code: [Select]
cTitle = "Test run"
Set FSO = CreateObject("Scripting.FileSystemObject")

str2011dir = "W:\ACAD JOBS\2011 Projects\"

strDF = InputBox("Design Firm Name: ", cTitle, "Design Firm")
strDFm = "\b " & strDF     [color=maroon]' Normally, the switch would go to the front of the string right?[/color]

MsgBox "Input is -" & strDfm,, cTitle     [color=maroon]' This is here to show me what the string consists of.[/color]

If FSO.FolderExists(str2011dir & strDFm) Then
  MsgBox "The folder " & UCase(strDF) & " exists.",, cTitle
 Else
  MsgBox "No folder found",, cTitle
End If

EDIT:  According to MS <http://msdn.microsoft.com/en-us/library/ms974570.aspx>
Quote
\b - Matches any word boundary
 "ly\b" matches "ly" in "possibly tomorrow."

So my code
Code: [Select]
strDF = InputBox("Design Firm Name: ", cTitle, "Design Firm")
strDFm = strDF & "\b"
MsgBox "Input is -" & strDfm,, cTitle
The message box should be displaying "Design Firm\b" so my search should be finding anything with "Firm" in it correct?  But it isn't   :( .
« Last Edit: March 07, 2012, 11:47:12 AM by Hangman »
Hangman  8)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Drafting Board, Mechanical Arm, KOH-I-NOOR 0.7mm
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

BlackBox

  • King Gator
  • Posts: 3770
Re: VB Script - searching for a folder ...
« Reply #1 on: March 07, 2012, 12:49:55 PM »
FWIW -

Code - vb.net: [Select]
  1. Dim strSearchString, strSearchFor
  2.  
  3. strSearchString = "C:\foo\"
  4. strSearchFor = "foo"
  5.  
  6. If InStr(1, strSearchString, strSearchFor) > 0 then
  7.         MsgBox "True"
  8.   Else
  9.         MsgBox "False"
  10. End If
  11.  

Edit - Revised code for a better example.
« Last Edit: March 07, 2012, 12:55:24 PM by RenderMan »
"How we think determines what we do, and what we do determines what we get."

BlackBox

  • King Gator
  • Posts: 3770
Re: VB Script - searching for a folder ...
« Reply #2 on: March 07, 2012, 03:28:21 PM »
This could definitely use some refinement, but as a quick example, give this a try:

Code - vb.net: [Select]
  1. '
  2. ' Define the function:
  3. '
  4. Function ShowFolderList(strFolderPath, strSearchString)
  5.  
  6.         Dim i, strTitle, strFolderName, strSubFolderList
  7.  
  8.         strTitle = "Show Folder List"
  9.        
  10.         Set oFso = CreateObject("Scripting.FileSystemObject")
  11.        
  12.         If oFso.FolderExists(strFolderPath) = True Then
  13.        
  14.                 Set oSubFolders = _
  15.                         oFso.GetFolder(strFolderPath).SubFolders
  16.                
  17.                 For Each oSubFolder in oSubFolders
  18.                         strFolderName = oSubFolder.Name
  19.                         If InStr(1, strFolderName, strSearchString) > 0 Then
  20.                                 i = i+1
  21.                                 strSubFolderList = strSubFolderList & _
  22.                                         strFolderName & vbNewLine
  23.                         End If
  24.                 Next
  25.                 MsgBox i & " of " & oSubFolders.Count & _
  26.                         " subfolders match: " & vbNewLine & _
  27.                         vbNewLine & strSubFolderList,, strTitle
  28.         Else
  29.                 MsgBox "Failure to create FileSystemObject Object",, _
  30.                         strTitle
  31.         End If
  32.  
  33.         Set oSubFolders = Nothing
  34.         Set oFolder = Nothing
  35.         Set oFso = Nothing
  36.        
  37. End Function
  38.  
  39. '
  40. ' Call the function with multiple arguments:
  41. '
  42. ShowFolderList "C:\_FooVBScriptTest\", "foo - "
  43.  

HTH
« Last Edit: March 07, 2012, 03:32:17 PM by RenderMan »
"How we think determines what we do, and what we do determines what we get."