Simple backups with windows 1
by Jens on Nov.26, 2009, under Computer, Programmierung
Ich werde mal versuchen einige meiner Einträge auf Englisch zu verfassen – um in der Übung zu bleiben.
At my current workplace, working with Linux (or anything other than windows) seems to be something no one would even bother to think about. So I had to dig into the world of Windows Batch Scripting in order to create those little tools that make my life just a bit easier when using linux.
The first thing I had to do was creating a script that simply backups folders. I have a folder which contains other folders that should be archived – I usually zip (or 7zip) them and move them to the backup space later, which is a fairly easy task using a bash-script and nautilus actions.
Under windows it’s not that much harder to accomplish. (In the following example I used WinRAR to compress the files because that’s what we use at work.)
The first thing I needed was of course the batch-file that does the actual work. Here it is:
@echo off rem ===================== Begin Configuration ===================== rem !!! Do not use quotes for the values of the following variables !!! rem Path to the WinRAR executable file set winrarPath=%PROGRAMFILES%\WinRAR\WinRAR.exe rem ====================== End Configuration ====================== rem Please only edit after this line if you know exactly what you are doing. set curVer=0.1 [2009-11-26] rem ======================== Begin Script ======================== rem Tell the user which version of the tool they use echo. echo Mi ArchiveSubfolders version %curVer% echo. echo. if not exist "%winrarPath%" ( echo Cannot find WinRAR exe file: %winrarPath% goto eof ) set dir=%* if not exist "%dir%" ( echo The directory "%dir%" cannot be found goto eof ) set dateString=%DATE:~-4,4%-%DATE:~-7,2%-%DATE:~0,2%_%TIME:~0,2%-%TIME:~3,2%-%TIME:~6,2% cd "%dir%" for /D %%a in (*) do ( rem Creating the archive echo Archiving "%%~na"... "%winrarPath%" M -afzip -ibck -inul -m5 "%dateString%-%%~na.zip" "%%a" ) echo. echo Archiving done :eof
Now that is a nice, simple script, but what should I do with it? I wanted it to be useable directly in the explorer context-menu, so I made a small registry-entry. This is the exported .reg-file:
Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\Folder\shell\archiveSubfolders] @="Mi: Archive Subfolders (ZIP)" [HKEY_CLASSES_ROOT\Folder\shell\archiveSubfolders\Command] @="cmd /c \"\"%%ProgramFiles%%\\MiTools\\archiveSubfolders\\archiveSubfolders.bat\"\" %1"
Now I wanted a simple installation-script, for really lazy people like me:
@echo off set installDir=%PROGRAMFILES%\MiTools\archiveSubfolders if not exist "%installDir%" ( mkdir "%installDir%" ) copy archiveSubfolders.bat "%installDir%" copy "ArchiveSubfolders Context Menu Entry.reg" "%installDir%" regedit /S "ArchiveSubfolders Context Menu Entry.reg" echo Installation complete. :eof