How To: Make a Batch file back up into a zip with version dates


PLEASE NOTE I AM NOT AN EXPERT. This works and is easy to do. Use at your own risk, ask any questions in the comments I will answer the best I can.

So you want to make a batch (.bat) file to back up your folder into a zip file and append today’s date on it for a version history?

We can do that.

First thing you will need to do is to download 7zip command line and put the 7za.exe wile into your windows folder. This will let us zip files from a .bat.

Once you have done that you will need to make your first back up of the folder.

@echo off
7za a “c:myfiles-backup.zip” “c:myfiles”
exit

This will simply take your file sin c:myfiles and zip them up to c:myfiles-backup.zip.

But you want to add a date on it? We can do that.

@echo off
FOR %%A IN (%Date%) DO (
FOR /F “tokens=1-3 delims=/-” %%B in (“%%~A”) DO (
SET mmddyyy=%%B.%%C.%%D
)
)
7za u “c:myfiles-%mmddyyy%.zip” -ur2y2 “C:myfiles”
exit

Want to update an archive? We can do that.

@echo off
7za u “c:myfiles-backup.zip” -ur2y2 “C:myfiles”
exit

Let’s say you want a simple bat file that does what I mentioned last time in backup a folder structure AND backs up to a .zip file WITH the appended date onit. Yup, we can do that.

@echo off
FOR %%A IN (%Date%) DO (
FOR /F “tokens=1-3 delims=/-” %%B in (“%%~A”) DO (
SET mmddyyy=%%B.%%C.%%D
)
)
xcopy “c:My Files” “d:My Files Backup” /D /E /C /R /I /K /Y
7za u “d:myfiles-%mmddyyy%.zip” -ur2y2 “d:My Files Backup”
exit

As you noticed this will create a redundant back up for you by taking your My Files folder and backing it up to your D: drive then taking that back up and putting it in a zip file (with date) for a second level back up. Keep in mind all drive letters can change and you can use UNC paths as well.

What’s next you say? Well, next time I plan to show you how to put it all together into a user menu.

Leave a comment

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