?
Solved

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

Posted on 2011-10-31
27
Medium Priority
?
423 Views
Last Modified: 2012-05-12
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
Comment
Question by:wajhiuddin
  • 11
  • 8
  • 5
  • +2
27 Comments
 
LVL 7

Expert Comment

by:Rahul_Gade
ID: 37059235
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
 
LVL 59

Assisted Solution

by:Bill Prew
Bill Prew earned 800 total points
ID: 37059273
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

Open in new window

~bp
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 37059287
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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 

Author Comment

by:wajhiuddin
ID: 37059498
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
 
LVL 10

Expert Comment

by:ReneGe
ID: 37059715

@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

Open in new window

0
 
LVL 10

Expert Comment

by:ReneGe
ID: 37059735
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

Open in new window

0
 
LVL 10

Assisted Solution

by:ReneGe
ReneGe earned 200 total points
ID: 37059750
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 37059963
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
Title="                           IMPORTANT - PLEASE ANSWER"

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)

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

Open in new window

PopUp.vbs
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 37059998
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
Title="                           IMPORTANT - PLEASE ANSWER"

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)

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

Open in new window

0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 37062752
@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

Open in new window

~bp
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 37062798
@ReneGe,

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

~bp
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 37062814
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

Open in new window

~bp
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 37062829
Perhaps he hasn't got choice.exe ... only in Win 7, resource kit, 2003 R2, 2008 wasn't it?
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 37062857
@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
 
LVL 43

Accepted Solution

by:
Steve Knight earned 1000 total points
ID: 37062903
@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
 
LVL 59

Expert Comment

by:Bill Prew
ID: 37062999
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 37063014
It centred it (ish!).... though tend to agree!
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 37063020
(I meant did wajhiuddin try mine btw, not you Bill!)
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 37063033
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
 
LVL 59

Expert Comment

by:Bill Prew
ID: 37063037
(I think maybe you're trying to hard to be "artistic" Steve :-) ).

~bp
0
 

Author Closing Comment

by:wajhiuddin
ID: 37065960
thanks to billprew,ReneGe and dragon-it it works as  you all sugested
0
 
LVL 10

Expert Comment

by:ReneGe
ID: 37066080
@wajhiuddin:
Glad I could somehow help and thanks for the points.

@billprew:
In response to your question addressed to me at 37062798:

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
 
LVL 59

Expert Comment

by:Bill Prew
ID: 37066323
@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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 37066690
np, glad it helped.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 37066697
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
 
LVL 10

Expert Comment

by:ReneGe
ID: 37066775
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
 
LVL 59

Expert Comment

by:Bill Prew
ID: 37067425
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

Featured Post

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question