JSI Tip 10000. How can a batch script open a document in its' associated application?

I have scripted RunFile.bat to open a document in its' associated application, or if there is no association, in Notepad.exe.

The syntax for using RunFile.bat is:

call RunFile FileName \[/WAIT\]


FileName is the file that you wish to open in its' associated application,
         like File.doc opening in Microsoft Word, and File.pdf opening in Acrobat Reader.
         If no association exists, the file is opened in Notepad.exe.

/WAIT    is an optional parameter that causes RunFile.bat to wait until you close the application before returning.
RunFile.bat contains:
@echo off
if \{%1\}

\{\} @echo Syntax: RunFile FileName \[/WAIT\]&goto :EOF if not exist %1 @echo Syntax: RunFile - %1 NOT found.&goto :EOF if \{%2\}

\{\} goto no2 if /i \{%2\}==\{/WAIT\} goto no2 @echo Syntax: RunFile %1 %2 - %2 NOT /WAIT. goto :EOF :no2 setlocal set type=N call :typeOK %1>nul 2>&1 if "%type%" EQU "N" goto BAD start "" %2 %1 endlocal goto :EOF :BAD start "" %2 notepad.exe %1 endlocal goto :EOF :typeOK for /f "Tokens=*" %%x in ('assoc %~x1') do ( set type=Y )

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.