Batch File to Compress File Using Windows

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
accesshawaii
Posts: 12
Joined: 01 Nov 2012 09:19

Batch File to Compress File Using Windows

#1 Post by accesshawaii » 01 Nov 2012 09:24

The computer that I am on, I cannot download Winzip or 7Z etc. I have to work with what I have. I have the regular Windows default right click on a file and select "Send to Compressed Zip Folder"

If I have a file called MyTest.XLS located in C:\MyFolder and I want it to zip to C:\MyZips, how would I do that? I have some knowledge of batch files but by no means an expert. Any assistance would be appreciated.

accesshawaii
Posts: 12
Joined: 01 Nov 2012 09:19

Re: Batch File to Compress File Using Windows

#2 Post by accesshawaii » 01 Nov 2012 10:16

Ok, I was able to get 7-Zip installed. Could someone help me with the syntax for zipping the file that I outlined in the first post using 7-zip? Thanks.

Boombox
Posts: 80
Joined: 18 Oct 2012 05:51

Re: Batch File to Compress File Using Windows

#3 Post by Boombox » 01 Nov 2012 11:08

.
7z <command> [<switches>...] <archive_name> [<file_names>...]

Code: Select all

7z a -t 7z -r "c:\backup.7z" "c:\MyStuff\*.*"

(Adds to -type 7zip archive -Recursively Save to "c:\backup.7z" All from "c:\Mystuff\*.*")

Remember to use " " quotes if the destination has spaces in it!

http://www.dotnetperls.com/7-zip-examples


Type 7zip /? at CMD for help (at a guess)

accesshawaii
Posts: 12
Joined: 01 Nov 2012 09:19

Re: Batch File to Compress File Using Windows

#4 Post by accesshawaii » 01 Nov 2012 11:28

Thanks for the reply but that did not work. I even made a folder called "backup" and one called "MyStuff", which I placed files in both and nothing happened. I'm assuming that backup is where the files would be placed and MyStuff is where the files are coming from. Below is the exact syntax that I used.

7z a -t 7z -r "C:\backup.7z" "c:\MyStuff\*.*"

Any suggestions?

Boombox
Posts: 80
Joined: 18 Oct 2012 05:51

Re: Batch File to Compress File Using Windows

#5 Post by Boombox » 01 Nov 2012 11:42

.
EDITED

-t7z instead of -t 7z

NO SPACE!

accesshawaii
Posts: 12
Joined: 01 Nov 2012 09:19

Re: Batch File to Compress File Using Windows

#6 Post by accesshawaii » 01 Nov 2012 12:02

I appreciate your patience with this but it's still not working. Below is what I'm using.

7z a -t7z -r "c:\backup.7z" "c:\MyStuff\*.*"

Boombox
Posts: 80
Joined: 18 Oct 2012 05:51

Re: Batch File to Compress File Using Windows

#7 Post by Boombox » 01 Nov 2012 12:14

.
I copied the code above, into CMD after creating the MyStuff folder on c:

Everything went ok

Where and when did you get your version?
What error message do you get?

Backup.7z is the archive name,
MyStuff\*.* is everything inside MyStuff folder including sub-directories

Make sure 't7z and Backup.7z are the same.

accesshawaii
Posts: 12
Joined: 01 Nov 2012 09:19

Re: Batch File to Compress File Using Windows

#8 Post by accesshawaii » 02 Nov 2012 04:36

When I paste the below in a dos command prompt

7z a -t7z -r "c:\backup.7z" "c:\MyStuff\*.*"

I get the error

'7z' is not recognized as an internal or external command, operable program, or batch file

I'm running 7-Zip 9.20. I just downloaded it yesterday.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch File to Compress File Using Windows

#9 Post by foxidrive » 02 Nov 2012 04:59

You need to include the path to the executable. Assuming it is located in "c:\Program Files\7-Zip\" then try this:

Code: Select all

"c:\Program Files\7-Zip\7z.exe"  a -t7z -r "c:\backup.7z" "c:\MyStuff\*.*"

Boombox
Posts: 80
Joined: 18 Oct 2012 05:51

Re: Batch File to Compress File Using Windows

#10 Post by Boombox » 02 Nov 2012 05:04

.
I can go straight to CMD, type 7z (on its own), press enter and get a response...

You many need to reinstall...

Can you find the 7zip folder?

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: Batch File to Compress File Using Windows

#11 Post by alan_b » 02 Nov 2012 05:33

Boombox wrote:.
You many need to reinstall...

NOT true if the Portable version of 7z was used.
Foxidrive's solution will always work so long as you correctly stipulate the path.

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: Batch File to Compress File Using Windows

#12 Post by miskox » 02 Nov 2012 19:30

Why don't you use a standalone zip.exe? You can just put it somewhere in your path.

Here http://www.info-zip.org/Zip.html you can get it. It works on XP, I don't have Win7 to test it. It probably works.

Saso

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch File to Compress File Using Windows

#13 Post by foxidrive » 02 Nov 2012 22:16

7-zip can handle more files in a ZIP than ZIP can.

7-zip also has the capability to make ZIP archives.

7-zip has superior compression compared to ZIP

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Batch File to Compress File Using Windows

#14 Post by Ed Dyreen » 03 Nov 2012 03:29

'
7-Zip 4.57 Console ( Igor Pavlov ) can unpack .ISO images 8)

accesshawaii
Posts: 12
Joined: 01 Nov 2012 09:19

Re: Batch File to Compress File Using Windows

#15 Post by accesshawaii » 05 Nov 2012 09:08

foxidrive,

What you posted worked!!!. Thanks. Thanks to everyone for the responses.

Ready for another brain teaser? What I'm actually trying to do is create a back-up of a file each month. The name of the zip will need to have the date that it was backed up and the abbreviation of the previous month. So, if I did a back-up today, it would look something like below.

20121105_Oct_Data.7z
I know I can get the date portion by doing something like

"c:\Program Files\7-Zip\7z.exe" a -t7z -r "c:\%date:~10,4%%date:~4,2%%date:~7,2%_MyBackup.7z" "c:\MyStuff\*.*"

My question is how could I get the actual name of the previous month, so it would display as the example I gave above? Any assistance would be appreciated.

Post Reply