在vbscript中获取当前目录

时间:2013-04-22 03:08:53

标签: vbscript directory

我试图获取当前目录并使用它来运行应用程序,无论文件放在何处,无论路径如何更改

    dim fso: set fso = CreateObject("Scripting.FileSystemObject")
    dim CurrentDirectory
    CurrentDirectory = fso.GetAbsolutePathName(".")
    dim Directory
    Directory = CurrentDirectory\attribute.exe

    Set WinScriptHost = CreateObject("WScript.Shell")
    WinScriptHost.Run Chr(34) & "Directory" & Chr(34), 0
    Set WinScriptHost = Nothing

我如何实际设置此代码,以便它做我想要它正确做的事情? 提前谢谢!

7 个答案:

答案 0 :(得分:37)

您可以使用 WScript.ScriptFullName ,它将返回执行脚本的完整路径。

您可以使用字符串操作( jscript示例):

scriptdir = WScript.ScriptFullName.substring(0,WScript.ScriptFullName.lastIndexOf(WScript.ScriptName)-1)


或者从FileSystemObject获取帮助,( vbscript示例):

scriptdir = CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName)

答案 1 :(得分:25)

您可以使用CurrentDirectory属性。

Dim WshShell, strCurDir
Set WshShell = CreateObject("WScript.Shell")
strCurDir    = WshShell.CurrentDirectory
WshShell.Run strCurDir & "\attribute.exe", 0
Set WshShell = Nothing

答案 2 :(得分:23)

您的问题是没有获取目录(fso.GetAbsolutePathName(".")解析当前工作目录就好了)。即使你想要脚本目录而不是当前的工作目录,你也可以轻松地确定正如Jakob Sternberg在his answer中所描述的那样。

在代码中不起作用的是从目录和可执行文件构建路径。这是无效的语法:

Directory = CurrentDirectory\attribute.exe

如果要从变量和文件名构建路径,则必须将文件名指定为字符串(或包含字符串的变量),并与变量目录变量连接:

Directory = CurrentDirectory & "\attribute.exe"

或(更好)使用BuildPath方法构建路径:

Directory = fso.BuildPath(CurrentDirectory, "attribute.exe")

答案 3 :(得分:5)

你的行

Directory = CurrentDirectory\attribute.exe

与我在vbscript说明手册中遇到的任何功能都不匹配。 以下对我有用,不知道你期望“attribute.exe”驻留在哪里/哪里。

dim fso
dim curDir
dim WinScriptHost
set fso = CreateObject("Scripting.FileSystemObject")
curDir = fso.GetAbsolutePathName(".")
set fso = nothing
Set WinScriptHost = CreateObject("WScript.Shell")
WinScriptHost.Run curDir & "\testme.bat", 1
set WinScriptHost = nothing

答案 4 :(得分:5)

'-----Implementation of VB6 App object in VBScript-----
Class clsApplication
    Property Get Path()
          Dim sTmp
          If IsObject(Server) Then
               'Classic ASP
               Path = Server.MapPath("../")
          ElseIf IsObject(WScript) Then 
               'Windows Scripting Host
               Path = Left(WScript.ScriptFullName, InStr(WScript.ScriptFullName, WScript.ScriptName) - 2)
          ElseIf IsObject(window) Then
               'Internet Explorer HTML Application (HTA)
               sTmp = Replace( Replace(Unescape(window.location), "file:///", "") ,"/", "\")
               Path = Left(sTmp, InstrRev( sTmp , "\") - 1)
          End If
    End Property
End Class
Dim App : Set App = New clsApplication 'use as App.Path

答案 5 :(得分:0)

Usin with…End With Statement

Example With … End With Statement

''''Way 1
currentdir=Left(WScript.ScriptFullName,InStrRev(WScript.ScriptFullName,"\"))


''''Way 2

With CreateObject("WScript.Shell")
CurrentPath=.CurrentDirectory
End With


''''Way 3

With WSH
CurrentDirr=Replace(.ScriptFullName,.ScriptName,"")
End With

答案 6 :(得分:-1)

简单:

            <Drawer
                docked={false}
                onRequestChange={(drawerOpen) => this.setState({ drawerOpen })}
                open={this.state.drawerOpen}>
                <Menu>
                    <MenuItem
                        containerElement={<Link to="/" />}
                        primaryText="Home"
                    />
                </Menu>
                <BottomMenu>
                    <MenuItem
                        containerElement={<Link to="/asd" />}
                        primaryText="asd"
                    />
                </BottomMenu>
            </Drawer>