How Can I Trap an error?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
themathprof
Posts: 5
Joined: 20 Feb 2011 12:39

How Can I Trap an error?

#1 Post by themathprof » 26 Aug 2012 06:48

This code is to locate a usb drive that has the program math911.exe. It works fine and locates the usb at drive I: However, just before the echo statement it shows an error message in a pop up window that says:
There is no disk in the drive. Please insert a disk into drive \Device\Harddisk1\DR1

When I remove F and J the popup does not appear because those are the drives
that I do not have. The popup only shows one time. I must press cancel to continue with the batch file.

How can I get the batch file to ignore the error and continue without the pop up?

@Echo Off
Setlocal
For %%a in (D E F G H I J) do (
IF Exist "%%a:\math911.exe" set "drive=%%a:"
)
echo The USB drive is: %drive%
set Pathname=%drive%
cd /d %Pathname%
dir
pause
md auto6
cd auto6
md con\

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: How Can I Trap an error?

#2 Post by Ed Dyreen » 26 Aug 2012 07:31

'
If you are running XP 32x or below you may be in luck ( This link will has expired ).

Code: Select all

> drvgtact.BAT drvgtact.EXE &call drvgtact.BAT &set drive
Otherwise you may need to enumerate 'mountvol /?', search dosTips, plenty of topics around.

themathprof
Posts: 5
Joined: 20 Feb 2011 12:39

Re: How Can I Trap an error?

#3 Post by themathprof » 26 Aug 2012 09:10

Thanks for your quick reply.

I'll be using various OS not just xp.

I really do not understand your tip.

Are you saying that there is no code (switch?) that I can add to that
will ignore the error and continue executing without the popup message?

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: How Can I Trap an error?

#4 Post by Squashman » 26 Aug 2012 11:21

Is the batch file being started from the same USB drive that has your executable you are checking to see if it exists?

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: How Can I Trap an error?

#5 Post by Ed Dyreen » 26 Aug 2012 16:23

themathprof wrote:I'll be using various OS not just xp.
The 'drvgtact.EXE' program I posted previously is obsolete from XP and up but if the program is started from usb just use

Code: Select all

echo.%~d0
Otherwise it will be more tricky, you could use This link will has expired which was originally designed for floppy drives.

Code: Select all

@echo off &setlocal enabledelayedexpansion

echo.Currently mounted drives:
set "$=" &for /f "skip=16 tokens=1 delims=\ " %%? in (

       'mountVol.EXE'

) do   for %%? in ( %%~? ) do set "$=!$!,%%~?"

set "$"
echo.

Echo.These drives are ready and:
for %%? in (

       !$!

) do   dready.COM %%~? &&if exist "%%~?\*.EXE" (

       echo.exists "%%~?\*.EXE"
) else echo.not exists "%%~?\*.EXE"

pause
exit

Code: Select all

Currently mounted drives:
$=,F:,G:,C:,D:,E:,X:,H:,I:,J:,A:,M:,N:,O:,P:

These drives are ready and:
exists "F:\*.EXE"
exists "G:\*.EXE"
not exists "C:\*.EXE"
not exists "D:\*.EXE"
not exists "E:\*.EXE"
not exists "X:\*.EXE"
Druk op een toets om door te gaan. . .
It works equally well on USB drives but not on a 64bit OS.
themathprof wrote:Are you saying that there is no code (switch?) that I can add to that
will ignore the error and continue executing without the popup message?
I see, you mean the famous On Error Resume Next :) no !

But what if all the above fails ?

Code: Select all

;Download autoIt compiler, change $title and $text to match the messageBox,
;Compile the script and if it doesn't works, post your question at the autoIT forums.

$title = 'Title:Windows - Geen schijf;Class:#32770'
$text  = 'Exception Processing Message c0000013'
If WinWait( $title, $text ) Then
       ;
       $time = 0
       While WinExists( $title, $text )
              ;
              If Sleep( $time )                          _
              And WinActivate( $title, $text )           _
              And ControlFocus( $title, $text, Button3 ) _
              And ControlClick( $title, $text, Button3 ) _
              Then WinWaitClose( $title, $text, 1 )
              $time += 1
              ;
       WEnd
       ;
EndIf
I wouldn't venture into vbScript's sendKeys commands though, it isn't safe.

Regards,

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: How Can I Trap an error?

#6 Post by Squashman » 26 Aug 2012 16:57

Came up with this but was also thinking of checking the errorlevel of the pushd command as well. I don't know what that will do with your sdcard drive letters. I have an SDcard reader on my Windows 7 laptop but I don't see the drive letters in My computer until something is put in them so I assume this is a windows xp issue.




Code: Select all

@Echo Off
Setlocal
For %%a in (D E F G H I J) do (
dir %%a:\math911.exe > nul 2>&1 &&set "drive=%%a:"
)
echo The USB drive is: %drive%
set Pathname=%drive%
cd /d %Pathname%
dir
pause
md auto6
cd auto6
md con\

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: How Can I Trap an error?

#7 Post by Ed Dyreen » 26 Aug 2012 17:02

'
Why didn't I come up with that :roll:

Code: Select all

dir a:

Code: Select all

Microsoft Windows XP [versie 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

>dir a:
Het apparaat is niet klaar.

>
Ah now I remember my solution works for windowsME command.COM
But even there the dir should throw an error, I don't remember :(

Or would it have asked me PLEASE INSERT DISK:
ABORT, RETRY, FAIL ?

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: How Can I Trap an error?

#8 Post by Squashman » 26 Aug 2012 17:24

Well I fired up my XP VM which just has A: C: and D:(dvd drive) and just did a little command line testing.

Code: Select all

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\XPMUser>dir D:\math911.exe > nul 2>&1 &&echo it exists


C:\Documents and Settings\XPMUser>dir a:\math911.exe > nul 2>&1 &&echo it exists


C:\Documents and Settings\XPMUser>dir a:\math911.exe
The device is not ready.

C:\Documents and Settings\XPMUser>

I didn't get any pop ups with this code but I don't know about sdcard readers.

themathprof
Posts: 5
Joined: 20 Feb 2011 12:39

Re: How Can I Trap an error?

#9 Post by themathprof » 26 Aug 2012 18:42

Squashman,

Your code works!

Brilliant.

Thank you much.

Themathprof.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: How Can I Trap an error?

#10 Post by Squashman » 26 Aug 2012 19:17

themathprof wrote:Squashman,

Your code works!

Brilliant.

Thank you much.

Themathprof.

Your Welcome.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: How Can I Trap an error?

#11 Post by dbenham » 26 Aug 2012 20:00

Here is a completely different method to find the drive letter that has the target file in the root directory. No need to provide a list of potential drive letters. I added an extra column so I don't have to worry about the line ending carriage return that FOR /F gets when used with WMIC.

Code: Select all

@echo off
setlocal
for /f "usebackq skip=1" %%A in (
  `wmic datafile where "path='\\' and filename='math911' and extension='exe'" get drive^, extension`
) do (
  set drive=%%A
  goto :break
)
:break
echo The USB drive is: %drive%
cd /d %drive%\
dir
pause
md auto6
cd auto6
md con\


Dave Benham

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: How Can I Trap an error?

#12 Post by Squashman » 26 Aug 2012 20:05

Dave, that is a thing of beauty!

Post Reply