VBscript 相对路径 [英] VBscript relative path
问题描述
我正在尝试使用以下脚本(顺便说一下,它被批处理文件调用)在 Windows XP 中解压缩文件:
I'm trying to use the following script (It's being called by a Batch file, by the way) to unzip files in 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
问题是,我计划使用它的地方我不知道 zip 文件的完整路径,我只知道它将与 VBScript 位于同一文件夹中,因此,在请注意,是否可以用相对路径调用它?示例:
Problem is, where I plan to use it I won't know the full path of the zip file, all I'm going to know is that it will be in the same folder as the VBScript, so, with that in mind, would it be possible to call it with a relative path? Example:
strZipFile ="test.zip"
但是,这个例子不起作用(它给出了一个错误需要对象:'objShell.NameSpace(...)'"),所以当然我的意思是那些可以工作的东西.
This example doesn't work, though (It gives an error "Object required: 'objShell.NameSpace(...)' "), so of course I mean something along those lines that would work.
推荐答案
WScript.ScriptFullName 和 FSO.GetParentFolder 应该可以解决您的问题:
WScript.ScriptFullName and FSO.GetParentFolder should solve your problem:
>> p = CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName)
>>
>> WScript.Echo p
>>
M:\bin
更新基里尔的评论:
回答是"的证据:
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
输出:
cscript 15621395.vbs
1
Class_Initialize
Class_Terminate
2
qed
这篇关于VBscript 相对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!