Friday 10 September 2010

FileMenu Tools - Running Unix tools from the right click menu

For a long time I have been after a way to "head" or "tail" a file simply by right clicking it and selecting an option from the drop down menu. I was also keen to find a way to copy a full path to the clipboard through the right click menu too.

After a bit of searching for the Copy Path functionality, I came across a great little utility called FileMenu Tools, developed by LopeSoft and best of all it's freeware!

I was able to use this to add "Copy Path" and "Copy Name" to the right click menu.
Then I noticed the "Custom Command" option, and started thinking about how I could use it for the head/tail functionality.

I have Cygwin installed, which gives you access to many unix programs compiled for Windows, including Head/Tail, but in the past I've had to open a command window, browse to the folder and then type out the required head/tail command, followed by piping (>) the output stream to a new file. I had to have an environment PATH variable set to C:\Cygwin\Bin folder to get this to work.

So all I needed was a way to use the Custom Command functionality of FileMenu tools, to add commands to Head and Tail the files.

I had a bit of an issue passing my usual string as arguments to the head/tail .exe's directly, as the pipe and subsequent output filename were being taken in as parameters and not used by the method. I realised that it is the Command prompt that handles the redirection of the output to a file, so I managed to get it working by invoking the command prompt to run the head/tail method instead of calling them directly.

The command I used was:
cmd /C C:\Cygwin\bin\head.exe -N inputfile.txt > outputfile.txt

Using cmd /C tells cmd.exe to fire the following command and then exit. If you want to leave the prompt open you can use /K.

Next to get this working with FileMenu Tools...
Open FileMenu Tools and select Add Command from the menu on the LHS.

Here are the settings you'll need:
Program Properties
For all unix commands,
Program: C:\WINDOWS\system32\cmd.exe

Arguments
FOR HEAD:

Arguments: /C "C:\Cygwin\bin\head.exe" -@How many lines?@ %FILENAME1% > %FILENAME1%.head.%FILEEXT1%

FOR TAIL:
/C "C:\Cygwin\bin\tail.exe" -@How many lines?@ %FILENAME1% > %FILENAME1%.tail.%FILEEXT1%

FOR MIDDLE:
/C "C:\Cygwin\bin\tail.exe -n +@Start at line:@ %FILENAME1% | head -n @How many lines?@ > %FILENAME1%.middle.%FILEEXT1%"

This asks the user the question "How many lines?" and replaces the statement with the response.
In these examples, the output file is appended with "head", or "tail", and then the same extension as the original file.

Once you get the hang of it you can extend the functionality to other methods, I have also implemented "Middle" to get a sample of lines from the middle of a file.

Settings

Head Command Properties


Tail Command Properties

Middle Command Properties