3

When I start the batch file as posted below, I got this error ( was unexpected at this time.
I think this happens on the IF command line if %ad%==60 ( but I am not sure.

( was unexpected at this time.

@echo off
color 0f
title TITLE
mode con cols=50 lines=25
set ad = 0

set s = 0
set m = 0
set h = 0
set d = 0

if exist start.txt (
    del start.txt
    goto :1
) else (
    exit
)
:1
if %ad%==60 (
:: Something here
set ad = 0
)

:: MINUTES
if %s%==60 (
set /a m=m+1
set s = 0
)
:: HOURS
if %m%==60 (
set /a h=h+1
set m = 0
)
:: DAYS
if %h%==24 (
set /a d=d+1
set h = 0
)

cls
echo Something here...
timeout 1 > nul
set /a ad=ad+1
set /a s=s+1
goto :1

What could be the reason for this error message on execution of the batch file?

Mofi
  • 46,139
  • 17
  • 80
  • 143
Kake
  • 65
  • 1
  • 1
  • 7
  • 3
    Remove the spaces from all your `set var=value` expressions. Example `set ad = 0` => `set ad=0`. Else `ad` is empty, but the variable `ad` is set to `0` – jeb Dec 19 '17 at 23:28
  • @Kake Please read the answers on [Why is no string output with 'echo %var%' after using 'set var = text' on command line?](https://stackoverflow.com/a/26388460/3074564) and [Symbol equivalent to NEQ, LSS, GTR, etc. in Windows batch files](https://stackoverflow.com/a/47386323/3074564). – Mofi Dec 20 '17 at 13:34
  • Possible duplicate of [Why is no string output with 'echo %var%' after using 'set var = text' on command line?](https://stackoverflow.com/questions/26386697/why-is-no-string-output-with-echo-var-after-using-set-var-text-on-comman) – aschipfl Dec 20 '17 at 17:57
  • You can debug errors of this kind by running your script interactively (from a CMD prompt) with the `@echo off` line commented out. That way you'll be able to see your commands as they are when the % expansion has already been applied, which, in turn, will enable you to see the issue for yourself. And even if the issue is not immediately obvious when you observe it, you'll be at least helped by being able to see the specific command that's producing the issue. – Andriy M Dec 22 '17 at 07:35

3 Answers3

4

For a start,

set ad = 60

will not set ad to 60. It will set the variable adspace to space60, leaving ad to whatever it was before the script started.

In your case, it's almost certainly an empty string because the command that would result from that would be the same as that in the following transcript (note the error generated):

d:\pax> if ==60 (
( was unexpected at this time.

If you want nicely spaced out expressions, you already know how to do that, since you used it to increment h. In other words:

set /a "ad = 0"
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • 2
    The last part of your answer is a bit misleading, as `set /a` behaves diffenrently than `set`. `set /a` removes itself all spaces, it has nothing to do with the quotes. – jeb Dec 20 '17 at 08:49
  • jeb/Mofi, I'm not sure exactly what you think is misleading in the final paragraph. It provides a way to nicely set out an expression so that the variable, `=`, and value don't have to abut. If there's a bug in doing it that way, please let me know. – paxdiablo Dec 20 '17 at 21:49
2

This isn't technically an answer, (that has already adequately been provided). It is just an example to show you how you may optionally shorten your script a little:

@Echo Off
Color 0F
Title TITLE
Mode 50,25

Set /A ad=s=m=h=d=0

If Not Exist "start.txt" Exit /B
Del "start.txt"

:1
If %ad% Equ 60 (
    Rem Something here
    Set "ad=0"
)

Rem Minutes
If %s% Equ 60 Set /A m+=1,s-=60
Rem Hours
If %m% Equ 60 Set /A h+=1,m-=60
Rem Days
If %h% Equ 24 Set /A d+=1,h-=24

ClS
Echo Something here...
Timeout 1 >Nul
Set /A ad+=1,s+=1
GoTo :1

Notes:
1. Using Timeout 1 will not increment clocklike (in accurate seconds), it would be approximately one second plus the time taken to run through :1 again.
2. Be careful not to fall into Set /A's octal trap, by ensuring that you don't have leading 0's in your variables.

Compo
  • 36,585
  • 5
  • 27
  • 39
0

I have IF command without spaces, and also batched with command in quotes; still got error ( was unexpected at this time. when called batch file from command line.
If launched from explorer, the batch file crashed.

Here's the crash code:

if %file%==local (
    set miss=n
    set /p miss=Any missing?
    set miss=%miss:~0,1%

    if /I %miss%==y (
    Goto check2
    ) 
)

When I echo the value of miss before the IF statement, I get %miss:~0,1%

TO fix this, I enabled delayed expansion, to force values to be updated at runtime, and works ok now.

if %file%==local (
    set miss=n
    set /p miss=Any missing?
    set miss=!miss:~0,1!

    if /I !miss!==y (
    Goto check2
    ) 
)
Zimba
  • 2,854
  • 18
  • 26
  • Delayed expansion is only needed when you access a variable that was defined/changed within the same command block (a series of commands enclosed in parentheses). Your first snippet runs fine unless it's within a surrounding command block you didn't show. – Stephan Mar 17 '20 at 11:59
  • Ah yes, this code is in a code block of `If`, so only works when `DelayedExpansion` is enabled. Might help someone who overlooked it, and got same error – Zimba Mar 17 '20 at 14:06
  • 1
    You may want to include that into your answer (or show the whole code block) because as it stands now, your answer is wrong or at least misleading. – Stephan Mar 17 '20 at 14:49