DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: Tiny batch file: zips current directory, date+time stamps, moves to parent  (Read 17,436 times)

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,931
    • Gathering Badge
    • Plugin Coding Medal
    • Participated in NANY Challenge Seventeen Times
    • Cody Award
    • Coding Snacks
    • Volunteers/Staff
Just thought I'd share this little code snippet for a batch (zipcurdir.bat) file that I use quite frequently.
Requires 7-zip but could be adapted for any other zip tool.
Then just put this little .bat file in any directories where you want to be able to super quickly make a uniquely timestamped zip file of the contents of the directory, and then move the zip file out of the way to the parent directory.
I use this all the time when I'm working on a project and it's not under version control and I want to make sure I have quick backups before something goes wrong.

Add any exclusion patterns you want to the extraoptions line, and change 7-zip location if different; or maybe you want to change the destination folder..

Code
for %%a in ("%cd%") do set "CurDir=%%~na"
set thedir=%CurDir%

set mydate=%date:~12,2%%date:~4,2%%date:~7,2%
set TIMEZERO=%TIME: =0%
set HOUR=%TIMEZERO:~0,2%
set MIN=%TIMEZERO:~3,2%
set mytime=%HOUR%%MIN%
set zformat=zip

set extraoptions=-xr!Temp -xr!Junk

set zcmd="c:\program files\7-zip\7z" a -t%zformat% -r %extraoptions%
set fname=%thedir%_%mydate%_%mytime%.%zformat%

echo %zcmd% ..\%fname% .\
%zcmd% ..\%fname% .\


As an example if I put this zipcurdir.bat file in my Documents\Arduino\ folder and run it, I get the file in the parent directory: "Arduino_220506_0615.zip"

The only thing neat about this batch file is the autonaming of the created file based on the current directory and the fact that you never have to modify the batch file no matter where you put it.

Nod5

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,175
    • Coding Snacks
    • Participated in NANY Challenge Three Times
    • Plugin Coding Medal
Handy! I have some similar "lazy version control" habits, for example to clone some single work in progress file with a timestamp suffix (via autohotkey and FARR, of course ;D). For when strict VC is too much but you still want to avoid the "file.ext", "file - copy.ext", "file -copy(2) final.ext" mess.

Contro

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,947
Seems very interesting !!!!!!!
:-* :P

erikts

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 227
Thanks for sharing. I use it today to backup original files of a project I'm working on.  :up:

anandcoral

  • Honorary Member
  • Joined in 2009
  • **
  • Posts: 802
    • Participated in NANY Challenge Thirteen Times
I just found this neat batch codes. :Thmbsup:

I use a similar batch file but I have to put the folder name manually in each one. Now I can get it auto. Thanks Mouser.

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,931
    • Gathering Badge
    • Plugin Coding Medal
    • Participated in NANY Challenge Seventeen Times
    • Cody Award
    • Coding Snacks
    • Volunteers/Staff

Original address: https://www.donationcoder.com/forum/index.php?topic=52321.0