Jump to content

Batch Choice.exe replacement


Bidder
 Share

Recommended Posts

I needed a replacement for the built in command line choice.exe functionality for use in WinPE

Accepts timeout in seconds with a default selection

This is set to compile as x64

Sets the %errorlevel% according to the selection made.  

 

@echo off
choice64.exe "Choose from the following" YNC 15 N
echo %errorlevel%

 

#Region ;Compiler Directives created by AutoIt3Wrapper_GUI
#AutoIt3Wrapper_icon=S:\Data\AutoIt\Resources\Icons\AutoIt.ico
#AutoIt3Wrapper_Res_Comment=Autoit version
#AutoIt3Wrapper_Res_Description=Autoit version
#AutoIt3Wrapper_Res_Fileversion=1.0.0.0
#AutoIt3Wrapper_Res_LegalCopyright=Autoit ©2016
#AutoIt3Wrapper_Res_Language=1033
#AutoIt3Wrapper_Run_AU3Check=y
#AutoIt3Wrapper_UseX64=n
#AutoIt3Wrapper_Change2CUI=y ; Console application
#include <Date.au3>
#EndRegion

;Options
Opt("MustDeclareVars", 1) ;Must declare variables

Local $sText, $sChoices="YN", $sChoicesArray, $sTimeoutSecs=0, $DftChoice=1, $Rtn=0
Local $sStrTime, $sEndTime

;Parm check
if $CmdLine[0] < 1 Then
    ConsoleWrite("CHOICE Question Text [Choices] [Timeout in seconds] [Default choice]" & @CRLF)
    Exit
EndIf


; Set parms
$sText=$CmdLine[1]
if $CmdLine[0] >= 2 Then $sChoices=$CmdLine[2]
if $CmdLine[0] >= 3 then $sTimeoutSecs=$CmdLine[3]
if $CmdLine[0] >= 4 then $DftChoice=$CmdLine[4]

;Setup vars
$sChoicesArray = StringSplit($sChoices, '')

;Print question and wait for response
While True
    $sStrTime=_NowTime(5) ;Start timer
    ConsoleWrite($sText & "[" & $sChoices & "]:  ")
    $Rtn=ReadConsolInput()
    if $Rtn>0 then ExitLoop
WEnd

;Exit and set %errorlevel% for batch file
Exit($Rtn)


;Read console and optionally execute default
Func ReadConsolInput()

    While True
        Local $sResult=-1 ;error
        Local $sRead = ConsoleRead()
        If @error Then ExitLoop
        if $sRead<>"" then
            Local $Pos=StringInStr($sRead, @CRLF)
            if $Pos>0 then
                For $i = 0 to (UBound($sChoicesArray) - 1)
                    $sRead=StringReplace($sRead,@CRLF,"")
                    if $sRead=$sChoicesArray[$i] then
                        $sResult=$i
                        ExitLoop
                    EndIf
                Next
                ExitLoop
            EndIf
        EndIf

        if Number($sTimeoutSecs)>0 Then
            $sEndTime=_NowTime(5)
            Local $Secs=TimeDiff()
            if Number($Secs) >= Number($sTimeoutSecs) then
                For $i = 0 to (UBound($sChoicesArray) - 1)
                    if $DftChoice=$sChoicesArray[$i] then
                        ConsoleWrite(@CRLF)
                        $sResult=$i
                        ExitLoop
                    EndIf
                Next
                ExitLoop
            EndIf
        EndIf

        Sleep(200) ;milliseconds

    WEnd

    return $sResult
EndFunc



Func TimeDiff()
Local $sTime1 = $sStrTime, $sTime2 = $sEndTime
Global $iTime1,  $iTime2
Global $aTemp, $sHour, $sMinute, $sSecond
$aTemp = StringSplit($sTime1, ":")
$iTime1 = _TimeToTicks($aTemp[1], $aTemp[2], $aTemp[3])
$aTemp = StringSplit($sTime2, ":")
$iTime2 = _TimeToTicks($aTemp[1], $aTemp[2], $aTemp[3])
_TicksToTime($iTime2-$iTime1, $sHour, $sMinute, $sSecond)
Return $sSecond
EndFunc

 

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