Solved

Batch GOTO Problem

Posted on 2004-10-09
8
784 Views
Last Modified: 2008-02-01
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.
0
Comment
Question by:foxdesignz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 3

Expert Comment

by:KeithWatson
ID: 12266886
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.
0
 
LVL 1

Author Comment

by:foxdesignz
ID: 12266954
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.
0
 
LVL 3

Expert Comment

by:KeithWatson
ID: 12267017
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
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:KeithWatson
ID: 12267035
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
0
 
LVL 1

Author Comment

by:foxdesignz
ID: 12267193
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.
0
 
LVL 3

Accepted Solution

by:
KeithWatson earned 250 total points
ID: 12267228
Apologies, made a typo: it's just:

if not defined test goto create

echo off
set comm=
cls
:begin
set /p name=Select User Name:
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
goto prob
:create
echo Got to the 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
echo Processing
set test=%~1
echo I'm here
pause
goto EOF
:prob
echo The Username entered is already in use.
pause
:exit
cls
:EOF
0
 
LVL 1

Author Comment

by:foxdesignz
ID: 12267254
Works like a charm. Thx alot man. Heres your points, dont use them all in one place :)
0
 
LVL 3

Expert Comment

by:KeithWatson
ID: 12267265
No problem... Glad it's sorted.
0

Featured Post

Raise the IQ of Your IT Alerts

From IT major incidents to manufacturing line slowdowns, every business process generates insights that need to reach the people required to take action. You need a platform that integrates with your business tools to create fully enabled DevOps toolchains.

You need xMatters.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Progress

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question