Script to Unzip

Hello,

I need a scrip/bat file that would unzip a *.zip file (located on the network share) and subsequently delete the original .zip, so only the unzipped file is left in the directory. The problem is that I am not sure what would be the file name for the zip file.
exhuserAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NVITCommented:
If you don't already have one, you would need a 3rd party zip/unzip program, e.g. 7-Zip.

> ...I am not sure what would be the file name for the zip file.
You could make a .bat file that checks the directory for files. Then unzip all of them or a certain .zip file, assuming you know beforehand, part of or the whole filename.

If there is just 1 zip file...

@echo off
set SrcDir=\\server\share\folderofzipfile
set TgtDir=\\server\share\folderofzipfile

pushd %SrcDir%
for %%a in (*.zip) do (
  "%ProgramFiles%\7-Zip\7z.exe" e "%%a"
  if %errorlevel% equ 0 del "%%a"
)
popd

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
exhuserAuthor Commented:
I would use 7 ZIP, but I don't see any options when I try to execute it with /? It does not appear script friendly to me.
0
NVITCommented:
My code in the last post would actually process all .zip files, not just 1.

In the code...
- Adjust SrcDir value to your needs
- Remove the set TgtDir line since it isn't used.

> I would use 7 ZIP, but I don't see any options when I try to execute it with /?
When installed, 7-zip doesn't add itself to the PATH. You can either...
- Add a 7z registry entry to "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths"
- Call it directly via "%ProgramFiles%\7-Zip\7z.exe"
- Make a 7z.bat containing "%ProgramFiles%\7-Zip\7z.exe" %1
- Add "%ProgramFiles%\7-Zip\7z.exe" to the PATH variable.

Update: I checked an XP station I'm on and 7zip already has the registry key I mentioned. But, it doesn't work. So, use the other 3 options I mentioned above.

> I would use 7 ZIP, but I don't see any options when I try to execute it with /? It does not appear script friendly to me.
7z -? shows the options. I prefer the help file (.chm) myself.
IMO, it isn't anymore complicated than other command line zip tools, e.g. pkzip. It's just a matter of learning their unique syntax.

The code I posted does answer your need.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

exhuserAuthor Commented:
Question:
The line below will delete the source file if the process was successful, correct?

 if %errorlevel% equ 0 del "%%a"
0
NVITCommented:
> The line below will delete the source file if the process was successful, correct?
Yes. I have tested it. If possible, you should test it on a sample folder, also. Just to be sure.
0
exhuserAuthor Commented:
Also, how would I use 'TgtDir' if I wanted to unzip to a different directory?
0
NVITCommented:
> ...how would I use 'TgtDir' if I wanted to unzip to a different directory?
e.g.
set TgtDir=\\server\share\adifferentfolder

Open in new window

...or
set TgtDir=c:\testfolder

Open in new window

I think you get the idea.
0
Bill PrewCommented:
@NVIT,

They might need help with referencing that variable in the 7z command line...

~bp
1
NVITCommented:
BTW, here's a site I use for learning more about the various Windows Batch (CMD) commands. In addition to the usual reference, it has examples and related commands, too. e.g. for the commands used in my code:

SET: http://ss64.com/nt/set.html
FOR: http://ss64.com/nt/for.html
PUSHD: http://ss64.com/nt/pushd.html
POPD: http://ss64.com/nt/popd.html
IF: http://ss64.com/nt/if.html
0
NVITCommented:
Good catch, Bill.

This version does some error checking and logging to a file UnzipAndDelSrc.txt in the %TEMP% folder:
@echo off
if "%PROCESSOR_ARCHITECTURE%" equ "AMD64" (set PrgDir=%ProgramFiles(x86)%) else (set PrgDir=%ProgramFiles%)

set SrcDir=\\server\share\folderofzipfile
set TgtDir=\\server\share\folderofzipfile

set LogFN=%temp%\UnzipAndDelSrc.txt
>> "%LogFN%" echo %date% %time% BEGIN

