Getting current directory in VBScript

后端 未结 7 2362
天命终不由人
天命终不由人 2020-11-29 02:45

I\'m trying to get the current directory and use it to run an application no matter where the file is put and no matter how the path is changed

Dim fso: set          


        
相关标签:
7条回答
  • 2020-11-29 03:17

    You can use CurrentDirectory property.

    Dim WshShell, strCurDir
    Set WshShell = CreateObject("WScript.Shell")
    strCurDir    = WshShell.CurrentDirectory
    WshShell.Run strCurDir & "\attribute.exe", 0
    Set WshShell = Nothing
    
    0 讨论(0)
  • 2020-11-29 03:21

    Use With in the code.

    Try this way :

    ''''Way 1
    
    currentdir=Left(WScript.ScriptFullName,InStrRev(WScript.ScriptFullName,"\"))
    
    
    ''''Way 2
    
    With CreateObject("WScript.Shell")
    CurrentPath=.CurrentDirectory
    End With
    
    
    ''''Way 3
    
    With WSH
    CD=Replace(.ScriptFullName,.ScriptName,"")
    End With
    
    0 讨论(0)
  • 2020-11-29 03:22
    '-----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
    
    0 讨论(0)
  • 2020-11-29 03:23

    simple:

    scriptdir = replace(WScript.ScriptFullName,WScript.ScriptName,"")
    
    0 讨论(0)
  • 2020-11-29 03:27

    Your line

    Directory = CurrentDirectory\attribute.exe
    

    does not match any feature I have encountered in a vbscript instruction manual. The following works for me, tho not sure what/where you expect "attribute.exe" to reside.

    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
    
    0 讨论(0)
  • 2020-11-29 03:32

    Your problem is not getting the directory (fso.GetAbsolutePathName(".") resolves the current working directory just fine). Even if you wanted the script directory instead of the current working directory, you could easily determine that as Jakob Sternberg described in his answer.

    What does not work in your code is building a path from the directory and your executable. This is invalid syntax:

    Directory = CurrentDirectory\attribute.exe

    If you want to build a path from a variable and a file name, the file name must be specified as a string (or a variable containing a string) and either concatenated with the variable directory variable:

    Directory = CurrentDirectory & "\attribute.exe"
    

    or (better) you construct the path using the BuildPath method:

    Directory = fso.BuildPath(CurrentDirectory, "attribute.exe")
    
    0 讨论(0)
提交回复
热议问题