Jump to content

Not understating errorlevels received from apps


Recommended Posts

I am trying to whip up a small program to help in safley ejecting my usb drive from xp via PStart.exe. I have it working from a dos batch file.

In a nut shell it works like below:

run C:\sync.exe -r -e %usbdrive%

if sync.exe gives an exit code of 0 then run c:\del_eject.cmd and delete the reg entry below in my current non-working code then display message stating its ok to unplug the drive. if an exit code of 1 is received then display a message stating that it could not safley eject the drive.

Thanks for any help.

TraySetState(2)
Run("C:\del_eject.cmd", @SW_HIDE)

Func eject()
    $val = RunWait(@ComSpec & ' /c "C:\sync.exe -r -e %usbdrive%"', '', @SW_HIDE)
        If $val = 0 Then
            FileDelete "C:\sync.exe"
            end()

ElseIf $val = 1 Then
    warning()
    FileDelete "C:\sync.exe"
EndIf
EndFunc

Func warning()
    MsgBox(0, '', 'There was a problem ejecting the USB Storage Device. Not all files/handles may be closed. You may need to close them manualy before you are able to safley eject the USB storage device"')
EndFunc

Func end()
RegDelete('HKCU\Environment', 'usbdrive')
        MsgBox(0, '', 'You may now safely unplug the USB Storage Device!')
EndFunc
Link to comment
Share on other sites

;You need RunErrorsFatal to get error msg back from RunWait
Opt("RunErrorsFatal",0)
; Eject usb device
TraySetState(2)
; What does this do? Probably use RunWait to make shure del_eject.cmd has finished?
Run("C:\del_eject.cmd", @SW_HIDE)
; You must add calls to your functions
eject()
exit 

Func eject()
   $val = RunWait(@ComSpec & ' /c "C:\sync.exe -r -e %usbdrive%"', '', @SW_HIDE)
   ; Test @error ???
  If $val = 0 Then
      FileDelete "C:\sync.exe"
      end()
   ElseIf $val = 1 Then
      warning()
      FileDelete "C:\sync.exe"
   EndIf
EndFunc

Func warning()
    MsgBox(0, '', 'There was a problem ejecting the USB Storage Device. Not all files/handles may be closed. You may need to close them manualy before you are able to safley eject the USB storage device"')
EndFunc

Func end()
RegDelete('HKCU\Environment', 'usbdrive')
        MsgBox(0, '', 'You may now safely unplug the USB Storage Device!')
EndFunc

Link to comment
Share on other sites

Good deal. Thanks a ton. But where would be the best place to get a better understanding on how this function/statement works in AutoIt. I read the help and see the function in it but it doesn't explain much about it.

Thanks again.

MrChris

Link to comment
Share on other sites

Good deal. Thanks a ton. But where would be the best place to get a better understanding on how this function/statement works in AutoIt. I read the help and see the function in it but it doesn't explain much about it.

Thanks again.

MrChris

Try the Search feature and enter the function, I'm sure it will pull up some topic related to the function.

Nomad :D

Link to comment
Share on other sites

OK I am using the below code and what it is doing is it looks like the dos windows that runs c:\sync.exe is popping up but its popping up and closing so fast I cant see what it doing then its giving a message that it is unsafe to unplug the drive and its not ejecting the drive but it is deleting the files I want it to.

Opt("RunErrorsFatal",0)
TraySetState(2)
Runwait("del_eject.cmd","c:\", @SW_HIDE)
eject()
exit

Func eject()
   $val = RunWait ('"sync.exe -r %usbdrive%"', "c:\", @SW_MAXIMIZE)
   If $val = 0 Then
       FileDelete ("C:\sync.exe")
       end()
   ElseIf $val = 1 Then
       warning()
        FileDelete ("C:\sync.exe")
        EndIf
EndFunc

Func warning()
    MsgBox(0, '', 'There was a problem ejecting the USB Storage Device. Not all files/handles may be closed. You may need to close them manualy before you are able to safley eject the USB storage device')
    EndFunc

Func end()
    RegDelete('HKCU\Environment', 'usbdrive')
    MsgBox(0, '', 'You may now safely unplug the USB Storage Device!')
    EndFunc
Edited by MrChris
Link to comment
Share on other sites

I don't know what sync.exe is supposed to do or how it communicates with the world. But if it is a command line application you could possibly get the messages it prints out with the StdoutRead function.

Modified code from the beta help file.

; Demonstrates StdoutRead()
#include <Constants.au3>

$foo = Run('"sync.exe -r %usbdrive%"', "c:\", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)

While 1
    $line = StdoutRead($foo)
    If @error = -1 Then ExitLoop
    ConsoleWrite( "STDOUT read: " & $line & @LF)
Wend

While 1
    $line = StderrRead($foo)
    If @error = -1 Then ExitLoop
    ConsoleWrite( "STDERR read: "& $line & @LF)
Wend

ConsoleWrite( "Exiting...")

You should be able to extract what you want and make decisions based on it.

To find out more about functions I frequently (several times a day) use the search link. One tip to get results faster is to select the "search titles only" option:)

Edited by Uten
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...