Link to home
Start Free TrialLog in
Avatar of foxdesignz
foxdesignz

asked on

Batch GOTO Problem

I'm sure this is easy, so dont point and laugh. But i'm getting aggravated at this little problem i'm having.
When i debug my batch file a error shows that "goto was unexpected at this time" right here:

if not %test%==%name% goto create

why in the world is this happening???, i've changed it around so the variables had quotes, or a space in between the equal signs, i've even tried taking the goto command of it and placing it underneath. I'm desperate.
Avatar of KeithWatson
KeithWatson

Strange,

It works for me in this program; perhaps there's something more subtle going on in your own program. Could you post it?

@echo off
set test=Hi
set name=NotHi

if not %test%==%name% goto create

goto finish

:create
echo Hello

:finish
echo finishing.


When I run this, I get:

Hello
finishing.

Implying that it did go to the :create label.
Avatar of foxdesignz

ASKER

This is where my problem is (******)

echo off
set comm=
cls
:begin
set /p name=Select User Name:
findstr "%name%" namelog.txt > nametmp.txt
set test=
for /f "tokens=1 delims= " %%a in ('type nametmp.txt') do CALL :process %%~a
if not %test%==%name% goto create    ****** ****** ****** ****** ****** ******
goto prob
:create
echo %name% >> namelog.txt
echo %name% was added succesfully to the LOG file
set /p comm=Create a new one? Y or N
if %comm%==Y goto begin
goto exit
:process
set test=%~1
echo I'm here
pause
goto EOF
:prob
echo The Username entered is already in use.
pause
:exit
cls
:EOF

I've been thinking that the call command above it might have had something to do with it. Cause when i debug this it acts like it dosen't go to :process at all before it reaches the if command.
I'm not sure, i'm speculating about everything, because i'm so confused about it.
It looks as though if you enter a name that doesn't exist, then the "test" variable is never set to anything, since it never calls the process procedure, which set the variable; being set to null appears to be confusing the statement that's having the problem.

As a test, I set it to something and this seems to solve the problem.

set test=XXXXXXXX
To tidy this up, you can use "if not defined" to check if the variable has been set up, rather than setting it to nothing to start with; so:

findstr "%name%" namelog.txt > nametmp.txt
for /f "tokens=1 delims= " %%a in ('type nametmp.txt') do CALL :process %%~a
if not defined %test% goto create
if not %test%==%name% goto create
This didn't do anything. I typed it in and now instead of just the goto error, it now says that create is not a internal or external command for the if not defined. I'm running XP Pro in case you need to know.
ASKER CERTIFIED SOLUTION
Avatar of KeithWatson
KeithWatson

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Works like a charm. Thx alot man. Heres your points, dont use them all in one place :)
No problem... Glad it's sorted.