how to use windows zip utility from a dos command?

bfuchs
bfuchs used Ask the Experts™
on
Hi Experts,
I have the following batch file
@echo off
setlocal

set  tm=%time: =0%
set  tm=%tm::=%
set dt=%date:~10,4%%date:~4,2%%date:~7,2%

copy  C:\Users\bfuchs\Dropbox\Caspio\CaspioTables.zip  h:\CaspioTablesDaily\CaspioTables%dt%%tm:~0,4%.zip

copy  F:\Data\SkilledNursingVisitNotestbl.mdb  h:\AccessTablesDaily\Skilled_Nursing_Tables%dt%%tm:~0,4%.mdb

Open in new window


Would like to have for the second file (the access file), to zip it into the destination, instead of just copying.

How can I accomplish that?

fyi- I have windows 7.

Thanks in advance.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
The best typical approach to this is to get a copy of 7-Zip which has great command line version (and is free) and then use that for zipping.  Windows doesn't have a good command line zip tool.

7-Zip


»bp
Hi,

I have winzip95 on my pc, any idea how to use that?
also saw on google suggesting windows compact command.
would need to know how to apply here.
to get a copy of 7-Zip
Would prefer not to have to install something new if possible...


Thanks,
Ben
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
Hi bfuchs,

Winzip95 is pretty old, if you are open to get a new version please buy the latest version possible. With that said, you can get Winzip command line Add-on https://www.winzip.com/win/en/prodpagecl.html to achieve this easily.

If you don't want to upgrade, I think you will be very much interested in this undocumented Winzip options. You have to first try them in order to verify if it will work with Winzip95 verison.
winzip32 -a FILENAME.zip FILESTOBEZIPPED

Open in new window

Source: http://www.memecode.com/docs/winzip.html

Also, Windows Compact command is NTFS compression i.e. disk based compression it does not create a zip file.

And as you have tagged Scripting, I was wondering if you would want to try PowerShell? If so, you can just use
Compress-Archive -Path C:\Reference\Draftdoc.docx, C:\Reference\Images\diagram2.vsd -CompressionLevel Optimal -DestinationPath C:\Archives\Draft.Zip

Open in new window


From: https://docs.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Archive/Compress-Archive?view=powershell-5.1

Regards,
Chinmay.
Acronis in Gartner 2019 MQ for datacenter backup

It is an honor to be featured in Gartner 2019 Magic Quadrant for Datacenter Backup and Recovery Solutions. Gartner’s MQ sets a high standard and earning a place on their grid is a great affirmation that Acronis is delivering on our mission to protect all data, apps, and systems.

Dr. KlahnPrincipal Software Engineer

Commented:
Not directly from the command line, though there is a very ugly hack to accomplish it.

https://stackoverflow.com/questions/17546016/how-can-you-zip-or-unzip-from-the-script-using-only-windows-built-in-capabiliti

Better approach:  There are elderly copies (25 years old) of the DOS command line PKZIP floating around on the internet.  Either version 2.04 or version 2.5 would be suitable.  Being command line only it is relatively simple to use.

https://www.netogram.com/msdos.htm

Copy it into C:\WINDOWS\SYSTEM32 so it can be invoked from the command line.  After that everything is fairly obvious.

Command line PKZIP
Side note:  Neither of these versions understands Windows long directory names or long filenames.  The simplest way to get around this is to (a) use 8.3 file names and (b) have all files that will be zipped up resident in the command line current directory.  Otherwise you must deal with filenames of the form C:\PROGRA~1\MICROS~2\HALIFA~1\DATABA~1.SQL.  This is not an issue in more current versions, but then you must shell out to PKWARE for a newer copy.

Much better approach:  imo, GZIP or BZIP2 are better choices.  They are free, understand long filenames, and get much better compression than ZIP.
Distinguished Expert 2017

Commented:
what are you trying to do? you have a zip file to start with

are you trying to append the new files into an existing ZIP archive?

as mentioned 7-zip includes command line tools....
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
Hi bfuchs,

One more point, most of the free command utilities including 7zip are available as portable version from their authors i.e. you don't have to install *ANYTHING*.

