Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: need CALL command help.  (Read 3263 times)

0 Members and 1 Guest are viewing this topic.

RandomGuy

    Topic Starter


    Rookie

    need CALL command help.
    « on: April 26, 2008, 11:17:12 PM »
    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?

    RandomGuy

    Dias de verano

    • Guest
    Re: need CALL command help.
    « Reply #1 on: April 27, 2008, 12:40:41 AM »
    You can only use a label in the batch file in which it exists.

    RandomGuy

      Topic Starter


      Rookie

      Re: need CALL command help.
      « Reply #2 on: April 27, 2008, 01:22:04 AM »
      hmm.. wel that sucks. as my secondary option.. i wil giv u my code and see if anyone can help with my situation.

      <PROBLEM>
      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)
      « Last Edit: April 27, 2008, 01:42:52 AM by RandomGuy »

      Dias de verano

      • Guest
      Re: need CALL command help.
      « Reply #3 on: April 27, 2008, 01:38:43 AM »
      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?
      « Last Edit: April 27, 2008, 01:50:28 AM by Dias de verano »

      RandomGuy

        Topic Starter


        Rookie

        Re: need CALL command help.
        « Reply #4 on: April 27, 2008, 01:44:27 AM »
        yep, all fixed. :) but it stil makes an unwanted loop.. can u help wit tat one? :-\

        Dias de verano

        • Guest
        Re: need CALL command help.
        « Reply #5 on: April 27, 2008, 01:52:03 AM »
        Where is the loop occurring (in which batch file)?

        RandomGuy

          Topic Starter


          Rookie

          Re: need CALL command help.
          « Reply #6 on: April 27, 2008, 01:59:19 AM »
          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 loop

          Dias de verano

          • Guest
          Re: need CALL command help.
          « Reply #7 on: April 27, 2008, 02:22:00 AM »
          It 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 ..\



                     


          RandomGuy

            Topic Starter


            Rookie

            Re: need CALL command help.
            « Reply #8 on: April 27, 2008, 02:29:10 AM »
            :) 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*

            Dias de verano

            • Guest
            Re: need CALL command help.
            « Reply #9 on: April 27, 2008, 02:37:17 AM »
            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%!

                           

             





            « Last Edit: April 27, 2008, 02:51:32 AM by Dias de verano »

            ghostdog74



              Specialist

              Thanked: 27
              Re: need CALL command help.
              « Reply #10 on: April 27, 2008, 02:52:50 AM »
              @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.

              Dias de verano

              • Guest
              Re: need CALL command help.
              « Reply #11 on: April 27, 2008, 03:02:46 AM »
              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.


              RandomGuy

                Topic Starter


                Rookie

                Re: need CALL command help.
                « Reply #12 on: April 27, 2008, 07:43:00 AM »
                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!