jcunews

timed-input-sample.bat

Jan 29th, 2022 (edited)
655
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 1.47 KB | None | 0 0
  1. @echo off
  2. setlocal
  3.  
  4. rem create a 100ms delayer/timeout vbscript
  5. >"%temp%\delay.vbs" echo wsh.sleep 100
  6.  
  7. rem create prompter batch file. input will be put into a file
  8. >"%temp%\ask.bat" echo @echo off
  9. >>"%temp%\ask.bat" echo set/p "inp=Enter input within about 10 seconds: "
  10. >>"%temp%\ask.bat" echo if "%%inp%%" == "" (
  11. >>"%temp%\ask.bat" echo ^>"%temp%\input.tmp" rem.
  12. >>"%temp%\ask.bat" echo ) else (
  13. >>"%temp%\ask.bat" echo ^>"%temp%\input.tmp" echo %%inp%%
  14. >>"%temp%\ask.bat" echo )
  15.  
  16. rem run prompter in separate process but in current console window
  17. 2>nul del "%temp%\input.tmp"
  18. set id=%random%%random%%random%
  19. start /b cmd.exe /c "%temp%\ask.bat" %id%
  20.  
  21. rem set timeout duration to 10 seconds (100 * 100ms)
  22. set t=100
  23.  
  24. :chk
  25. rem answer file exist?
  26. if not exist "%temp%\input.tmp" (
  27.  
  28.   rem still has time to wait?
  29.   if %t% gtr 0 (
  30.  
  31.     rem generate 1 second delay
  32.     cscript //nologo "%temp%\delay.vbs"
  33.  
  34.     rem decrease time to wait
  35.     set/a t-=1
  36.  
  37.     rem check again
  38.     goto chk
  39.  
  40.   ) else (
  41.  
  42.     rem no more time to wait
  43.     echo.
  44.     echo timed out.
  45.  
  46.     rem terminate prompter process
  47.     wmic process where 'commandline like "%%%id%"' call terminate>nul 2>&1
  48.  
  49.     rem all done
  50.     goto end
  51.  
  52.   )
  53.  
  54. )
  55.  
  56. rem get and display input
  57. set inp=
  58. for /f "usebackq delims=" %%A in ("%temp%\input.tmp") do set "inp=%%A"
  59. call echo Enterred input: %inp%
  60.  
  61. :end
  62. rem cleanup
  63. 2>nul del "%temp%\input.tmp"
  64. del "%temp%\ask.bat"
  65. del "%temp%\delay.vbs"
  66.  
Add Comment
Please, Sign In to add comment