0

I'm trying to create an alarm clock service via CMD Prompt. The service can be programmed to a certain time, and will play a song and ask a question before terminating. I'm running into two difficulties:

1) The command prompt window opens and then closes when it reaches the if/else statement (doesn't even pause after the if/else as coded below). Is my if statement faulty? It was literally working up until the time changed to 11:00 PM, and then the faulty behavior began.

2) Using more in the for loop to read the nth line of a file (https://stackoverflow.com/a/6410343/3746582) gave an error saying Cannot access file (filename) before the if/else statement started to fault. Any thoughts?

@ECHO OFF
rem Alarm generated that won't turn off until interacted by user.

rem Get time
For /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (set mytime=%%a%%b)

if %mytime% == 2328 (
    start wmplayer "C:\Users\Public\Music\Sample Music\Kalimba.mp3"
    pause

    rem Stop music with user input
    taskkill /im "wmplayer.exe"

    rem Pick random question
    SET /a _rand=%RANDOM%*216929/32768+1 
    SET /a _rand-=1
    For /f "tokens=6-7 delims=," %%a in (more +%_rand% 'JEOPARDY_CSV.txt') do (set /a myquestion = %%a && set /a myanswer = %%b)
    set /p userinp=Answer the question of the day! %myquestion%:%=%     
    pause
    ) else (
    pause
    )
pause

Thanks!

Community
  • 1
  • 1
rselvak6
  • 15
  • 1
  • 5

1 Answers1

0

Here is the solution to both of your problems:

Problem 1:

The most likely problem is the variable mytime does not have a value set (Which suggests the for loop before it doesn't work). As a general precaution you should always use "'s when in if statements.

if "%mytime%" EQU "2328" (

As for your for-loop I would recommend instead using variable syntax instead (set /? | more +78). Replace your for-loop with:

set mytime=%time:~0,2%%time:~3,2%

And that should fix problem 1 (as in your if statement should not crash).

Problem 2:

In batch, referring to a variable within (...brackets...) you will refer to the value of the variable BEFORE the start of the brackets. Hence your variables don't exist!

To fix this we use setlocal enabledelayedexpansion (Setlocal /?) and inside the brackets we replace % with !.

One Last thing which might explain why your code is not working is there is no Loop! You could use a simple goto loop. I have added the goto statement to your code.

I also added "usebackq" to the for statement so the more command works.

So your finished code should look like:

@ECHO OFF
setlocal enabledelayedexpansion
title Alarm Clock

:loop
set mytime=%time:~0,2%%time:~3,2%

if "%mytime%" EQU "2328" (
    start wmplayer "C:\Users\Public\Music\Sample Music\Kalimba.mp3"
    pause

    rem Stop music with user input
    taskkill /im "wmplayer.exe"

    rem Pick random question
    SET /a _rand=!RANDOM!*216929/32768+1 
    SET /a _rand-=1
    For /f "usebackq tokens=6-7 delims=," %%a in ('more +!_rand! JEOPARDY_CSV.txt') do (set /a myquestion = %%a && set /a myanswer = %%b)
    set /p userinp=Answer the question of the day !myquestion!:%=%     
    pause
    ) else (
    goto loop
    )
pause
endlocal

Which should work for you.

Monacraft
  • 6,510
  • 2
  • 17
  • 29
  • Thank you @Monacraft - Problem 1 is solved by your solution, but Problem 2 is only partially solved. First- I needed to put quotes (" ") around the `more +!_rand!...` statement to read the text file properly. Unfortunately, in the command window, the text file is not being read properly and the variables `myquestion` and `myanswer` are not being set at all. This is seen when trying to access `!myquestion!` . Any thoughts? Finally, could you explain how there was no loop in my code? Or why a loop is needed? Thank you for the help! – rselvak6 Jan 27 '15 at 16:22
  • Try my edited code solution. Also if my solution is helpful don't forget to mark it right – Monacraft Jan 28 '15 at 09:48
  • Unfortunately the script is not letting me access the file with the `Cannot access file (file directory)` error. This error only arises when using the `more` command- do you have any suggestions as to why that is? Thanks again for the help so far! – rselvak6 Jan 28 '15 at 15:15