Solved

Search script that will return a list of results from a specific folder and sub folders

Posted on 2013-01-28
25
336 Views
Last Modified: 2013-02-07
Hello,

I am looking for a vbscript or batch file that when double clicked will open a search box, accept the input from a user (folder name typed by a user), and search a specific folder and its sub folders and return a list with all folders with the searched criteria.

Thank you.
0
Comment
Question by:cbones
  • 11
  • 11
  • 2
  • +1
25 Comments
 
LVL 11

Expert Comment

by:ScriptAddict
ID: 38828197
VB doesn't really support input.  If you need input you need to bind into something else.  I typically use internet explorer.  

But even that is a pain, every time they update IE I have to update my VB to make sure it works properly.

-SA
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 38828396
Can you explain better what you mean by "search" in this case?  Are you looking for files that match a certain name?  Or files that contain certain text?  Etc...

~bp
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 38829096
In VBScript, you can use a BrowseForFolder dialog:
http://blogs.technet.com/b/heyscriptingguy/archive/2005/06/17/how-can-i-show-users-a-dialog-box-that-only-lets-them-select-folders.aspx

but as Bill says, we're not sure what your criteria would be to find files?

Rob.
0
 

Author Comment

by:cbones
ID: 38829215
BillPrew,

I would have files stored in different folders on a server.

Ex:  D Drive 5TB with a main folder
       Main Folder = Main
       Main will have subfolders
Ex:  D:\Main\folder1
       D:\Main\folder2
       D:\Main\folder3
       etc...
Each folder under D:\main\folder1,2,3,4,5,etc...can have sub folders in itself.

What I am looking for is to have someone type in the folder name they are looking for and have a list returned to them that they can select.  So if someone typed in "test" it would return them a list of folders that start with the name test.

Is this possible?

Thank you.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 38829237
What would they do with the folder once selected?  Would it be enough to just display the matching folder names, or do you want to select one of them to do something with?

As a manual way, in a Windows 7 search box, you can type
kind:folder name:<criteria>

like
kind:folder name:test

to display matching names.

Rob.
0
 

Author Comment

by:cbones
ID: 38829308
Rob,

I would have an option in the sendto menu for them to right click and transfer the folder.

Thank you.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 38829359
You couldn't visually present that functionality with any scripting technology (that I'm aware of), so I think the custom Windows search would be your best bet, since you then have access to the Send To function via the right-click context menu.

Rob.
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 38829429
And what does "transfer the folder" mean, is that an FTP file transfer, or something else?

~bp
0
 

Author Comment

by:cbones
ID: 38852719
Once they found the folder or folders they were looking for, there would be a utility for them to use on their right click menu. That would transfer the folders.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 38852869
Do you how that utility is launched?  Is it already in your SendTo context menu?  If so, if you're on Windows XP, browse to
C:\Documents and Settings\%username%\SendTo

or Vista / 7, browse to
C:\Users\%username%\AppData\Roaming\Microsoft\Windows\SendTo

In that folder, you should have a shortcut to the utility you're after.  Right click it, select properties, and paste the path to it here.  Using this, we should be able to build a function in VBScript that would run this on the select folder.

Regards,

Rob.
0
 

Author Comment

by:cbones
ID: 38853098
Yes, the utility I have is in the SendTo folder in Windows.   So once they would do a search for the folders and it was returned, they would then execute that utility.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 38853255
We could have the script execute the utility.  What is the command?  You don't need to show the exact executable, but the structure of the path would be useful.

Rob.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:cbones
ID: 38853302
Rob,

The path would be simple. C:\scripts\send.exe

That would be in the properties of the SendTo shortcut
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 38853338
Does it have a parameter after it though?  Usually they're something like
C:\Scripts\Send.exe "%1"

so, if you manually run this at a command prompt:
C:\Scripts\Send.exe "C:\MyFolder"

does that do what you want?

Rob.
0
 

Author Comment

by:cbones
ID: 38853351
Rob,

Sorry. It does have parameters. I don't have that info on me right now.  Can I add it in after?

And yes, it would run as you put above with the parameters.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 38853589
OK, so here is a HTA script that I've just built that should suit your requirement.

Save the code as something like FolderSearch.hta and double click it to run it.

