vbscript to unzip files

A

Anonymous

Every day we get a zip file(s). For example:

20090811a.zip
20090811b.zip
20090812.zip

I need to create the appropriate folders and extract the zip file(s) into
the correct folders. For example:

A folder called 20090811a will be created and the contents of 20090811a.zip
will be extracted into that folder.

A folder called 20090811b will be created and the contents of 20090811b.zip
will be extracted into that folder and so on.

I have the following code to extract a zip but it doesn't create the folder
and I need the zip file path to locate all zip files. I tried using a
wildcard in the file name and it won't run if I do that.

pathToZipFile="c:\document\test\20090811.zip"
extractTo="c:\document\test\"

set sa=CreateObject("Shell.Application")
set filesInzip=sa.NameSpace(pathToZipFile).items
sa.NameSpace(extractTo).CopyHere(filesInzip)

Thanks!
 
P

Pegasus [MVP]

Anonymous said:
Every day we get a zip file(s). For example:

20090811a.zip
20090811b.zip
20090812.zip

I need to create the appropriate folders and extract the zip file(s) into
the correct folders. For example:

A folder called 20090811a will be created and the contents of
20090811a.zip
will be extracted into that folder.

A folder called 20090811b will be created and the contents of
20090811b.zip
will be extracted into that folder and so on.

I have the following code to extract a zip but it doesn't create the
folder
and I need the zip file path to locate all zip files. I tried using a
wildcard in the file name and it won't run if I do that.

pathToZipFile="c:\document\test\20090811.zip"
extractTo="c:\document\test\"

set sa=CreateObject("Shell.Application")
set filesInzip=sa.NameSpace(pathToZipFile).items
sa.NameSpace(extractTo).CopyHere(filesInzip)

Thanks!

Your post is a little vague about what you're trying to achieve. I assume
that it is this:
- You have a number of .zip file in a ZIP folder.
- Their names look like 20090811.zip
- You wish to create target folders based on the name of the zip files, e.g.
c:\documents\test\20090811
- You wish to extract the contents of each .zip file to the correct target
folder.

VB Scripts have many facilities that are not available in batch files. On
the other hand they are also quite chatty. In your case I see no advantage
in using a VB Script. The batch file below will do the job nicely.

@echo off
set ZipPath=c:\document\test
set ExtractPath=c:\document\test
for %%a in ("%ZipPath%\*.zip") do call :Sub "%%a"
goto :eof

:Sub
echo md "%ExtractPath%\%~n1"
echo unzip %1 "%ExtractPath%\%~n1"

You must remove the words "echo" in the last two lines in order to activate
the batch file - perhaps starting with just the first one, then the second
one. You must also use your own command line unzipper. The batch file shows
the one I use on my machine, unzip.exe.
 
C

Cheeso

Your post is a little vague about what you're trying to achieve. I assume
that it is this:
- You have a number of .zip filein a ZIP folder.
- Their names look like 20090811.zip
- You wish to create target folders based on the name of the zip files, e..g.
c:\documents\test\20090811
- You wish to extract the contents of each .zip fileto the correct target
folder.

VBScripts have many facilities that are not available in batch files. On
the other hand they are also quite chatty. In your case I see no advantage
in using aVBScript. The batch file below will do the job nicely.

@echo off
set ZipPath=c:\document\test
set ExtractPath=c:\document\test
for %%a in ("%ZipPath%\*.zip") do call :Sub "%%a"
goto :eof

:Sub
echo md "%ExtractPath%\%~n1"
echo unzip %1 "%ExtractPath%\%~n1"

You must remove the words "echo" in the last two lines in order to activate
the batch file - perhaps starting with just the first one, then the second
one. You must also use your own command line unzipper. The batch file shows
the one I use on my machine, unzip.exe.- Hide quoted text -

- Show quoted text -

No, Pegasus, the advantage to VBSCript is he can use the
Shell.Application object, which includes unzip capability. You can't
do that in a batch file.
I'm not sure what the question is, but I think it is "how do I create
a folder?" and "how do I extract ALL zip files?"

To create a folder in vbscript, try this:

Dim fso
Set fso= CreateObject("Scripting.FileSystemObject")
fqPathToZip= fso.GetAbsolutePathName(pathToZipFile)

If Not fso.FolderExists(extractLocation) Then fso.CreateFolder
(extractLocation)
fqExtractLoc= fso.GetAbsolutePathName(extractLocation)

To find and extract all zip files, you can

Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(folderspec)
Set fc = f.Files
For Each f1 in fc
''f1.name contains the file name
'' can check here if the file matches *.zip.
Next
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top