Solved

Batch GOTO Problem

Posted on 2004-10-09
8
741 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
  • 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
 
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article will show, step by step, how to integrate R code into a R Sweave document
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
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 …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

863 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now