Solved

Batch GOTO Problem

Posted on 2004-10-09
8
729 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

757 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