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

VBScript to open directory based on user input? 3

Status
Not open for further replies.

PRJ2011

MIS
Apr 5, 2011
3
US
Hello,

I am new here and not a scripting expert. I am trying to make a script that will ask a user for their name and then open their home directory on the server. I have mapped the root of the home directories to Z:\ . The problem I have is that I keep getting an error message after putting in the name that says "The system cannot find the file specified." Code: 80070002 Source: (null)

I got the script from and modified it because it seemed close to what I am trying to do, except that I don't need to open a file within the folder. Can anyone see what I did wrong. Thanks.

CONST strDir = "Z:\"

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 your name:")
strWeb = findFolder(strDir, strFlag)
objShell.Run strWeb
 
It's that last line, I think. findFolder returns a file path string that you are trying to .Run. Include some conditional code in there. It's possible the folder wasn't found thus strWeb = ""

Code:
CONST strDir = "Z:\"

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 your name:")
strWeb = findFolder(strDir, strFlag)

[red]if (objFSO.FolderExists(strWeb)) then
   msgbox "Opening Folder " & strWeb"[/red]
   objShell.Run strWeb 
[red]else
   msgbox "Folder not found: " & vbNewLine & strWeb
end if[/red]

-Geates

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live."
- Martin Golding

"There are seldom good technological solutions to behavioral problems."
- Ed Crowley, Exchange guru and technology curmudgeon
 
Thanks for the help. I still can't get it to work though. It will tell me it is opening the person's folder, but it still says it cannot find it after I close the input box.
 
And this ?
objShell.Run Chr(34) & strWeb & Chr(34)

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I tested the code, seems to work fine.

PRJ2011 said:
It will tell me it is opening the person's folder, but it still says it cannot find it after I close the input box.

What exactly does it say? Is it a windows error message?
 
Ah. The problem is the exit function. Because the function is recursive, exit function simply exits to the parent level instead of completely. Change the "exit function" to "exit for" and add a line of code to test if findFolder is set or not. Also, cast your strFlag and objSubFolder.Name as lowercase to avoid typos.

Code:
function findFolder(strDir, strFlag)
    set objFolder = objFSO.GetFolder(strDir)
    for each objSubFolder in objFolder.SubFolders
		if (inStr([red]lcase[/red](objSubFolder.Name), [red]lcase[/red](strFlag))) then
			findFolder = objSubFolder.Path
            [red]exit for[/red]
        else
            findFolder = findFolder (objSubFolder.Path, strFlag)
        end if
    next
    [red]if (len(findFolder) <> 0) then exit function[/red]
end function

I initially wrote this function and probably did so in a rush and didn't test it.

-Geates

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live."
- Martin Golding

"There are seldom good technological solutions to behavioral problems."
- Ed Crowley, Exchange guru and technology curmudgeon
 
guitarzan said:
I tested the code, seems to work fine.

This is true if the folder is found on the top level of the directory structure.

-Geates

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live."
- Martin Golding

"There are seldom good technological solutions to behavioral problems."
- Ed Crowley, Exchange guru and technology curmudgeon
 
ahhhh I suspect PHV nailed it... My test folders didn't have spaces... surrounding the path with quotes will fix that
 
Geates: The code with your first mod worked fine, even if the target folder was several levels deep.. Except objShell.Run will fail if the target has spaces, unless surrounded by double-quotes (though, FSO's FolderExists doesn't require quotes)
 
Although Guitarzan and PHV are correct, the problem isn't that the target path may have spaces, but rather, that the function returns an empty value. The reason being that, even if findFolder equals something, unless it is that last folder in the dir struct, it will eventually become empty. Therefore, the issue lies within the function.

Testing the folder to see if it's match should be done after the subfolder are tested. Moving the test outside of the for..loop will acomplish this.

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

NOTE: This function will return the DEEPEST folder. Moving the test before the for..loop will return the FIRST ENCOUNTER

-Geates

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live."
- Martin Golding

"There are seldom good technological solutions to behavioral problems."
- Ed Crowley, Exchange guru and technology curmudgeon
 
Thanks so much for all of your help. It is definitely on the right track. The script works. I had to map a drive with credentials so that it finds the "Z:\" variable. The only things that I notice is that it is case sensitive and will open the first folder in the directory if you put nothing in there. Also, is there a way to make it require first and last name? If you have 5 Bobs it will open the first Bob directory.

I am happy that it is working. It does find the correct directory if you enter first and last name.

Thanks again for all of the help.
 
Validating wether or not the name entered contains a first and last name it close to impossible because of the way people interpret "things" differently.

"There are seldom good technological solutions to behavioral problems."

However, you could simply say it's required in the prompt but that doesn't gaurentee they'll do it. :) For simplcity (considering the aforementioned quote)

Code:
strName = inputBox "Enter your first and last name (e.g. Mike Jones)"

Keep in mind that because the will be a space in there name, the eventual path will also, so it needs to be encapsulated in quote (chr(13)) as to not cause an FSO error.

Code:
function findFolder(strDir, strFlag)
    set objFolder = objFSO.GetFolder([red]chr(13) & [/red] strDir [red] & chr(13)[/red])
    ...

-Geates

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live."
- Martin Golding

"There are seldom good technological solutions to behavioral problems."
- Ed Crowley, Exchange guru and technology curmudgeon
 
Geates, 2 things:
1) quote is Chr(34)
2) FSO handles pathname with space without problem.
 
I just noticed the chr(13). But I guess they are not necessary. Thanks for pointing that out.

-Geates

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live."
- Martin Golding

"There are seldom good technological solutions to behavioral problems."
- Ed Crowley, Exchange guru and technology curmudgeon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top