Modify these two lines to suit:
            strRootFolder = "C:\Temp\Scripts"
            strCommand = "C:\Scripts\Send.exe ""%FOLDERPATH%"""

but make sure you keep ""%FOLDERPATH%"" in the string where the selected folder is required.

Also, you will see a message box that shows you the command that it try to run, before running it.  Once you're happy and don't want that, just comment out or delete this line:
   			MsgBox "Running " & Replace(strCommand, "%FOLDERPATH%", lstFolders.value)

Open in new window


Regards,

Rob.

<head>
<title>Folder Search</title>
<HTA:APPLICATION 
     APPLICATIONNAME="Folder Search"
     BORDER="dialog"
     SCROLL="no"
     SHOWINTASKBAR="no"
     SINGLEINSTANCE="yes"
     SYSMENU="yes"
     WINDOWSTATE="normal"
>
</head>

<script language="VBScript">
	Dim strCommand
	Sub Window_OnLoad
		Dim intWidth, intHeight
		intWidth = 800
		intHeight = 650
		Me.ResizeTo intWidth, intHeight
		Me.MoveTo ((Screen.Width / 2) - (intWidth / 2)),((Screen.Height / 2) - (intHeight / 2))
		
		strRootFolder = "C:\Temp\Scripts"
		strCommand = "C:\Scripts\Send.exe ""%FOLDERPATH%"""
		
		spanRootFolder.innerHTML = strRootFolder
		lstFolders.Style.Width = 650
		btnOpen.disabled = True
	End Sub

    Sub Search
	    For Each objOption In lstFolders.Options
	        lstFolders.Remove(objOption.Index)
	   	Next
	   	If txtSearch.Value <> "" Then
	   		btnOpen.disabled = True
	   		RecurseFolder spanRootFolder.innerHTML
	   		If lstFolders.options.length = 0 Then
	   			MsgBox "No matches found."
	   		Else
	   			btnOpen.disabled = False
	   		End If
	   	Else
	   		MsgBox "Please enter a search term."
	   		txtSearch.focus
	   	End If
   	End Sub
   	
   	Sub RecurseFolder(strFolderPath)
	   	Set objFSO = CreateObject("Scripting.FileSystemObject")
   		For Each objSubFolder In objFSO.GetFolder(strFolderPath).SubFolders
   			If Left(LCase(objSubFolder.Name), Len(txtSearch.Value)) = LCase(txtSearch.Value) Then
		        Set objNewOption = Document.CreateElement("OPTION")
		        objNewOption.Text = objSubFolder.Path
		        objNewOption.Value = objSubFolder.Path
				lstFolders.Add(objNewOption)
			End If
			RecurseFolder objSubFolder.Path
		Next
   	End Sub
   	
   	Sub OpenFolder
   		If lstFolders.SelectedIndex = -1 Then
   			MsgBox "Please select a folder."
   		Else
   			Set objShell = CreateObject("WScript.Shell")
   			MsgBox "Running " & Replace(strCommand, "%FOLDERPATH%", lstFolders.value)
   			objShell.Run Replace(strCommand, "%FOLDERPATH%", lstFolders.value), 1, False
   		End If
   	End Sub
</script>

<body>

    <table width="95%">
    	<tr>
    		<td align="center">
    			<H2>Folder Search Utility</H2>
    		</td>
    	</tr>
    	<tr>
    		<td align="center">
    			Folder being searched:&nbsp;<span id="spanRootFolder"></span>
    		</td>
    	</tr>
    	<tr>
    		<td align="center">
    			Enter folder search term:&nbsp;<input type="text" size="65" name="txtSearch" id="txtSearch">
    			&nbsp;<input type="button" id="btnSearch" name="btnSearch" value="Search" onclick="vbs:Search">
    		</td>
    	</tr>
    	<tr>
    		<td align="center">
				<select size="10" name="lstFolders" id="lstFolders">
				</select>
    		</td>
    	</tr>
       	<tr>
    		<td align="center">
				<input type="button" id="btnOpen" name="btnOpen" value="Transfer Folder" onclick="vbs:OpenFolder">
    		</td>
    	</tr>
	</table>


</body>

Open in new window

0
 