Check the Standalone version https://www.7-zip.org/download.html
You will need 7z exe version(no need to use installation, just download the exe and use it as it is to extract the standalone version though.

Regards,
Chinmay.
Hi Experts,

Just want mention that besides for a zipping tool, I need it to store the date and time as part of the file name, as the posted DOS command does, so this way I can get it to create every time another file.

Wondering if those suggested have that option, and how to implement it?

@Chinmay,
The following is not recognized command.
winzip32 -a FILENAME.zip FILESTOBEZIPPED

Open in new window


Thanks,
Ben
Distinguished Expert 2017

Commented:
the file namei you can use the logic you have, the point being you would use he 7z.exe or one of the other components where you would specify wat you want to add to the archive /compressed file./


do the zips are wha you start with?

you psted a sriipt tht copies a ZIP, re-existing that you are renamig, and then you have an MDB that is being copied.....
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
Here is how you can do it with 7-Zip.  As mentioned, get the portable version and place in a folder the BAT file has access to, or the folder where the BAT exists, etc.  Point to it in the BAT file.  I added a simple command to ZIP the MDB to the same file name you were copying to.

@echo off
setlocal

set ZipExe=C:\util\7z.exe

set tm=%time: =0%
set tm=%tm::=%
set dt=%date:~10,4%%date:~4,2%%date:~7,2%

copy  C:\Users\bfuchs\Dropbox\Caspio\CaspioTables.zip  h:\CaspioTablesDaily\CaspioTables%dt%%tm:~0,4%.zip

"%ZipExe%" a "h:\AccessTablesDaily\Skilled_Nursing_Tables%dt%%tm:~0,4%.zip" "h:\AccessTablesDaily\Skilled_Nursing_Tables%dt%%tm:~0,4%.mdb" -tzip

Open in new window


»bp
Hi Bill,

I followed instructions (see attachment), however code as is does not work.
tried changing to the name of the exe, didnt work either.

when I try like this
"c:\ftp\scripts\7z1805-x64.exe" a "h:\AccessTablesDaily\Skilled_Nursing_Tables%dt%%tm:~0,4%.zip" "h:\AccessTablesDaily\Skilled_Nursing_Tables%dt%%tm:~0,4%.mdb" -tzip

Open in new window

an installation message pops up

what am I missing?

Thanks,
Ben
Untitled.png
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
Hi bfuchs,

Because you are using 7Zip Installation package. As mentioned earlier, please use Standalone package from: https://www.7-zip.org/a/7z1805-extra.7z

You will need 7zip installed on your computer to extract the standalone executable, but once you have it you can uninstall 7zip and use the executable for your batch file. The exe will be named 7z.exe when you extract the standalone package.

Regards,
Chinmay.
@Chinmay,
since I installed already the other one, what should be next step, uninstall it and take yours? or there is a way to get that working?
Thanks,
Ben
From those 3 exe listed in attachment, which one should I use?

Thanks,
Ben
Untitled.png
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
Which one you have already installed. I think Bill suggested 7zip only. and I am not suggesting any change in his batch file all I am suggesting is that you use the portable version which does not need installation.
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
7z.exe 45KB one :) Looks like we are posting simultaneously.
you meant 451KB, right?

Thanks,
Ben
well, I tried the following
"C:\Program Files\7-Zip\7z.exe" a "h:\AccessTablesDaily

\Skilled_Nursing_Tables%dt%%tm:~0,4%.zip" "h:\AccessTablesDaily

\Skilled_Nursing_Tables%dt%%tm:~0,4%.mdb" -tzip

Open in new window

It creates an empty zip file...1KB-:(

looks like something got messed up...
what is the first parameter and what is the 2nd?

Thanks,
Ben
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
Are you trying these command lines individually? or you are trying the Batch file Bill posted?

The command line that I use for such operation is

7z a -tzip FILETOBECREATED.zip FILESTOBEZIPPED

Open in new window


Regards,
Chinmay.
I use this in the batch.

Does it makes a difference where -tzip is located?
I see you use it before the file names while he placed after.

Thanks,
Ben
Trying this below.
"C:\Program Files\7-Zip\7z.exe" a -tzip "h:\AccessTablesDaily

\Skilled_Nursing_Tables%dt%%tm:~0,4%.zip" "F:\Data

\SkilledNursingVisitNotestbl.mdb"

Open in new window

Still same thing happening, empty file being created.

didn't know its that complicated...

Thanks,
Ben
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
I am not sure why he did that, I just use this command and it works for me. You can give it a try, maybe that's the reason it is not working for you.
wondering is it possible that it will not work if file is in use?

Thanks,
Ben
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
Hi Ben,

Please remove @echo off from your batch file. And see if you are getting any errors.

I just ran my batch without the timestamps and it worked.

Regards,
Chinmay.
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
Here is my batch file

setlocal

set ZipExe="D:\Softwares\Portable\Archivers\7z\7z.exe"

set tm=%time: =0%
set tm=%tm::=%
set dt=%date:~10,4%%date:~4,2%%date:~7,2%

"%ZipExe%" a -tzip "D:\Softwares\Portable\Archivers\7z\EE\readme%dt%%tm:~0,4%.zip"  "D:\Softwares\Portable\Archivers\7z\EE\readme.txt" 

Open in new window


Regards,
Chinmay.
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
The placement of -tzip is not a problem, it can go any place on the command line.

I'm a bit lost on what you are trying there and what is happening...


»bp
Good news, its working!!
I was right with last comment.
The file we tried copying was in use.
Just tried that on a copy of that file and it worked.
Now in order to finalize...
Since this file will always be in use.
how can I overcome this?

Thanks,
Ben
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
Try to just copy the file (some files are locked for access but can still be copied) using your batch file first, if it does not work then I am not sure what creative idea we will have to apply. Any idea which process is locking this file? IIS? or something else?
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
Honestly, copying an Access database while it's open and in use is risky, at best.  You really want to close Access before copying it, or the copy may actually be corrupted, or in a bad state that isn't very useful.  I wouldn't recommend copying something like an MDB while it's in use...


»bp
Any idea which process is locking this file? IIS? or something else?
This is an MS Access database, and users tend to leave the application open...

Thanks,
Ben
You really want to close Access before copying it, or the copy may actually be corrupted, or in a bad state that isn't very useful.  I wouldn't recommend copying something like an MDB while it's in use...
Well in that case I will never end up having a backup...as closing an open file from servers file manager is less secure...

In my experience, it should not cause any corruption to the source file, worse case the backup file will not be the best..so I will go for that...

What is the syntax.
copy original new
7z a -tzip FILETOBECREATED.zip New
kill new

Open in new window


is that correct?

Thanks,
Ben
Chief Technology Ninja
Distinguished Expert 2018
Commented:
:D... I liked Kill

it is

Copy "File" "Destination Path"
zipppp....
Del "Destination Path"+"File"

Open in new window


Regards,
Chinmay.
Thank you my experts!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial