Open a File in the Default Application using the Windows Command Line (without JDIC)

Quite a few people have asked me about this in the past. If you have a file how can you open it in the default associated application without querying the registry or using some other Windows API? Or if you program in Java how can you do it without using JDIC?

The easiest way to do this is using the "start" command. For example to open the file "readme.txt" in the default text editor you would do this:

C:\>start readme.txt

You can also use start to open folders or follow shortcuts:

C:\>start "My Shortcut"    <-- note that you don't need .lnk at the end

This will open the target of the "My Shortcut" shortcut. If the shortcut points to a folder it will open a Windows Explorer window for it, if the shortcut points to a document it will open it in the default application and if the shortcut is for a program it will launch the program.

The trick is that "start" isn't an executable. It is a built-in command of the Windows command line interpreter "cmd.exe". In Java (and other languages) if you try to create a process using the "start" command this will fail -- since there is no "start.exe" executable in the system.

Instread you have to invoke "start" through the "cmd.exe" interpreter. This can be done using the /C flag:

cmd /c "start readme.txt"

This can be run successfully in Java using Runtime.exec() or a ProcessBuilder. Simply calling "start" directly would fail. Note that this limitation is also true for many other Windows commands. If something fails to invoke you should always try running it using "cmd /c".

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Same thing on the Mac

In fact, we had to do something very similar to this to get around a problem w/ JDIC on the Mac.

Instead of using JDIC to open content using native viewers, you can accomplish the same thing by calling 'open'.

ie) open readme.txt or open directory_name

Thanks for this snippet,

Thanks for this snippet, however I have found a problem with it in my application. If the path name has a space in it this call fails to work. e.g. the path C:\Documents and Settings\craig laptop\My Documents\COMS DETAILS.xls displayed an error message that the file C:\Documents did not exist. I tried putting quotes around it but it made no difference. Any ideas?

regards
Craig

Try this code

I found that only converting the file to "short name" works reliably on windows:

## Start arbitrary URL in external program (Windows only)
proc Start {url} {
  ## try file short name (that is the only way to deal with spaces that works)
  if {[catch {file attributes $url -shortname} c]} {set c $url}
  catch { exec cmd /C start $c & }
}

Any other methods, like multiple quotes, don't seem to work.

escaping white spaces

all you have to do is escaping white spaces, where c:\My Bag Of Files would become c:\My\ Bag\ Of\ Files.
I think this would works fine for you.

use multiple quotes?

Sorry, I don't know off the top of my head and I can't try it out right now. I do know that sometimes you have to use many quotes to get cmd to correctly interpret them. Have you tried something like this:

cmd /c "start """c:\My Documents\Some File.txt""""

If I recall correctly you have to use double or triple quotes or something for cmd to correctly handle quotes inside of quotes.

Good luck!

it's cmd /c "start c:\My"

it's cmd /c "start c:\My" "Documents\Some" "File.txt", it's working for me now!

thanks a lot!