• Status: Solved
• Priority: Medium
• Security: Public
• Views: 425

# how to create a VB script or a bat file with waiting period

I need to create a bat file or a VB script which will ask a question with a big banner
Is this a Master Y/N
(warning Y=Do not do anything, N= start cloning process)
if user responds Yes it shouldn't do anything and just exit.
If user DOES NOT RESPOND for 2 minutes or respond N then it should run command  “c:\windows\system32\XYZ.exe"
0
wajhiuddin
• 11
• 8
• 5
• +2
3 Solutions

Commented:
You need to use choice command in a batch file, the syntax is :

CHOICE  [ /C[:]choices ]  [ /N ]  [ /S ]  [ /T[:]c,nn ] text

/C:choices      Specifies allowable keys.
Default for English versions is YN
/N   Do not display choices an ? at end of prompt string.
/S or /CS   Treat choice keys as case sensitive.
Up to (and including) the Resource Kit versions, use /S.
In Windows 7 use /CS.
/T:c,nn   Default choice to c after nn seconds.
text   Prompt string to display.

e.g.:

CHOICE /C:AB /N /T:N,10 Format drive A:, B:, or None?
IF ERRORLEVEL 1 SET DRIVE=drive A:
IF ERRORLEVEL 2 SET DRIVE=drive B:
IF ERRORLEVEL 5 SET DRIVE=None
ECHO You chose to format %DRIVE%

-Rahul
0

Commented:
Here's a shot at a BAT approach to give you the idea. The formatting and messaging could be adjusted, but just to give you the basic idea.

@echo off

echo **********************************************************************************
echo **********************************************************************************
echo **********************************************************************************
echo *****                                                                        *****
echo *****                         I M P O R T A N T                              *****
echo *****                                                                        *****
echo *****  Please indicate below if this is the MASTER or not.                   *****
echo *****                                                                        *****
echo *****  W A R N I N G :                                                       *****
echo *****                                                                        *****
echo *****     Y = Do not do anything                                             *****
echo *****     N = start cloning process                                          *****
echo *****                                                                        *****
echo **********************************************************************************
echo **********************************************************************************

choice /C YN /N /T 120 /D N /M "Is this a Master (Y/N): "
if %ERRORLEVEL% EQU 1 exit /b

c:\windows\system32\XYZ.exe

~bp
0

Commented:
Also not that if the actual program path you want to execute contains spaces then you will need to quote it as in:

"c:\windows\system32\XYZ.exe"

~bp
0

Author Commented:
billprew,
Only yes option works "N" doesn't do do anything, it should run  command from c:\windows\system32when user etihter select N or don't do any thing
Thanks
0

Commented:

@echo off

echo **********************************************************************************
echo **********************************************************************************
echo **********************************************************************************
echo *****                                                                        *****
echo *****                         I M P O R T A N T                              *****
echo *****                                                                        *****
echo *****  Please indicate below if this is the MASTER or not.                   *****
echo *****                                                                        *****
echo *****  W A R N I N G :                                                       *****
echo *****                                                                        *****
echo *****     Y = Do not do anything                                             *****
echo *****     N = start cloning process                                          *****
echo *****                                                                        *****
echo **********************************************************************************
echo **********************************************************************************

choice /C YN /N /T 120 /D N /M "Is this a Master (Y/N): "
if %ERRORLEVEL% EQU 2 GOTO No
if %ERRORLEVEL% EQU 1 GOTO Yes
GOTO END

:Yes
ECHO DOING SOMETHING COMMAND LINE
GOTO END

:No
ECHO DOING NOTHING COMMAND LINE
GOTO END

:END
PAUSE
EXIT

0

Commented:
I should have payed more attention.

@echo off

echo **********************************************************************************
echo **********************************************************************************
echo **********************************************************************************
echo *****                                                                        *****
echo *****                         I M P O R T A N T                              *****
echo *****                                                                        *****
echo *****  Please indicate below if this is the MASTER or not.                   *****
echo *****                                                                        *****
echo *****  W A R N I N G :                                                       *****
echo *****                                                                        *****
echo *****     Y = Do not do anything                                             *****
echo *****     N = start cloning process                                          *****
echo *****                                                                        *****
echo **********************************************************************************
echo **********************************************************************************

choice /C YN /N /T 120 /D N /M "Is this a Master (Y/N): "
if %ERRORLEVEL% EQU 2 GOTO No
if %ERRORLEVEL% EQU 1 GOTO Yes
GOTO END

:Yes
ECHO DOING NOTHING
GOTO END

:No
ECHO DOING SOMETHING COMMAND LINE
"c:\windows\system32\XYZ.exe"
GOTO END

:END
PAUSE
EXIT

0

Commented:
If my version, build on Bill's script works, please just give me few thanks points and give most to Bill. As he did all the work.
0

IT ConsultancyCommented:
Just for a change here is a quick VBScript which does similar:
REM VBScript version of display of Yes/No with timeout for EE question 27424321
REM Steve Knight (dragon-it) - 31/10/2011

Const Yes = 6
Const No = 7
Const YesNoDialog = 4
Const QuestionMark = 32

Dim objShell, answer, Delay, Title, Question

Delay=120 'Delay In seconds

Question=_
"+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+" & chr(10) & _
"+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+" & chr(10) & _
chr(10) & _
"                          IMPORTANT" & chr(10) & _
chr(10) & _
"  Please choose whether this is the MASTER" & chr(10) & _
chr(10) & _
"  YES = Do nothing" & chr(10) & _
"   NO = Start cloning process" & chr(10) & _
chr(10) & _
"+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+" & chr(10) & _
"+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+" & chr(10)

Set objShell = CreateObject("Wscript.Shell")

answer = objShell.Popup(Question, Delay, Title, YesNoDialog + QuestionMark)

Wscript.quit
Else
objShell.Run "%SystemRoot%\System32\xyz.exe",,False
End If

PopUp.vbs
0

IT ConsultancyCommented:
Or I suppose better using built-in constants and making it appear "always on top":

Steve
REM VBScript version of display of Yes/No with timeout for EE question 27424321
REM Steve Knight (dragon-it) - 31/10/2011

Dim objShell, answer, Delay, Title, Question

Delay=120 'Delay In seconds

Question=_
"+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+" & chr(10) & _
"+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+" & chr(10) & _
chr(10) & _
"                          IMPORTANT" & chr(10) & _
chr(10) & _
"  Please choose whether this is the MASTER" & chr(10) & _
chr(10) & _
"  YES = Do nothing" & chr(10) & _
"   NO = Start cloning process" & chr(10) & _
chr(10) & _
"+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+" & chr(10) & _
"+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+" & chr(10)

Set objShell = CreateObject("Wscript.Shell")

answer = objShell.Popup(Question, Delay, Title, vbYesNo + vbQuestion + vbDefaultButton2 + vbSystemModal)

Wscript.quit
Else
objShell.Run "%SystemRoot%\System32\xyz.exe",,False
End If

0

Commented:
@wajhiuddin

==> Only yes option works "N" doesn't do do anything, it should run  command from
==> c:\windows\system32when user etihter select N or don't do any thing

I just tested it again here, and that is not the case.  It works as you asked, if they enter N, or if they enter nothing for 2 minutes it runs the executable.  I just did a test and got this result (displaying the EXE name rather than actually running it for my test).  So I think the code works as requested.

[Tue 11/01/2011  9:38:02.86 c:\ee]ee27424321
**********************************************************************************
**********************************************************************************
**********************************************************************************
*****                                                                        *****
*****                         I M P O R T A N T                              *****
*****                                                                        *****
*****  Please indicate below if this is the MASTER or not.                   *****
*****                                                                        *****
*****  W A R N I N G :                                                       *****
*****                                                                        *****
*****     Y = Do not do anything                                             *****
*****     N = start cloning process                                          *****
*****                                                                        *****
**********************************************************************************
**********************************************************************************
Is this a Master (Y/N): N
c:\windows\system32\XYZ.exe

~bp
0

Commented:
@ReneGe,

What was your intent in rearranging my code slightly, seems to provide basically the same solution but with more work?

~bp
0

Commented:
And to cover the base where the user presses CTRL-C and then sayes N to "Terminate batch job?" I'll assume you would not want to run the EXE in that odd case, and adjust my code as follows to cover this.

@echo off

echo **********************************************************************************
echo **********************************************************************************
echo **********************************************************************************
echo *****                                                                        *****
echo *****                         I M P O R T A N T                              *****
echo *****                                                                        *****
echo *****  Please indicate below if this is the MASTER or not.                   *****
echo *****                                                                        *****
echo *****  W A R N I N G :                                                       *****
echo *****                                                                        *****
echo *****     Y = Do not do anything                                             *****
echo *****     N = start cloning process                                          *****
echo *****                                                                        *****
echo **********************************************************************************
echo **********************************************************************************

choice /C YN /N /T 120 /D N /M "Is this a Master (Y/N): "
if %ERRORLEVEL% LEQ 1 exit /b

c:\windows\system32\XYZ.exe

~bp
0

IT ConsultancyCommented:
Perhaps he hasn't got choice.exe ... only in Win 7, resource kit, 2003 R2, 2008 wasn't it?
0

Commented:
@dragon-it

Fair point Steve, although the statement:

==> Only yes option works "N" doesn't do do anything, it should run  command from

didn't make me think he was getting an error on the CHOICE command.
@wajhiuddin

Can you clarify exactly what you are seeing?
~bp
0

IT ConsultancyCommented:
@billprew Good point.  If choice.exe wasn't present I suppose it may trigger an error which does the "Y" process, don't know off hand what error is returned when an EXE gives a "not recogniszed as a command" error but don't suppose it is 1.

Will see what feedback we get....

Did you try the VBScript one above too :-) http:#37059750 revised slightly in next post

Steve
0

Commented:
Of course I tried yours :-).  Seemed to work for me.  Although I wouldn't pad the Title with leading spaces, messes up the task bar icon text (IMHO).

~bp
0

IT ConsultancyCommented:
It centred it (ish!).... though tend to agree!
0

IT ConsultancyCommented:
(I meant did wajhiuddin try mine btw, not you Bill!)
0

Commented:
Yup, I knew what you were after in the dialog box title area, and that was okay, but in the taskbar icon it pushes the text far to the right and looks odd or truncates.  I'd stick with it left justified...

~bp
0

Commented:
(I think maybe you're trying to hard to be "artistic" Steve :-) ).

~bp
0

Author Commented:
thanks to billprew,ReneGe and dragon-it it works as  you all sugested
0

Commented:
@wajhiuddin:
Glad I could somehow help and thanks for the points.

@billprew:

I do not understand the nature of your question.  I thought that by adding these few lines, it would make it visually easyer to understand for the poster.

Cheers,
Rene

0

Commented:
@ReneGe

I think I'll just drop my question, I felt a bit like all you did was reformat my code and repost it, and for me that isn't in the spirit of EE.  That being said, it's sometimes a hard line to walk, and I know that.  So I guess it is what it is, if that's your style in responding to questions then so be it, live and let live.

~bp
0

IT ConsultancyCommented:
0

IT ConsultancyCommented:
Bill, Rene did say "If my version, build on Bill's script works, please just give me few thanks points and give most to Bill. As he did all the work. "
0

Commented:
Thanks Steve :)

Bill, I have the most respect for you, and I owe what I know in regards to Batch File scripting to mostly you, Steve, the Qs, and just a few other highly skilled Experts. Your contribution permit me to develop great and powerfull tools that made my life a lot easyer. If I offended you in some way, please forgive my innocence and sometimes lack of humain skills. Helping and receiving a note of some gratitude or points makes me feel good, and, I don't see the difference.

I hope you'll understand my nature and assume the best.

Truly,
Rene

0

Commented:
Guys, it's all good. That's why I said I'm not gonna flame off on this, but did want to at least share a thought or two.  You guys know I enjoy the interaction with you here, and I know we all take pride in our work.  I'll keep my ego in check...

~bp
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.