Friday, June 29, 2007

Cygwin bash script: open windows explorer anywhere

Here is my take on this job. It is a small script to open Windows Explorer from any path I give it, or the current directory if I don't specify a path.

Updates.
7/07/2009 1:09:35 PM. Updated handling of paths according to Igor Mikushkin's suggestion in the comments - thanks Igor!

Purpose. Open Windows Explorer to any path from a Cygwin bash shell.

Input. Alternative path if current path is not to be used.

Output. Windows Explorer, opened to a given path.

Code.

#!/bin/bash
# I have called this script "windows" in my bin dir.
usage ()
{
 echo "Open Windows Explorer"
 echo "Usage: $0 [-help] [path]"
 echo "          [path]: folder at which to open Windows Explorer, will"
 echo "              default to current dir if not supplied."
 echo "          [-help] Display help (this message)."
}


location=.
case "$1" in
 ""                 ) location=.;;
 "-help"            ) usage; exit 0;;
 *                  ) location="${1}";;
esac

if [ -e "$location" ]
then
   WIN_PATH=`cygpath -w -a "${location}"`
   cmd /C start "" "$WIN_PATH"
else
 echo ${location} does not exist!
 exit 2
fi

One further advantage to this script is when I use it in conjunction with my script to search/list/open files, which I have named "u" in my bin directory. Using these two, I can open a Word doc, for example, with code :

u -e windows someWordDoc.doc

It will search for the file, so I don't need its path. It will then get the Windows OS to handle opening the file.

I could also set up an alias to word directly:

alias word="/cygdrive/b/Program\ Files/Microsoft\ Office/Office/winword.exe"

In this case I could use the following to open the word doc:

word /path/to/someWordDoc.doc

The problem I found with this is that I kept getting an "install" action occurring each time I used word this way - plus, I need to know the path each time. Thus, I found it less painful to use my earlier script. :)

8 comments:

Brian T. Grant said...

Very nice! Just yesterday I was looking to see if cygwin had an equivalent to Mac OS X's open command.

Thanks for the post!

RobertMarkBram said...

I am glad the tip was of use to you Brian - thanks! :)

Tristan said...

Thanks a lot Rob! I have been wanting this functionality for a while but not been able to justify the time on it. Sweeeeeeeet

RobertMarkBram said...

Thanks Tristan - I am very happy it helped you. :)

Tabun said...

Yes thank you!
Such a time saver and feature that I have wanted for so long. Previous googling got me no where and have given up, thanks again!

Igor Mikushkin said...

I suppose it's better to use native windows' "start" command. It traits some types in a different way. E.g. explorer opens *.h files in IE window but "start" opens them in associated application.

Example:

#!/bin/bash
if [ "$1" = "" ]; then
NATIVE_PATH="."
else
NATIVE_PATH="$1"
fi

WIN_PATH=`cygpath -w -a "${NATIVE_PATH}"`
cmd /C start "" "$WIN_PATH"

RobertMarkBram said...

Thanks Igor - I will give that a try!

pseutro said...

There's a very good implementation of the "open" command for cygwin.

http://www.davehylands.com/Software/Open/

It will open any file using the default handler for the filetype or any directory using Explorer.