?
Solved

Batch GOTO Problem

Posted on 2004-10-09
8
Medium Priority
?
786 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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
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 1000 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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 …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Suggested Courses

752 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