7

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.

BDM
  • 3,760
  • 3
  • 19
  • 27
ShizukaSM
  • 343
  • 2
  • 5
  • 15

3 Answers3

11

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
Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96
6

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)
Keen
  • 1,327
  • 1
  • 17
  • 25
  • +1 Since your answer is also valid and different from the previous one, thanks! – ShizukaSM Mar 25 '13 at 18:13
  • @ShizukaSM There's more than one way to script a cat! :D – Keen Mar 25 '13 at 18:19
  • 4
    Note that GetAbsolutePathName will interpret a relative path as relative to the current directory, whereas @Ekkehard.Horner's solution assumes it relative to the script. They can be but aren't always the same. – peterchen Dec 05 '13 at 10:49
2

When a "File not found error" occurs, in a situation where you're trying to read a data file from the script's directory (or a subfolder thereof) via a relative path, the usual problem is an assumption that the current directory is the script directory. Depending on how the script is launched, the current directory could be C:\Windows\System32 (location of WScript.exe, CScript.exe, MSHTA.exe) or any directory from which the script was called (e.g. if run from another script or via the command line).

The following code sets the current directory to the script's directory (line 4), using the same method as in the accepted answer. Once that's done, you can reliably read your data file via a relative path. If the data file is in a subfolder of the script's directory then a dot prefix will be needed (e.g. ".\data\myfile.txt"). If the data file is in the script's directory, no prefix is needed at all, but I usually use ".\".

Const ForReading = 1
Set oWSH = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
oWSH.CurrentDirectory = oFSO.GetParentFolderName(WScript.ScriptFullName)
DataFile = ".\myfile.txt"
Set oFile = oFSO.OpenTextFile(DataFile,ForReading)
Data = oFile.ReadAll
oFile.Close
WScript.Echo Data
LesFerch
  • 1,540
  • 2
  • 5
  • 21