I sometimes create archives of folders I am working on when I am not sure if I’m going to break the folder.
I use 7Zip. It provides quite a few options for creating archives from folders via the Explorer Context Menu.
As this can get messy when you do this multiple times sometimes it would be nice if you could just use the context menu that creates an archive with the current timestamp.
That’s what I did! And you can get it now, too!
- 7Zip Installation (if you use the 32 Bit version you might need to change some stuff in the script)
- Probably admin rights for registry changes
- Windows 10
- 7Zip 64Bit
- PowerShell 5.Something
Your results may vary.
You are in the folder c:\temp and right-click the folder foo.
You left-click “TimestampArchive7z”.
A file named archive_foo_2016-09-28_104844.zip will appear in c:\temp.
- run regedit
- 1) navigate to key HKEY_CLASSES_ROOT\Directory\shell
- 2) right-click and add new key called TimeStampArchive7z or whatever you want to see in the context menu.
- 3) right-click the new key and add sub-key called command
- 4) Open the new sub-key, in the right lane double-click the (Default) entry
- 5) Paste the following one liner:
powershell.exe -NonInteractive -command "$v = '%v';$a = $v.split('\');$l = $a.length-1;$na = $a[0..($l-1)];$od = $na -join '\';$dn =$a[$l];$ts = $(get-date -f yyyy-MM-dd_hhmmss);$fn = -join('archive_', $dn , '_', $ts, '.zip');$alias = -join($env:ProgramFiles, '\', '7-Zip\7z.exe');set-alias sz $alias;$cp = -join($od, '\', $fn);sz a $cp $v"
Commented PowerShell code
The final line of the gist above (of course without the leading hashtag is also contained in the registry file in the automated installation below.
(this gist might be outdated at some point in time so preferably check https://github.com/timosperisen/TimestampArchive7z/blob/master/TimestampArchive7z.ps1 )
Get the archive ( clone or download)
Run the Install_TimestampArchive7z.reg to install the context menu entry.
Run the Uninstall_TimestampArchive7z.reg to remove it.
End of the tunnel
- Why short variable names?
Because at the end the script is a big one liner separated by semicolons.
- Why did you concatenate variables with -join?
Because at the end the script is run with
powershell -NonInteractive -command “[HUGE_ONELINER]” and escaping quotation marks there led to some problems.
- The context menu appears but when I click it no zip archive appears
If you haven’t installed 7Zip in the default location it might not work properly.
You can change the -NonInteractive parameter in the call to -NoExit in the install reg file. Then you need to rerun it and check what kind of error you see in the PowerShell window.
Ideas for extensions ( if you have nothing else to do)
- Change the script to create a folder with the same name as the input folder somewhere on a different drive (or share ) and then always write the archive there.
To avoid changes in the script use $env variables which you can easily change if you use a different location.
- Use additional 7z arguments for compression rate, 7z files instead of normal zips, etc.