Often, when I work on a project that requires a bunch of files to produce an output, be it a set of diagrams or documents or even sometimes code, I produce an output and then I tend to try a different idea by making changes to a bunch of files to see how something looks and then go back to how it was previously and try something else by changing the same or different set of files. You might do things the same way.
It quickly gets super tedious to make changes to the files to bring back something I tried a while ago or to restore the work to a particular state that I liked, because you need to keep track of all the changes you made and do them again or undo them all-mostly manually. In such times, I tend to take back up of the whole directory or sub-directory with all its files and sub-directories. I used WinRAR and a quick key combination to add the current directory and create an archive with a timestamp based filename, I keep this archive aside with a label to identify if I need to restore later, sometimes I don’t even bother giving a label if there is something that I want to try quickly. Later, I restore and replace the files to bring back the work to a state that I want. I do this even if I have source control or online sync’ing enabled with Dropbox or something. Of course, I delete all the unnecessary archives later.
You can also use source control and apply label for the set of changes, but if there is a bunch of revisions over a short timespan then it’s better to not pollute the source control’s version history.
I faced an issue sometime ago, I couldn’t use WinRAR due to licensing issues in the machine that I was working, I couldn’t find an alternative program that could do what WinRAR did. So I made a tiny tool, obviously it required a minuscule amount of effort, but still, the idea of automating or creating something to help make your life a bit easier or save a bit of your time is always exciting.
I decided PowerShell would be good to handle a bunch of files and though I haven’t written anything that deals with creating archives, I guessed there should be a simple cmdlet to do it. And, yes I found something- Compress-Archive
cmdlet can help you create archives.
Compress-Archive -Path "$(Get-Location)\*" -DestinationPath "bkp-$(get-date -f yyyyMMMdd-hh-mm-sstt).zip" -CompressionLevel Optimal -ErrorAction SilentlyContinue
I made a PowerShell script with the above one line code. The command creates an archive of the folder that the script resides in. I am providing the current location to Path
, for DestinationPath
-I am just passing the filename by creating a string using current date & time, this creates the archive in the current folder. I am specifying the compression level to be optimal.
Lastly, I set the ErrorAction
as SilentlyContinue
as there is a chance that there may be some files being locked by some processes, which ends up throwing error and aborting the archive creation, this is more likely in my case as I am archiving the files I am currently working on. Setting the ErrorAction
will ignore the errors and continue creating the archive.
You can find the code here as well.