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

Author Topic: Writing batch files in batch files help  (Read 6500 times)

0 Members and 1 Guest are viewing this topic.

kamak

    Topic Starter


    Rookie

    Writing batch files in batch files help
    « on: April 21, 2008, 11:12:42 PM »
    Hi, i've come across a small problem in my coding and was wondering if anyone could help me find a way around it.

    My problem is when i try and write a new batch file in a batch file using something like ECHO exampletext > example.bat i can't write the % for variables.

    eg.

    if i was trying to write the following through the command i mentioned earlier,

    set /p answer =
    if /i %answer% == hi'
    echo correct!


    it would come out as:


    set /p answer=
    if /i == hi
    echo correct!

    Please help!

    gpl



      Apprentice
    • Thanked: 27
      Re: Writing batch files in batch files help
      « Reply #1 on: April 22, 2008, 02:42:04 AM »
      Try 'escaping' any special DOS characters by preceding them with a ^ symbol
      for example
      Echo ^>
      Echo ^%

      etc

      Graham

      kamak

        Topic Starter


        Rookie

        Re: Writing batch files in batch files help
        « Reply #2 on: April 22, 2008, 05:05:55 AM »
        Thanks for the reply, but it didn't work  :(

        i'll post my code, and the what i did after you posted.

        @echo off
        echo please choose a password!
        set /p input=

        echo @echo off > pconfig.bat
        echo set /p pw = >> pconfig.bat
        echo :A >> pconfig.bat
        echo if /i %pw% == %input% ( >> pconfig.bat
        echo CALL 94lock.bat >> pconfig.bat
        echo pause >> pconfig.bat
        echo   cls >> pconfig.bat
        echo ) else goto next >> pconfig.bat

        echo :next >> pconfig.bat
        echo echo Wrong password >> pconfig.bat
        echo pause >> pconfig.bat
        echo goto :A >> pconfig.bat


        i assume you meant trying:
        echo if /i ^%pw% == %input% ( >> pconfig.bat

        any more suggestions?  :P





        RandomGuy



          Rookie

          Re: Writing batch files in batch files help
          « Reply #3 on: April 22, 2008, 05:09:52 AM »
          hey kamak, i tried echo if /i ^%pw% == %input% ( >> pconfig.bat, i also tried echo if /i ^%pw^% == %input% ( >> pconfig.bat, and neither of them work.  :-\
          « Last Edit: April 22, 2008, 05:51:26 AM by RandomGuy »

          gpl



            Apprentice
          • Thanked: 27
            Re: Writing batch files in batch files help
            « Reply #4 on: April 22, 2008, 06:12:21 AM »
            It really helps to make things clear if you place the redirection at the start of the line, you can then concentrate on what you are writing out.

            The % symbols around the input also need escaping -

            Code: [Select]
            @echo off
            echo please choose a password!
            set /p input=

              > pconfig.bat echo @echo off
            >> pconfig.bat echo set /p pw =
            >> pconfig.bat echo :A
            >> pconfig.bat echo if /i ^%pw^% == ^%input^% (

            >> pconfig.bat echo CALL 94lock.bat
            >> pconfig.bat echo pause
            >> pconfig.bat echo   cls
            >> pconfig.bat echo ) else goto next

            >> pconfig.bat echo :next
            >> pconfig.bat echo echo Wrong password
            >> pconfig.bat echo pause
            >> pconfig.bat echo goto :A

            Graham

            blastman



              Hopeful

              Re: Writing batch files in batch files help
              « Reply #5 on: April 22, 2008, 06:20:46 AM »
              this simple batch makes a variable and then creates another batch and uses that variable.

              Code: [Select]
              @echo off

              set answer=hello world

              echo.@echo off >>C:\test2.bat
              echo.echo %answer% >>C:\test2.bat
              echo.pause >>C:\test2.bat
              echo.exit >>C:\test2.bat

              start C:\test2.bat

              exit


              hope it helps



              Blastman, you are the man. Thank You Very Much!!!!!!!!!



              kamak

                Topic Starter


                Rookie

                Re: Writing batch files in batch files help
                « Reply #6 on: April 22, 2008, 06:21:02 AM »
                Thanks for the tip about the setting out, will make it much easier to read :P

                Only problem is my code still isn't working :(

                it's still showing as:

                @echo off
                set /p pw =
                :A
                if /i  == hey (
                CALL 94lock.bat
                pause
                  cls
                ) else goto next
                :next
                echo Wrong password
                pause
                goto :A

                EDIT:

                Quote
                this simple batch makes a variable and then creates another batch and uses that variable.


                Code:
                @echo off

                set answer=hello world

                echo.@echo off >>C:\test2.bat
                echo.echo %answer% >>C:\test2.bat
                echo.pause >>C:\test2.bat
                echo.exit >>C:\test2.bat

                start C:\test2.bat

                exit



                hope it helps

                That code creates a file with hello world in it, right?

                If it does, it wasn't quite what i wanted, but thanks for trying :)



                blastman



                  Hopeful

                  Re: Writing batch files in batch files help
                  « Reply #7 on: April 22, 2008, 06:51:08 AM »
                  That code creates a file with hello world in it, right?

                  If it does, it wasn't quite what i wanted, but thanks for trying :)


                  not quite, it creates a variable and then echo's a few lines of code to a new batch file and runs it, displaying the varaible.

                  I thought the issue was that your variable wasn't echo-ing into your new batch file correctly. I'm sorry if i got the wrong end of the stick....


                  Blastman, you are the man. Thank You Very Much!!!!!!!!!



                  kamak

                    Topic Starter


                    Rookie

                    Re: Writing batch files in batch files help
                    « Reply #8 on: April 22, 2008, 06:52:39 AM »
                    That code creates a file with hello world in it, right?

                    If it does, it wasn't quite what i wanted, but thanks for trying :)


                    not quite, it creates a variable and then echo's a few lines of code to a new batch file and runs it, displaying the varaible.

                    I thought the issue was that your variable wasn't echo-ing into your new batch file correctly. I'm sorry if i got the wrong end of the stick....




                    My problem was that it wouldn't print %pw% in the new file :)

                    RandomGuy



                      Rookie

                      Re: Writing batch files in batch files help
                      « Reply #9 on: April 22, 2008, 07:10:53 AM »
                      (kamak and i are actually working together btw) we want to print the actual % variable signs in the pconfig.bat file

                      blastman



                        Hopeful

                        Re: Writing batch files in batch files help
                        « Reply #10 on: April 22, 2008, 07:13:11 AM »
                        (kamak and i are actually working together btw) we want to print the actual % variable signs in the pconfig.bat file

                        arh, I didn't get that from the other posts.

                        sorry for the confusion.

                        Blastman, you are the man. Thank You Very Much!!!!!!!!!



                        Dias de verano

                        • Guest
                        Re: Writing batch files in batch files help
                        « Reply #11 on: April 25, 2008, 01:24:19 PM »
                        In a batch, to echo one percent sign, use two percent signs

                        Code: [Select]
                        @echo off
                        echo @echo off>test.bat
                        echo set variable=cats and dogs>> test.bat
                        echo echo variable=%%variable%%>>test.bat
                        echo this is test.bat
                        type test.bat
                        call test.bat

                        result

                        Code: [Select]
                        this is test.bat
                        @echo off
                        set variable=cats and dogs
                        echo variable=%variable%
                        variable=cats and dogs



                        « Last Edit: April 27, 2008, 12:24:52 AM by Dias de verano »

                        RandomGuy



                          Rookie

                          Re: Writing batch files in batch files help
                          « Reply #12 on: April 26, 2008, 11:21:38 PM »
                          THANKYOU Dias de verano! it helped us heaps! :)