客户端 VBScript 应用程序,当前工作目录不正确

发布于 2024-09-10 01:16:43 字数 1775 浏览 2 评论 0原文

我不理解这种行为。也许有人可以向我解释为什么我当前的工作目录不是我所期望的。

在我的桌面上,我有一个名为 STKGui 的文件夹:

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

位于该目录中的是以下文件: gui.html、style.css、save.html、load.html 在 STKGui 中有还有以下目录:Images、Scripts 和 SaveData。脚本包含各种 .vbs 文件,包括 gui.vbs。

我从 gui.html 开始。我单击一个按钮,将我带到 load.html。 load.html 使用 Scripts\gui.vbs 中的脚本。其中一个函数加载数据库,为此我提供了数据库的位置: C:\Documents and Settings\Lauren\Desktop\STKGui\SaveData\SaveData.accdb 当然,我想使用相对文件路径而不是一条固定的路径。我最初尝试加载数据库失败了;它试图从 C:\Documents and Settings\Lauren\Desktop\SaveData\SaveData.accdb 加载。因此,为了排除故障,我打印出了当前的工作目录;令我懊恼的是 C:\Documents and Settings\Lauren\Desktop

我不明白为什么我的桌面是我当前的工作目录。它不应该是文件运行的地方吗?我认为它是 C:\Documents and Settings\Lauren\Desktop\STKGui (load.html 的位置)或 C:\Documents and Settings\Lauren\Desktop\STKGui\Scripts (gui.vbs 的位置,其中包含尝试加载数据库/打印当前工作目录的调试消息的函数)。

有人可以解释为什么当前工作目录是这样的,或者更好地告诉我如何获得我真正想要的东西,即文件执行的位置? (我不在乎它是主 STKGui 文件夹还是脚本文件夹 - 只要它在应用程序的目录结构内,我就可以使用它!)


编辑(7/14/10 4:02 pm EDT):

各种尝试在打印当前工作目录或根据我认为的执行脚本的相对路径抓取文件时,会导致我的桌面路径而不是执行脚本的路径。我偶然发现了这个链接: http://leereid .wordpress.com/2008/03/19/vbscript-current-directory-or-folder/ 但没有一个解决方案对我有用,因为我收到有关 Wscript 对象的运行时错误。因此,虽然我不知道上述链接上的任何解决方案是否会产生不同的结果,但如果有人可以帮助我至少其中一个解决方案工作,这样我就可以发现这可能是朝着正确方向迈出的一步。

下面复制的解决方案之一:

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

产生以下错误:

需要对象:'Wscript'行:659 字符:1,

第 659 行是:

oShell.CurrentDirectory = ofso.GetParentFolderName(Wscript.ScriptFullName)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

总以为 2024-09-17 01:16:43

对于服务器端:

您应该使用 Server.MapPath() 来获取“工作目录”。例如,如果您想获取 C:\Documents and Settings\Lauren\Desktop\STKGui\SaveData\SaveData.accdb 中数据库文件的路径,您的应用根目录为 C: \Documents and Settings\Lauren\Desktop\STKGui,您将使用Server.MapPath("SaveData\SaveData.accdb")

对于客户端:

经过仔细检查和挖掘一些记忆,我意识到 MapPath 只能从 Server 类中使用。相反,您需要创建一个如下所示的文件系统对象:

''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)

如果有帮助,这里是在 vbScript 中使用文件系统的绝佳资源: http://www.activexperts.com/activmonitor/windowsmanagement/adminscripts/filesfolders/files/

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/

欢烬 2024-09-17 01:16:43

这个解决方案并不理想,但我最终所做的是解析浏览器中的 url 以获取目录。

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"

就像我说的,不太理想。一旦我使用将要运行的应用程序,它甚至可能无法工作。但我找不到更好的方法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文