tags:

views:

48

answers:

2

I'm not understanding this behavior. Maybe someone can explain to me why my current working directory is not what I expect.

On my desktop, I have a folder called STKGui:

C:\Documents and Settings\Lauren\Desktop\STKGui

Located in that directory are the following files: gui.html, style.css, save.html, load.html Within STKGui there are also the following directories: Images, Scripts, and SaveData. Scripts contains various .vbs files, including gui.vbs.

I start with gui.html. I click a button which takes me to load.html. load.html uses scripts from Scripts\gui.vbs. One of the functions loads a database, and to do so I provide the location of the database: C:\Documents and Settings\Lauren\Desktop\STKGui\SaveData\SaveData.accdb Of course I want to use a relative file path instead of a fixed path. My initial attempt to load the database failed; it was trying to load from C:\Documents and Settings\Lauren\Desktop\SaveData\SaveData.accdb. So to troubleshoot I printed out the current working directory; much to my chagrin it was C:\Documents and Settings\Lauren\Desktop

I don't understand why my desktop is my current working directory. Shouldn't it be where the file is running from? I figured it would be either C:\Documents and Settings\Lauren\Desktop\STKGui (the location of load.html) OR C:\Documents and Settings\Lauren\Desktop\STKGui\Scripts (the location of gui.vbs which contains the function that's trying to load the database/printing debug messages of the current working directory).

Can someone explain why the current working directory is what it is, or better yet tell me how to get what I really want, which is the location of the files executing? (I don't care if it's the main STKGui folder or the scripts folder--as long as it's within the application's directory structure I can work with it!)


EDIT (7/14/10 4:02 pm EDT):

Various attempts at printing the current working directory or grabbing files based on what I -thought- was the relative path from my executing script have resulted in my desktop's path instead of the path of the executed script. I stumbled across this link: http://leereid.wordpress.com/2008/03/19/vbscript-current-directory-or-folder/ but none of the solutions are working for me, as I get run-time errors regarding the Wscript object. So while I don't know if any of the solutions on the aforementioned link will produce different results, if someone can help me get at least one of them working so I can find out that may be a step in the right direction.

One of the solutions, reproduced below:

Set oShell = CreateObject("WScript.Shell")
Set ofso = CreateObject("Scripting.FileSystemObject")
oShell.CurrentDirectory = ofso.GetParentFolderName(Wscript.ScriptFullName)

produces the following error:

Object required: 'Wscript' line: 659 char: 1

with line 659 being:

oShell.CurrentDirectory = ofso.GetParentFolderName(Wscript.ScriptFullName)
+2  A: 

For Server-Side:

You should be using Server.MapPath() to get your "working directory". For instance, if you want to get the path to your database file in C:\Documents and Settings\Lauren\Desktop\STKGui\SaveData\SaveData.accdb, your app root being C:\Documents and Settings\Lauren\Desktop\STKGui, you would use Server.MapPath("SaveData\SaveData.accdb").

For Client-Side:

Upon closer examination and digging up some memories, I realized that MapPath is only available from the Server class. Instead, you need to create a file system object like this:

''get fs object
Set objFSO = CreateObject("Scripting.FileSystemObject")
''get actual file using path relative to calling vbs file
Set objFile = objFSO.GetFile("SaveData\SaveData.accdb")
''get path to the database
set sPathToDatabase = objFSO.GetAbsolutePathName(objFile)

In case it helps, here is a great resource for working with the file system in vbScript: http://www.activexperts.com/activmonitor/windowsmanagement/adminscripts/filesfolders/files/

Byron Sommardahl
This is client-side, not server-side stuff. Strange, I know, but it's for an application that will load this into its interface in an html viewer.
Lauren
Lauren
Can you post the entire error message and verify the line number?
Byron Sommardahl
Type mismatch: 'MapPath' line: 652 char: 2 gui.vbs line 652 is: Set sCurPath = MapPath("SaveData\SaveData.accdb") Sorry for the poor formatting, not sure how to format better in comments.
Lauren
Ok. Just making sure the mappath line is really the offending line. Gimme a sec and I'll try it out.
Byron Sommardahl
It is still giving me this for my filepath:C:\Documents and Settings\Lauren\Desktop\SaveData\SaveData.accdb completely ignoring my STKGui directory! Any idea why this is happening?
Lauren
As a side note, if I change the code to sPathToDatabase = objFSO.GetAbsolutePathName("SaveData.accdb"), sPathToDatabase equals C:\Documents and Settings\Lauren\Desktop\SaveData.accdb --it's just appending whatever I put to my desktop path. I don't understand. :(
Lauren
Ok. I believe I've got it now.
Byron Sommardahl
Your code isn't wrong. I believe what you had before was also correct. I think the problem here is that as per your comment ''get actual file using path relative to calling vbs file, you and I are both expecting it to use a path relative to the calling vbs file, but this is not so. For whatever reason, it keeps referring to my desktop no matter what. When I used Set objFile = objFSO.GetFile("SaveData\SaveData.accdb"); sPathToDatabase = objFSO.GetAbsolutePathName(objFile), (; inserted to show line break), it told me the file didn't exist. (It didn't print the full path of the file it was...
Lauren
attempting to grab so I just had to guess that it was still trying to grab Desktop\SaveData\SaveData.accdb.) Set objFile = objFSO.GetFile("STKGuiI\SaveData\SaveData.accdb");''get path to the database;sPathToDatabase = objFSO.GetAbsolutePathName(objFile); worked. As an experiment, I copied my STKGui folder into another location--My Documents--and tried running the application from there. It still worked but only because I hadn't removed the original folder from the desktop: it was still printing that the location of the database was Desktop\STKGui\SaveData\SaveData.accdb ...
Lauren
Crap. I'm at a loss, then. Best of luck to you.
Byron Sommardahl
If you take a look at this: http://leereid.wordpress.com/2008/03/19/vbscript-current-directory-or-folder/ I'm beginning to think that my current working directory is not the same as the location my script is running from. So I tried the approaches listed on that site to get the directory my script is running from. Problem is, I keep getting a really unhelpful error message that tells me Object Required: Wscript. I have no idea what this error message means, but the code for the first bullet in the list under "Edit:..." throws that error on the very last line.
Lauren
Thanks anyway, Byron! I really do appreciate your help. :)
Lauren
A: 

This solution was NOT ideal, but what I ended up doing was parsing the url in my browser to get the directory.

guiPath = Mid(location.PathName, 2, len(location.PathName))

Set regExp = New RegExp
regExp.IgnoreCase = False
regExp.Global = True
regExp.Pattern = ".*/"

Set matchCollection = regExp.Execute(guiPath)

Set match = matchCollection(0)

guiPath = match.value

regExp.Pattern = "%20"

guiPath = regExp.Replace(guiPath, " ")

systemsDBPath = guiPath & "SaveData\SaveData.accdb"

Like I said, less than ideal. May not even work once I'm working with the application this will be running in. But I couldn't find a better way.

Lauren