Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBscript relative path

Tags:

vbscript

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

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" 

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.

like image 560
ShizukaSM Avatar asked Mar 25 '13 17:03

ShizukaSM


2 Answers

WScript.ScriptFullName and FSO.GetParentFolder should solve your problem:

>> p = CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName)
>>
>> WScript.Echo p
>>
M:\bin

Update wrt Kiril's comment:

Evidence for the answer "Yes":

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

output:

cscript 15621395.vbs
1
Class_Initialize
Class_Terminate
2
qed
like image 192
Ekkehard.Horner Avatar answered Sep 25 '22 22:09

Ekkehard.Horner


This should get you the zip file's full path:

strZipFile ="test.zip" 
dim fso, fullPathToZip
set fso = CreateObject("Scripting.FileSystemObject")
fullPathToZip = fso.GetAbsolutePathName(strZipFile)
like image 41
Keen Avatar answered Sep 26 '22 22:09

Keen