' was unexpected at this time error

April 30, 2009 at 15:34:07
Specs: Windows XP
Hello all,

I've written a big batch file which prompts you to insert a password. The password changes every hour. I made it based on time an date variables. So you can use the password - after an hour passed - one month later again. Everything seems to be ok BUT when I hit the spacebar end press enter, I get the error '=='9807' was unexpected at this time.

By the way, 9807 is the password. As you can see you can get the password just by hitting the spacebar and enter.

Here the batch content:

@echo off
color 70
cls
set TIME=
for /F "tokens=1-4 delims=:., " %%a in ("%TIME%") do set T=%%a
set DATE=
for /F "tokens=1-4 delims=:., " %%a in ("%DATE%") do set D=%%c
goto t%d%%t%


:t010
echo Enter password to continue:
echo.
set choice=
set /p choice=
echo.
if '%choice%'=='8785' goto mainmenu
if not '%choice%'=='8785' goto wrongpass


:t011
echo Enter password to continue:
echo.
set choice=
set /p choice=
echo.
if '%choice%'='9807' goto mainmenu
if not '%choice%'=='9807' goto wrongpass


:t012
echo Enter password to continue:
echo.
set choice=
set /p choice=
echo.
if '%choice%'=='1179' goto mainmenu
if not '%choice%'=='1179' goto wrongpass
....
...
..
.

Can anybody help. At least the password shouldn't be displayed.


See More: was unexpected at this time error

Report •


#1
April 30, 2009 at 16:15:42
Try using " (double quotes) instead of ' (single quote)

Good luck


Report •

#2
April 30, 2009 at 16:24:36
Hi Valerie,

That's what I thought be didn't want to guess and get it wrong.

To OP,

Resetting built-in vars, like TIME & DATE, is usually a bad idea.


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#3
April 30, 2009 at 20:34:50
Hi M2G. Thanks for your comment.

There's more than one thing wrong and I hope the OP will find the rest soon. I don't see any point in resetting the Date & Time vars at all but there's more than one way of skinning a 'possum.

Kind regards

V..


Report •

Related Solutions

#4
May 1, 2009 at 01:33:50
Thank you Valerie, it worked.

Do you have another idea based on what other variables this could work? The batch will be sent as an exe file to end users but I don't want them to use the file without calling our helpdesk. In some cases I'm not able to esteblish a remote connection. So I will have to give the password to end user and guide him/her through the menus. An hour later the password won't work any more. Thats the idea.

Best Regards


Report •

#5
May 1, 2009 at 03:16:33
Having said that resetting system vars is a bad idea, and glossing over the whole notion of passwording a bat file, look at this bit. I've changed TIME to TIM and DATE to DAT. But regardless, if you start by clearing TIM & DAT you token will always be void.

================================
@echo off & setLocal EnableDelayedExpansion

set TIM=
for /F "tokens=1-4 delims=:., " %%a in ("%TIM%") do set T=%%a
set DAT=
for /F "tokens=1-4 delims=:., " %%a in ("%DAT%") do set D=%%c
echo goto t%D%%T%


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#6
May 2, 2009 at 04:50:30
Sorry Punica, I just don't get your logic. First you delete the vars %time% and %date% by using Set time= and Set date= then you try to access them in the For command lines. %time% and %date% are dynamic environment variables.

A lot of thought needs to go into what you are trying to do. For example can you be sure that the %time% format will always use the 24-hour clock, some systems may be set to the 12-hour clock with the am or pm suffix. Why are you accessing the %date% env var when the password changes and is tested with the hour only?

Below is a code snippet to show roughly what I'd do if I was coding a similar spec. The code is NOT fully tested.

:: Code begins...
@echo off
cls
setlocal enabledelayedexpansion

:: Set passwords (24) for each hour.
set pwords=8795 7654 3217 8790 1234 5467 2398 6543 4321 8765 9876 5687 ^
           3245 5679 6578 9785 8975 3456 2365 4569 4568 5698 6789 9324

:: Get current hour (Based on 24-hour clock).
set hour=%time:~0,2%
if %hour% lss 10 set hour=%hour:~-1%
set /a hour +=1

:: Set current password based on hour.
for /f "tokens=%hour%" %%1 in ("%pwords%") do (
    set pword=%%1
)

:: User to enter password.
echo.&echo.&echo.
set /p choice=             Enter password to continue:   
cls

:: Compare user entry with current password and branch if false.
if %choice% neq %pword% goto wrongpass 

cls
echo.&echo.&echo.
echo                       Password valid...processing continues...
:: Other coding here...
pause > nul
cls
exit/b

:wrongpass
cls
echo.&echo.&echo.
echo                       Password not valid at this time...
echo                       Processing terminated...
pause > nul
cls
exit/b

:: Code ends...

V.


Report •


Ask Question