Author Comment

by:cbones
ID: 38855117
Rob,

Thank you for the script.  It looks perfect.  The issue I am having is when I look in the SendTo folder, it is a shortcut file, not the actual file.  Can I put a shortcut into this script and have it work?

Thank you.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 38857202
Probably not.  All you need to do though is replicate the syntax of the "Target" field on the shortcut into
            strCommand = "C:\Scripts\Send.exe ""%FOLDERPATH%"""

So, if the target is
C:\Scripts\Send.exe "%1"

then this would be the equivalent that would work in the script
            strCommand = "C:\Scripts\Send.exe ""%FOLDERPATH%"""

where the ""%FOLDERPATH%"" will be replaced in the command automatically by the folder selected by the user.

Rob.
0
 

Author Comment

by:cbones
ID: 38859488
Rob,

I added the syntax from the target field of the shortcut into the strCommand so it looks like below:

strCommand = "C:\scripts\send.exe +r -v 192.168.1.108 444""%FOLDERPATH%"""
but when i click transfer it says running:
C:\scripts\send.exe +r -v 10.10.10.120 444 "c:\test\files" -> c:\test\files being the folder I selected

when I click OK a command prompt opens (which should be the program running) but closes so fast I cannot see what it says.  No files are transferred.

Is there anything I can check?

Thank you.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 38861732
Sure, change
            strCommand = "C:\scripts\send.exe +r -v 10.10.10.120 444 ""%FOLDERPATH%"""


to
            strCommand = "cmd /k C:\scripts\send.exe +r -v 10.10.10.120 444 ""%FOLDERPATH%"""

and see what the output is.  You can also run a command prompt yourself and type
            C:\scripts\send.exe +r -v 10.10.10.120 444 "c:\test\files"

and see what output you get.  Massage the command until it works, then plug that into strCommand.  You may actually need to use cmd /c at the start of the command if it's a DOS only program.

Regards,

Rob.
0
 

Author Comment

by:cbones
ID: 38862033
Rob,

Thank you for your response.  When I run manually:
c:\scripts\send.exe +r -v 10.10.10.120 444 "c:\test\files"  the command runs successful. (This is from the command prompt)

When I click transfer folder on the app it pops the screen:
Running cmd /c C:\scripts\send.exe +r -v 10.10.10.120 444 "C:\test\files"

but it appears that it is not seeing the FOLDERPATH.  I tried with the /c, /k.

When I do the cmd /k the command prompt appears and that is where it is failing. It doesn't know what folder to transfer.

Thank you.
0
 

Author Comment

by:cbones
ID: 38862077
Rob,

Sorry.  I changed the strCommand around a little.  When I change this:
strCommand = "cmd /k C:\scripts\send.exe +r -v 10.10.10.120 444 ""%FOLDERPATH%"""

to this:
strCommand = "cmd /k C:\scripts\send.exe +r -v 10.10.10.120 444 %FOLDERPATH%"

it goes to send the folder.  But if the folder has a space in the name it fails because it tries to send a folder with just the first name.
Ex: c:\test\files 1234
the command tries to send c:\test\files instead of c:\test\files 1234.  Can this be adjusted with quotes to end up with quotes around "C:\test\files 1234"?

Thank you.
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 38862129
Hmmm, that's odd.  The quotes should be there from the original strCommand, since you can see it in the MsgBox.  Anyway, try changing this:
   			Set objShell = CreateObject("WScript.Shell")
   			MsgBox "Running " & Replace(strCommand, "%FOLDERPATH%", lstFolders.value)
   			objShell.Run Replace(strCommand, "%FOLDERPATH%", lstFolders.value), 1, False

Open in new window


to this
   			Set objShell = CreateObject("WScript.Shell")
   			strNewCommand = Replace(strCommand, "%FOLDERPATH%", """" & lstFolders.value & """")
   			MsgBox "Running " & strNewCommand
   			objShell.Run strNewCommand, 1, False

Open in new window


and see if that helps.

Regards,

Rob.
0
 

Author Closing Comment

by:cbones
ID: 38864763
Rob,

That did the trick.  Thank you for all your help!.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 38865864
Great!  No problem.  Thanks for the grade.

Rob.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now