客户端 VBScript 应用程序,当前工作目录不正确
我不理解这种行为。也许有人可以向我解释为什么我当前的工作目录不是我所期望的。
在我的桌面上,我有一个名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于服务器端:
您应该使用 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 类中使用。相反,您需要创建一个如下所示的文件系统对象:
如果有帮助,这里是在 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 inC:\Documents and Settings\Lauren\Desktop\STKGui\SaveData\SaveData.accdb
, your app root beingC:\Documents and Settings\Lauren\Desktop\STKGui
, you would useServer.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:
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/
这个解决方案并不理想,但我最终所做的是解析浏览器中的 url 以获取目录。
就像我说的,不太理想。一旦我使用将要运行的应用程序,它甚至可能无法工作。但我找不到更好的方法。
This solution was NOT ideal, but what I ended up doing was parsing the url in my browser to get the directory.
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.