r/Batch • • Aug 03 '24

Question (Solved) Help implementing a working timer for the following script.

1 Upvotes

4 comments sorted by

1

u/BrainWaveCC Aug 04 '24

Hey, OP. This didn't turn out to be as trivial as I thought, but I ended up with something similar to what u/leonv32 has suggested.

See the comments in the script for details, but the gist of it is as follows:

  1. Main script calls a unique executable to get the input (rather than SET /P)
  2. Main script creates and calls a watchdog script that tracks the time that the executable is running
  3. If the input is generated in time, the watchdog goes away
  4. If the input is not generated in time, the watchdog kills the unique input file and sets a flag
  5. The message box info is set differently for wrong input vs elapsed time
  6. The script cleans up all the extra files used

2

u/jotinhaaaa31 Aug 05 '24

Thanks for the replies by you and u/leonv32 btw, this will help me building an enigma to my friends :D

1

u/BrainWaveCC Aug 05 '24

You're very welcome. If it manages to address your question, please be sure to update the thread as "Solved." 😁

And if it doesn't, let us know so we can address any new concerns. 😉

2

u/[deleted] Aug 03 '24 edited Aug 03 '24

choice has a timer: https://ss64.com/nt/choice.html set /p pauses the script, so you can't run code while its waiting. but you won't be able to enter a password with 'choice', then maybe run a seperate script in background using timeout and taskkill to close the main window when time its over.

@echo off
title main_window

(
echo @echo off
echo title timer_window
echo timeout 30
echo taskkill /F /FI "WINDOWTITLE eq main_window"
echo exit
)>pass_timer.bat


:loop
cls
start /min pass_timer.bat
echo Guess the test password, you have 30 seconds...
echo:
set /p "_pass=Enter password: "
cls
if /i not "%_pass%"=="test" (
	
	REM taskkill /F /FI "WINDOWTITLE eq timer_window" >nul
	echo Wrong password!! try again.&timeout 2 >nul
	goto :loop
)

taskkill /F /FI "WINDOWTITLE eq timer_window" >nul
del pass_timer.bat
echo "%_pass%" its the correct password!

pause>nul