Page 1 of 1
How Can I Trap an error?
Posted: 26 Aug 2012 06:48
by themathprof
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\
Re: How Can I Trap an error?
Posted: 26 Aug 2012 07:31
by Ed Dyreen
'
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.
Re: How Can I Trap an error?
Posted: 26 Aug 2012 09:10
by themathprof
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?
Re: How Can I Trap an error?
Posted: 26 Aug 2012 11:21
by Squashman
Is the batch file being started from the same USB drive that has your executable you are checking to see if it exists?
Re: How Can I Trap an error?
Posted: 26 Aug 2012 16:23
by Ed Dyreen
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
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,
Re: How Can I Trap an error?
Posted: 26 Aug 2012 16:57
by Squashman
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\
Re: How Can I Trap an error?
Posted: 26 Aug 2012 17:02
by Ed Dyreen
'
Why didn't I come up with that
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 ?
Re: How Can I Trap an error?
Posted: 26 Aug 2012 17:24
by Squashman
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.
Re: How Can I Trap an error?
Posted: 26 Aug 2012 18:42
by themathprof
Squashman,
Your code works!
Brilliant.
Thank you much.
Themathprof.
Re: How Can I Trap an error?
Posted: 26 Aug 2012 19:17
by Squashman
themathprof wrote:Squashman,
Your code works!
Brilliant.
Thank you much.
Themathprof.
Your Welcome.
Re: How Can I Trap an error?
Posted: 26 Aug 2012 20:00
by dbenham
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
Re: How Can I Trap an error?
Posted: 26 Aug 2012 20:05
by Squashman
Dave, that is a thing of beauty!