if not exist "%PrgDir%\7-Zip\7z.exe" >> "%LogFN%" echo %date% %time% Missing 7-zip program & goto :eof
if not exist "%SrcDir%\*." >> "%LogFN%" echo %date% %time% Missing source "%SrcDir%" & goto :eof
if not exist "%TgtDir%\*." >> "%LogFN%" echo %date% %time% Missing target "%TgtDir%" & goto :eof
pushd %SrcDir%
for %%a in (*.zip) do (
  "%PrgDir%\7-Zip\7z.exe" e "%%a"
  if %errorlevel% equ 0 (
    del "%%a" & >> "%LogFN%" echo Successfully unzipped. Deleted source %%a
  ) else (
    >> "%LogFN%" echo %date% %time% Errors during unzip
  )
)
popd
>> "%LogFN%" echo %date% %time% END

Open in new window

0
exhuserAuthor Commented:
I am getting "%%a was unexpected at this time" error. Right after the "for" statement.

Please advise.
0
NVITCommented:
Are you pasting my code to a file?

Save it to a filename like UnzipAndDelSrc.bat (or WhateverNameYouLike.bat)

Then, in a CMD window...
- Type UnzipAndDelSrc.bat
- Press ENTER
0
exhuserAuthor Commented:
With the last bat, I get ") unexpected at this time"...
0
NVITCommented:
Please change Line 2 to:
if /i "%PROCESSOR_ARCHITECTURE%" equ "AMD64" (set PrgDir=C:\Program Files ^(x86^)) else (set PrgDir=C:\Program Files)

Open in new window

0
Bill PrewCommented:
FYI, here's how I solve that type of problem:

if "%PROCESSOR_ARCHITECTURE%" equ "AMD64" (set "PrgDir=%ProgramFiles(x86)%") else (set "PrgDir=%ProgramFiles%")

Open in new window

~bp
0
exhuserAuthor Commented:
Btw, which version of 7-Zip are you using?
0
Daz_1234Commented:
Hi, 7-zip is an excellent tool, and the solution offered above should work fine.  However you don't need a zip program to do unzipping programmatically.  

Attached is a stand-alone VBScript I wrote a while ago that unzips for you.  The script is portable and can be called from a batch file.   The syntax is described in the attached "Unzip.vbs - ReadMe.txt" text file.

If you decided to test this, you would need to download the unzip.vbs script below and amend NVIT's script - I had to change couple of things as I couldn't get the For line to work with the pushd method.  See pasted amended batch below which I have tested - line 15 is the main functional change as it uses my VBScript and has no 7-zip dependency  (imagine you filed unzip.vbs into c:\temp):

I also REM'd the echo off and added a pause at the end for testing.  Apart from that I have reused NVIT's script (so please don't forget to credit him if you choose this method).

REM @echo off


set SrcDir=\\server\share\folderofzipfile
set TgtDir=\\server\share\folderofzipfile

set LogFN=%temp%\UnzipAndDelSrc.txt
>> "%LogFN%" echo %date% %time% BEGIN


if not exist "%SrcDir%\*." >> "%LogFN%" echo %date% %time% Missing source "%SrcDir%" & goto :eof
if not exist "%TgtDir%\*." >> "%LogFN%" echo %date% %time% Missing target "%TgtDir%" & goto :eof

for /F %%a in ('dir /b "%SrcDir%\*.zip"') do (
  "c:\temp\unzip.vbs" /source:"%SrcDir%\%%a" /target:"%TgtDir%\" /testfile:notest
  if %errorlevel% equ 0 (
    del "%%a" & >> "%LogFN%" echo Successfully unzipped. Deleted source %%a
  ) else (
    >> "%LogFN%" echo %date% %time% Errors during unzip
  )
)

>> "%LogFN%" echo %date% %time% END


Pause

Open in new window


Hope this helps, Daz.
Unzip.vbs---ReadMe.txt
Unzip.vbs.txt
0
exhuserAuthor Commented:
The script works with 7Zip on Windows 7. However, Windows XP gives "Unsupported Method" error and the extracted file has the size of 0k. Please advise.
0
NVITCommented:
My code was tested on XP and Win7, which works.

7-zip version on XP and Win7 = 7-Zip 9.25 alpha
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.