It is one step of my backup script under Windows 2000. Of course it works under Windows XP.
First, I use 7z as my default compress tool. Not because of it is a free, Open Source software, but it supports more format and compress methods.
The command program is 7z.exe under the installation of 7z package.
You can download it from here.


I use it in my daily backup script. So, the file name is contain date information.

7z a %DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%.7z *.sql

The output file name looks like:

2009-05-26.7z

The original file is a MySQL database exporting file. So the name is *.sql.
a means add files into 7z file.
I don’t use method switch. The default is LZMA.
I give you an example. I have a database sql file about 184MB. The compressed file made by above command is 31MB. Very tiny.

David Yin

David is a blogger, geek, and web developer — founder of FreeInOutBoard.com. If you like his post, you can say thank you here

One Reply to “7z command to compress file with date as file name”

  1. Actually it’s something like %DATE:~-4%-%DATE:~3,2%-%DATE:~0,2%.7z
    Your “4,2” cuts the ‘0’ from the month and the “7,2” includes the ’01’ from the year for the day, instead of the actual day.
    But a good idea nonetheless, since I was looking for a 7zip switch that could put the date in the filename, forgetting I could do that directly in the filename formatting.

Leave a Reply

Your email address will not be published. Required fields are marked *