SgDotNet
Singapore Professional .NET User Group -For Cool Developers

tar bzip2 with SendTo

This is a continuity of the SendTo topic. WinZip and WinRar have pretty convenient explorer context menu such as "Add to archive...".

I regularly use tar and bzip2 from the Cygwin on the Windows box and I think putting them into a SendTo would be handy sometimes.

I create a shortcut item in the SendTo folder, name it as "tar bzip2 (Create Archive)" and in the command target field enters the follows:

%windir%\System32\cmd.exe /v:on /k set /p TARBIZ2FILE=Enter the file name: && echo !TARBIZ2FILE! && tarzip.bat

This instructs the cmd.exe to carry out the command specified by string followed /k switch and remains open. /v:on is used to turn on delayed environment variable expansion using ! as the delimiter instead of the usual @.

We will get the file name of the to-be-created archive from the user input and store it in an environment variable, whose later use therefore must be expanded in a delayed fashion, or substituted with the value of an environment variable at execution time.

"set /p TARBIZ2FILE=Enter the file name:" is used to prompt the user to enter an archive name (without .tar.bz2 extension) and the file name is assigned to EV TARBIZ2FILE.

&& is used to run the second command only if the command preceding the symbol is successful, though & should be good enough, which just separates multiple commands on one command line.

Afterwards, in a similar fashion to the previous post, tarzip.bat is called to process the file or the directory name, which is passed in as %1.

@echo off
// change drive
%~d1
// change to parent folder
cd %~dp1
// process the folder or the file and create the archive in the
// parent folder, %~nx1 expands to the portion after the last tar cf %TARBIZ2FILE%.tar.bz2 --use-compress-program=bzip2 %~nx1

A new .tar.bz2 file would be created in the parent folder.

Posted Nov 23 2004, 01:00 AM by blackinkbottle
Filed under:
Copyright SgDotNet 2004-2008
Powered by Community Server (Commercial Edition), by Telligent Systems