VBscript相对路径

Shi*_*aSM 5 vbscript

我正在尝试使用以下脚本(顺便通过批处理文件调用它)来解压缩Windows XP中的文件:

strZipFile ="C:\test.zip"                        'name of zip file
outFolder = "C:\"                                'destination folder of unzipped files

Set objShell = CreateObject( "Shell.Application" )
Set objSource = objShell.NameSpace(strZipFile).Items()
Set objTarget = objShell.NameSpace(outFolder)
intOptions = 256
objTarget.CopyHere objSource, intOptions
Run Code Online (Sandbox Code Playgroud)

问题是,在我计划使用它的地方我不会知道zip文件的完整路径,我将要知道的是它将与VBScript在同一个文件夹中,因此,考虑到这一点,可以用相对路径调用它吗?例:

strZipFile ="test.zip" 
Run Code Online (Sandbox Code Playgroud)

这个例子不起作用(它给出了一个错误"Object required:'objShell.NameSpace(...)'"),所以当然我的意思是那些可行的东西.

Ekk*_*ner 10

WScript.ScriptFullName和FSO.GetParentFolder应该可以解决您的问题:

>> p = CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName)
>>
>> WScript.Echo p
>>
M:\bin
Run Code Online (Sandbox Code Playgroud)

更新Kiril的评论:

答案"是"的证据:

Option Explicit

Class cX
  Private Sub Class_Initialize()
    WScript.Echo "Class_Initialize"
  End Sub
  Private Sub Class_Terminate()
    WScript.Echo "Class_Terminate"
  End Sub
  Public Function f()
    f = "qed"
  End Function
End Class

WScript.Echo 1
Dim f : f = (New cX).f()
WScript.Echo 2
WScript.Echo f
Run Code Online (Sandbox Code Playgroud)

输出:

cscript 15621395.vbs
1
Class_Initialize
Class_Terminate
2
qed
Run Code Online (Sandbox Code Playgroud)


Kee*_*een 6

这应该为您提供 zip 文件的完整路径:

strZipFile ="test.zip" 
dim fso, fullPathToZip
set fso = CreateObject("Scripting.FileSystemObject")
fullPathToZip = fso.GetAbsolutePathName(strZipFile)
Run Code Online (Sandbox Code Playgroud)

  • 请注意,GetAbsolutePathName 会将相对路径解释为相对于当前目录,而@Ekkehard.Horner 的解决方案假定它相对于脚本。它们可以但并不总是相同的。 (4认同)