tags:

views:

56

answers:

4

On a cmd prompt or bat file, I issue the following:

start textpad myfile.txt  and it works fine.

If the program textpad does not exist on the computer, then an error sound and a popup occurs which the OK button must be pushed.

I desire to trap this error so that I could do something like

start textpad myfile.txt || start notepad myfile.txt

where the || implies that if the start of textpad is not successful, then the start of notepad should occur. HOWEVER, I still get the error sound and requirement of hitting OK. My intent is to avoid the sound and the requirement of any user intervention.

I have also tried the following bat approach below, to no avail.

start textpad 
if not %ERRORLEVEL% == 0 GOTO END
start notepad
:END

Any help would be great.

thanks

ted

A: 

I do not believe you will find a way to make that work. Perhaps look for the existence of textpad.exe on the machine? If you can predict what directory it would be loaded from, this should be pretty easy using IF EXIST.

Gary
A: 

There are some techniques to detect the presence of a specific tool, but this only works for command line tool, or with GUI applications also supporting command line options.

For these tricks, take a look at this page.

Frank Bollack
A: 

"/wait" parameter would do the trick for you..

START /wait NOTEPAD.EXE SOME.TXT
echo %ERRORLEVEL%
# This gives zero as output.

START /wait TEXTPAD.EXE SOME.TXT
echo %ERRORLEVEL%
# This gives non-zero output.
VJ
This doesn't work for me, the error message still pops up.
Frank Bollack
+4  A: 

You can use the following little snippet to find out whether the program you intend to launch exists:

for %%x in (textpad.exe) do set temp=%%~$PATH:x
if [%temp%]==[] echo Didn't exist.

But may I suggest that you simply use

start foo.txt

instead of forcing a specific editor onto the user? File type associations are there for a reason.

Joey
wow, how does %%~$PATH:x the trick works?
João
@jpm: Documented in `help for` at the very end. It's a bit confusing to understand. It searches a semi-colon-separated list of paths given by an environment variable (`PATH` in this case) for the filename in `%%x` and returns the full path in case such a file is found.
Joey
Nice one Johannes.
aphoria