Press Any Key to Continue Batch Script
Why batch?
No, really. It's great for quick things, but complex scripts should use a proper scripting language. vbScript at the very least, but PowerShell should be what you're using at this point.
thumb_up thumb_down
as im trying to do this in PDQ deploy and the only script it gives me to make is a bat file
thumb_up thumb_down
If you are waiting for a user prompt then it has to be a user script, but if it is uninstalling a program, unless it is an administrator that logs in the script might fail anyway. Unless I'm missing something.
Edit: although I don't know how pdq does the script thing. I was thinking logon scripts.
thumb_up thumb_down
yeah the script will be a user prompt to say press any key to quit otherwise it will continue with all the other steps to unistall/install programs
thumb_up thumb_down
Batch is really a sequential scripting language. Good for doing things in a certain order, so canceling during the uninstall operation without a a control-break is not going to work.
However you could do a script that waits for 30 seconds, press ctrl+c to cancel.
@echo off
echo Your PC will shutdown in 30 seconds! Press CTRL+C to abort.
ping -n 31 127.0.0.1>nul
<Enter install/uninstall command(s) here>
I think you could get fancier with a loop, maybe set input, if input != nul, then loop 30 times, checking to see if input is != nul would allow for an ANY key idea.
thumb_up thumb_down
Given that it would probably take more than 30 seconds for the average user to look for the "any" key, and then ask someone for help, I would specify pressing the space bar, but leave the script to accept any input.
thumb_up thumb_down
great thanks joshua it works!!!
is there anyway to show the time counting down so the user knows how much time they have left
how do i replace the CTRL+C with the space bar instead
thumb_up thumb_down
A tricky stuff you need. Neither pause nor timeout doesn't return a useful exit code you could use to make a decision. You need to "cheat" timeout command so it returns something different for break and something different for timeout.
Batchfile
timeout /t 30 |findstr /r ".0$" && goto continue || exit : continue REM continue installation of stuff after this point
When you pipe timeout into find / findstr, it will display the current counter after the "press any key to continue." message. Now when you search for regular expression ".0$" in it, it will only be true when timeout has occurred, and will be false if some different number after the dot appears when a key was pressed during the wait. We can make use of it to actually check if key was pressed or not.
thumb_up thumb_down
great thanks joshua it works!!!
is there anyway to show the time counting down so the user knows how much time they have left
how do i replace the CTRL+C with the space bar instead
You can't. Besides ctrl+c always ends the script, so it's useless if you want to do some additional maintenance when installation is canceled.
thumb_up thumb_down
It might be easier to just train the users how to use CTRL + C and then just use a TIMEOUT for 30 seconds (or more).
thumb_up thumb_down
but how do i add a timeout command to Joshuas script?
thumb_up thumb_down
Robert,
I am not sure we can pull off what you want. My script says, hey I am about to do something in 30 seconds, then uses the ping command 30 times to create a 30 seconds delay/timeout. then runs a command
However what I believe you want is hey i am about to do something, press any key to cancel, then it waits 30 seconds for an input and then performs it if no keys were entered
I am not sure batch can do this. However here are some alternatives.
- just deal with ctrl+c for breaking out of the script
- A batch file can call vbs script. which I think VBS will do what you want. (VBS is windows native)
- Bacon
thumb_up thumb_down
More help:
In windows vista/7 you have the choice command, which I think does what you want with C and N as input options
Batchfile
@ echo off choice /c:CN /n /t:N,1800 "PC will restart in 30 minutes. Press N to restart Now, or C to Cancel" if errorlevel 2 (shutdown -r -t 60 -f) else echo Restart Canceled
then if you want some VBS to do it, I like this one:
VB.net
Set objShell = CreateObject ( "WScript.Shell" ) for i = 30 to 1 step - 1 if i = 1 then unit = " minute" else unit = " minutes" rtn = objShell . Popup ( _ "The machine would like to Restart." & VbCrLf&VbCrLf& _ "Click OK to restart now" & VbCrLf& _ "Click Cancel or the [X] close button to abort the restart" & VbCrLf& _ "Do nothing and the machine will restart in " & i&unit , _ 60 , "Restart in " & i&unit , 1 + 48 _ ) if rtn <> - 1 then exit for next if rtn <> 2 then objShell . Run "shutdown -r -f"
This what my google-fu turned up:Stack Overflow
Also you seem new to this, so i will add some more to this: You can take that vbs code copy it to notepad, then save it as a .vbs file.
Then from your batch file you can call that .vbs file form a unc path \\baconserver\share\command-promt.vbs
(I like this VBS script a lot and posted it to the spiceworks scripts page) If you have never heard of the spiceworks scripts page, it is awesome. You could probably search that all day every day and find any script you are looking for.(http://community.spiceworks.com/scripts)
thumb_up thumb_down
thanks
can i do a choice to either quit or to continue onto the next stage of the batch file
thumb_up thumb_down
Yes, that is what the choice command gives you
choice /c:CN /n /t:N,1800 "PC will restart in 30 minutes. Press N to restart Now, or C to Cancel" if errorlevel 2 (shutdown -r -t 60 -f) else echo Restart Canceled
Using
C to cancel
N for now
as the choice options
thumb_up thumb_down
Source: https://community.spiceworks.com/topic/1320401-batch-script-press-any-key-to-exit
0 Response to "Press Any Key to Continue Batch Script"
Post a Comment