1.

Solve : need CALL command help.?

Answer» HEY,

is it possible to call a label from another batch file?
because so far i know that it is possible to call directly from another label in the same file.. and also call another file from its beginning.. but can you call a certain label from another batch file?

RandomGuyYou can only use a label in the batch file in which it exists.
hmm.. wel that sucks. as my secondary option.. i wil giv u my code and see if anyone can help with my situation.


before starting my main batch file, (main.bat) it calls another batch file (pconfig.bat) to check for a password. this call function is one of the first functions in main.bat and when pconfig proves a correct password; it proceeds to call main.bat, which again calls for a password, which if correct calls main.bat etc ITS STUCK IN A LOOP! here is my code for each of the files.. PLEASE HELP.

here is the portion of code from main.bat

Code: [Select]echo off
mkdir config
title BATCH FILE
call config\cconfig.bat
if exist config\pconfig.bat (
call pconfig.bat
) else (
goto start
OTHER UNIMPORTANT CODE
:start
and here is the code for pconfig.bat

Code: [Select]@echo off
cls
:A
echo enter your password to continue
set /p pw=
if /i "%pw%"=="password" (
cd ..\
call main.bat
) else (
goto next
)
:next
cls
echo That's the wrong password.
pause
cls
goto :A
please help! thankyou.

RandomGuy
(of the RandomKamak team)When saying that things "suck" it usually a good idea to be 100% sure your code is error-free.

1. Enclose the variables on both side of an IF test in quote marks. If the password is a LITERAL string, (i.e. the password is actually "password") then it should have quote marks around it like this

Code: [Select]if /i "%pw%"=="password" (
2. If password is a variable it should have percent signs before and after like this %password%.

Code: [Select]if /i "%pw%"=="%password%" (
but I doubt that you intended that, since you have not used SET to assign a value (unless you did so outside the batch file)

Fix that and then perhaps we can see what's happening?
yep, all fixed. but it stil makes an unwanted loop.. can u help wit tat one? Where is the loop occurring (in which batch file)?it starts at the if command in main.bat, which wil prove positive and call pconfig.bat. if the if command in pconfig.bat proves positive, it calls main.bat... etc etc etc
there is your loopIt appears that you have not fully grasped the meaning of the CALL command. When cmd.exe is running a batch file and it comes to a CALL command, it leaves the first batch file and starts the second one. When (if!) the second one REACHES an exit point, control is passed back to the first batch file, at the line immediately following the CALL command. Your error lies in using, in pconfig.bat, following a correct password entry, the CALL command to get back to main.bat. That doesn't happen. You are starting a new copy of main.bat from the beginning. What you are doing is setting up an infinite series of CALLs.

main.bat
calls pconfig.bat
calls main.bat
calls pconfig.bat
calls main.bat
(etc until you run out of memory and/or patience)

What you need to do is merely exit from pconfig.bat if (and only if) the password is successfully entered. Then main.bat picks up again. You don't need to direct the second batch back home, cmd.exe knows where to go because it remembers where it was CALLed from. Also, I eliminated the duplication of cls by moving the first one to after the A: label.

Like this

Code: [Select]@echo off
:A
cls
echo enter your password to continue
set /p pw=
if /i "%pw%"=="password" goto OK
echo That's the wrong password.
pause
goto :A
:OK
cd ..\





Dias de verano, you've been a GREAT help! i mean, kamak and i aren't exactly newbies, but we are still learning. you have made the call command a lot clearer now! =]
*yay*Glad to help.

PS you don't need the colon before the loop name after a goto command, although it does no harm.

I.e. this works

Code: [Select]:loop
do-stuff
goto loop
also, personally I find my code is much more readable if, in the editor, I use tabs or spaces to indent code in batch files with labels in them so I can see them more easily (It makes people think you're an old FORTRAN programmer too, which is cool)

Code: [Select] @echo off
cls
echo Pet Chooser
:start
set /p animal=Choose your pet - cat or dog
if /I "%animal%"=="dog" goto kennel
if /I "%animal%"=="cat" goto basket
echo Only dog or cat allowed
goto start
:kennel
echo GRRRR
goto end
:basket
echo MEOW
:end
echo Goodbye from %animal%!









@kamak team. If you guys are seriously learning how to program, i would suggest you pick a programming language, that shows you programming CONCEPTS like arrays, functions, procedures etc. Ah, Ghostdog. I'm getting used to you jumping into batch programming threads straight after me. I suppose I should be flattered? I have to say, guys, that in my opinion ghostdog74 is quite right. Batch language is OK for noodling around, but it can form lots of bad habits if you intend to learn programming seriously.

i'm sure we will go into more advanced programming languages.. but this is our first major project btw. just a nice, simple batch program to publish. check out www.wilding.com.au for version 1.5 gamma of our batch program!


Discussion

No Comment Found