开发者

Gracefully trap error on start cmd

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 avai开发者_如何学Gol.

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

Any help would be great.

thanks

ted


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.


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.


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.


"/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.


You probably already have an answer, but my over-sized ego has forced me to post my answer.

So, this should work.

start textpad 2> nul||start notepad

This will start notepad if the command start texpad fails, while also redirecting any error message you may get from the first command.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