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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBScript search directory 1

Status
Not open for further replies.

dtweten

MIS
Apr 28, 2010
18
US
I am looking for a little direction and help as to create a script that will ask the user for input and then take that input and search through a directory and for a folder and finally open an index.html file in that folder. Being I am pretty new to VBScripts I am not sure if this is possible or should I being approaching this differently. Your help is appreciated.
 
a couple of picking folder dialogues? take from older posts by other authors




Function PickFolder(strStartDir As Variant) As String
Dim SA As Object, f As Object
Set SA = CreateObject("Shell.Application")
Set f = SA.BrowseForFolder(0, "Choose a folder", 0, strStartDir)
If (Not f Is Nothing) Then
PickFolder = f.Items.Item.Path
End If
Set f = Nothing
Set SA = Nothing
End Function






Const cdlOFNAllowMultiselect = 512

Set ObjFSO = CreateObject("UserAccounts.CommonDialog")
ObjFSO.Filter = "VBScripts|*.vbs|Text Documents|*.txt|All Files|*.*"
ObjFSO.FilterIndex = 3
ObjFSO.InitialDir = "c:\myscripts"
ObjFSO.Flags = cdlOFNAllowMultiselect
InitFSO = ObjFSO.ShowOpen

If InitFSO = False Then
Wscript.Echo "Script Error: Please select a file!"
Wscript.Quit
Else
Wscript.Echo "You selected the file: " & ObjFSO.FileName
End If
 
I was looking for more of something like the user would be asked a question that would provided part of the folders name and their answer would be stored in a variable that would be used to find the folder and open an index.html file within it.

Basically I want an easy way fro the users to sort through thousands of folders and open a file.
 
you say 'find the folder', but the user provides 'part' of the folder name. sounds like you are trying to match and produce a list of folders?

what sort of a question? their age, their fav colour?

a simple inputbox

strFolder = InputBox("what folder do you want?")
Msgbox "the user told me " & strFolder

i thought the PickFolder was more user friendly
 
I am a beginner at this so please forgive me but I was thinking somethings like this.

strFolder = InputBox("what folder do you want?")
set shell = createobject("WScript.shell")
web = "V:/* & strFolder */index.html
shell.Run web

So the users would be prompted for a part of the folders name and they would enter it (e.g. folders would have a naming scheme that included some id number but there would also be other characters in their name as well). Then the script would go to a directory that contained all the folders and based on the users input simply launch a common file amongst all the folders. In this case all the folders have an index.html file in them. So from the users view point they just enter the folders ID number and it will open the correct index.html file. Can this be done?
 
I feel like I may be close or on the right track with the the script I have so far, but my issue is getting the wildcards to work in the URL for the line that sets the web variable. In that line i would like to use a wildcard before and after the strFolder variable for the folder name of the URL, but cant seem to get it to work. the script will just fail when you run it saying it cant locate the file.
 
In case I have completely made a mess of trying to describe to you what I am ultimately trying to do, I will try and clarify that some more now. So I have a folder that contains many other subfolders labeled similar to this

1.2.276.0.26.1.1.1.2.2007.219.24829.3764145.1_2008-01-15_11-32-49

The subfolder names will never be the same. In the example I provided I wan the user of the script to be able to simply enter part of the subfolder name like 24829 and then being all of these sub folders contain a file named index.html I want the script to then locate the subfolder based on the partial folder name they provided and then launch the index.html file located with in it. I am not even sure if this is possible or if my approach is the correct or best approach to the solution. I am currently trying to use "wildcards" in my script to obtain this outcome but with no success. Any additional insight would be helpful. Thanks
 
Will the user always enter a part of the name that uniquely identifies the folder? If the user types in "2008" will there be only 1 match or could there be multiples? If there are multiples, how do you know which index.html to launch (or does it even matter)?
 
Good questions, yes the user will only enter a unique part of the subfolder's name; the example I have earlier would be that unique identifier. If you did enter 2008 I am sure the script would fail or I hope it would. There is only one index.html file per subfolder
 
How are you searching for the folder? If you are iterating through the folders you can use the InStr() function to test if the user's input is contained in the folder's name.
 
forgive me for being ignorant but I cant see how that function will work for me based on the synax I have found

<script type="text/vbscript">

txt="This is a beautiful day!"
document.write(InStr(txt,"beautiful"))

</script>

Can you provide an example of how i would integrate that into my scenario?

To answer you question I am searching for the subfolder based off the users input and then placing that variable from their input into the location address of the file with wildcards in place around the variable.

strFolder = InputBox("Enter Folder ID")
web = "V:/* & strFolder */index.html

Is that possible and if not what would be a better way to accomplish this? Would the InStr() function really work? It looks like its used to search text within your script and then return the location of that text in relation to lines and charters.

 
The InStr() function can tell you whether the user's input is in the folder name.

If you have the folder name in some variable and the user's input you can do this:
if InStr(strFolderName, strUserInput) > 0 then
'the user's input is in the folder's name
'open the index.html file in this folder
 
Ok that makes logical sense but how do you tell it to open the index file in that folder and then i would have to have a ton of variables with all the folder names tied to them for this to work. As new folders are added to the parent folder then I would have to continue to edit the script.
 
You can thank mrmovie for posting a FAQ on a recursive search: faq329-5515

Look at the part that searches through the folders, this should give you some direction as to how to code the search. As you loop through the folders, when you find a match you use that folder in your Run command.
 
Recursively traverse a directory looking for the folder name containing strFlag. Once found, return the path and append "index.htm". Using a shell object, run the full path.

Pretty much what everyone else said :)

Code:
CONST strDir = "V:\"

set objShell = CreateObject("WScript.Shell")
set objFSO = CreateObject("Scripting.FileSystemObject")

function findFolder(strDir, strFlag)
	set objFolder = objFSO.GetFolder(strDir)
	for each objSubFolder in objFolder.SubFolders
		if (inStr(objSubFolder.Name, strFlag)) then
			findFolder = objSubFolder.Path
			exit function
		else		
			findFolder = findFolder (objSubFolder.Path, strFlag)
		end if
   next
end function

strFlag = inputBox("Enter partial directory name:")
strWeb = findFolder(strDir, strFlag) & "\index.htm"
objShell.Run strWeb

Because all folder names are unique, the script returns the first match, as it is expected there is no other.

Also, this function is case-sensitive, thus reducing the number of false positives.

-Geates
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top