• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 348
  • Last Modified:

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

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
cbones
Asked:
cbones
  • 11
  • 11
  • 2
  • +1
1 Solution
 
ScriptAddictCommented:
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
 
Bill PrewCommented:
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
 
RobSampsonCommented:
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
cbonesAuthor Commented:
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
 
RobSampsonCommented:
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
 
cbonesAuthor Commented:
Rob,

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

Thank you.
0
 
RobSampsonCommented:
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
 
Bill PrewCommented:
And what does "transfer the folder" mean, is that an FTP file transfer, or something else?

~bp
0
 
cbonesAuthor Commented:
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
 
RobSampsonCommented:
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
 
cbonesAuthor Commented:
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
 
RobSampsonCommented:
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
 
cbonesAuthor Commented:
Rob,

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

That would be in the properties of the SendTo shortcut
0
 
RobSampsonCommented:
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
 
cbonesAuthor Commented:
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
 
RobSampsonCommented:
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
 
cbonesAuthor Commented:
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
 
RobSampsonCommented:
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
 
cbonesAuthor Commented:
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
 
RobSampsonCommented:
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
 
cbonesAuthor Commented:
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
 
cbonesAuthor Commented:
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
 
RobSampsonCommented:
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
 
cbonesAuthor Commented:
Rob,

That did the trick.  Thank you for all your help!.
0
 
RobSampsonCommented:
Great!  No problem.  Thanks for the grade.

Rob.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 11
  • 11
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now