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

Author Topic: Batch file write variables to file  (Read 29974 times)

0 Members and 1 Guest are viewing this topic.

Linux711

    Topic Starter


    Mentor

    Thanked: 59
    • Yes
    • Programming Blog
  • Certifications: List
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows 7
Batch file write variables to file
« on: June 09, 2010, 07:21:57 PM »
I have this batch file. It asks the user to input a few things and then writes it to a file. The problem is that it does not write the variables to the file. It only partially works when I remove the whole if statement. By partially, I mean it writes everything to the file except leaves a trailing space after each line. I thought this was because I have a space before the >>, but when I remove the space it doesn't work at all. So can someone that understands this convoluted language help me figure this out?

Code: [Select]
@echo off
set /p yorn=Would you like to setup the server name etc.(y/n)?
if /i %yorn%==y (
set /p in=Maximum clients:
echo maxclients %in% > bla.cfg
set /p in=Bot limit:
echo serverbotlimit %in% >> bla.cfg
echo publicserver 0 >> bla.cfg
set /p in=Server title:
echo serverdesc "%in%" >> bla.cfg
) else (
echo said no.
)
pause
YouTube

"Genius is persistence, not brain power." - Me

"Insomnia is just a byproduct of, "It can't be done"" - LaVolpe

marvinengland



    Hopeful

    Thanked: 11
    Re: Batch file write variables to file
    « Reply #1 on: June 09, 2010, 07:51:27 PM »
    So can someone that understands this  language help me figure this out?



    C:\test>type lin.bat
    Code: [Select]
    @echo off
    setlocal enabledelayedexpansion
    set /p yorn=Would you like to setup the server name etc.(y/n)?
    if /i %yorn%==y (
            set /p in=Maximum clients:
            echo maxclients !in! > bla.cfg
            set /p in2=Bot limit:
            echo serverbotlimit !in2! >> bla.cfg
            echo publicserver 0 >> bla.cfg
            set /p in3=Server title:
            echo serverdesc "!in3!" >> bla.cfg
            echo Type bla.cfg
            type bla.cfg
    ) else (
            echo said no.
    )

    Output:

    C:\test>lin.bat
    Would you like to setup the server name etc.(y/n)?y
    Maximum clients:9
    Bot limit:12
    Server title:MyServer
    Type bla.cfg
    maxclients 9
    serverbotlimit 12
    publicserver 0
    serverdesc "MyServer"
    C:\test>
    USA

    Linux711

      Topic Starter


      Mentor

      Thanked: 59
      • Yes
      • Programming Blog
    • Certifications: List
    • Computer: Specs
    • Experience: Experienced
    • OS: Windows 7
    Re: Batch file write variables to file
    « Reply #2 on: June 09, 2010, 08:06:44 PM »
    Code: [Select]
    echo publicserver 0 >> bla.cfg
    when written without a space like this...

    Code: [Select]
    echo publicserver 0>> bla.cfg
    it doesn't work. Can you give me some suggestions?
    YouTube

    "Genius is persistence, not brain power." - Me

    "Insomnia is just a byproduct of, "It can't be done"" - LaVolpe

    marvinengland



      Hopeful

      Thanked: 11
      Re: Batch file write variables to file
      « Reply #3 on: June 09, 2010, 08:31:10 PM »
      Code: [Select]
      echo publicserver 0 >> bla.cfg
      when written without a space like this...

      Code: [Select]
      echo publicserver 0>> bla.cfg
      it doesn't work. Can you give me some suggestions?

      <<<<"echo publicserver 0 >> bla.cfg

      when written without a space like this...


      Code:
      echo publicserver 0>> bla.cfg

      it doesn't work. Can you give me some suggestions?">>>>>


      When numbers 1 or 2 are associated with redirection > symbols

      1>  redirects standard out  2> redirects standard error

      "0>"  does not redirect anything and you confused the cmd interpreter
      and there was no output.
      USA

      Linux711

        Topic Starter


        Mentor

        Thanked: 59
        • Yes
        • Programming Blog
      • Certifications: List
      • Computer: Specs
      • Experience: Experienced
      • OS: Windows 7
      Re: Batch file write variables to file
      « Reply #4 on: June 09, 2010, 08:39:44 PM »
      Quote
      When numbers 1 or 2 are associated with redirection > symbols

      1>  redirects standard out  2> redirects standard error

      "0>"  does not redirect anything and you confused the cmd interpreter
      and there was no output.

      OK, I understand.

      I want the file to contain "publicserver 0" with no space at the end. So how can I make it work?
      YouTube

      "Genius is persistence, not brain power." - Me

      "Insomnia is just a byproduct of, "It can't be done"" - LaVolpe

      gpl



        Apprentice
      • Thanked: 27
        Re: Batch file write variables to file
        « Reply #5 on: June 10, 2010, 12:35:23 AM »
        You could try changing around the command line to this

        Code: [Select]
        >> bla.cfg echo publicserver 0

        marvinengland



          Hopeful

          Thanked: 11
          Re: Batch file write variables to file
          « Reply #6 on: June 10, 2010, 03:11:45 AM »
          OK, I understand.

          I want the file to contain "publicserver 0" with no space at the end. So how can I make it work?

          maxclients 9
          serverbotlimit 12
          publicserver 0
          serverdesc "MyServer"

          There is not a space after "publicserver 0" but an invisible "\n" newline character. When the invisible newline character is removed the next line is placed on the same line as "publicserver 0"

          for example:

          maxclients 9
          serverbotlimit 12
          publicserver 0serverdesc "MyServer"

          Why would you want your output to look like the above?
           
          USA


          BC_Programmer


            Mastermind
          • Typing is no substitute for thinking.
          • Thanked: 1140
            • Yes
            • Yes
            • BC-Programming.com
          • Certifications: List
          • Computer: Specs
          • Experience: Beginner
          • OS: Windows 11
          Re: Batch file write variables to file
          « Reply #8 on: June 10, 2010, 12:01:22 PM »
          maxclients 9
          serverbotlimit 12
          publicserver 0
          serverdesc "MyServer"

          There is not a space after "publicserver 0" but an invisible "\n" newline character. When the invisible newline character is removed the next line is placed on the same line as "publicserver 0"

          for example:

          maxclients 9
          serverbotlimit 12
          publicserver 0serverdesc "MyServer"

          Why would you want your output to look like the above?
           

          your original solution:

          Code: [Select]
          echo publicserver 0 >> bla.cf

          puts a space between the 0 and the >>, therefore that space is being output to the file. it is that space that he does not want, but by removing the space:

          Code: [Select]
          echo publicserver 0>> bla.cf
          it becomes a stream number, so it is sent to the screen rather then the file, and the number is not part of the output as intended, either.

          The solution? escape the number:

          Code: [Select]
          echo publicserver ^0>>bla.cf
          this outputs "publicserver 0" to the file in question. (and of course the obligatory newline characters that have absolutely nothing to do with the problem)
          I was trying to dereference Null Pointers before it was cool.

          marvinengland



            Hopeful

            Thanked: 11
            Re: Batch file write variables to file
            « Reply #9 on: June 10, 2010, 01:40:30 PM »
            OK, I understand.

            I want the file to contain "publicserver 0" with no space at the end. So how can I make it work?

            The  escape (^) idea suggested by BC provides no output:
            echo publicserver ^0>>bla.cf


            C:\test>type  lin.bat
            Code: [Select]
            @echo off
            setlocal enabledelayedexpansion
            set /p yorn=Would you like to setup the server name etc.(y/n)?
            if /i %yorn%==y (
                    set /p in=Maximum clients:
                    echo maxclients !in! > bla.cfg
                    set /p in2=Bot limit:
                    echo serverbotlimit !in2! >> bla.cfg
                    echo 1 publicserver 0 >> bla.cfg
                    echo 2 publicserver ^0>>bla.cf
                    set /p in3=Server title:
                    echo serverdesc "!in3!" >> bla.cfg
                    echo Type bla.cfg
                    type bla.cfg
            ) else (
                    echo said no.
            )

            Output:

            C:\test>lin.bat
            Would you like to setup the server name etc.(y/n)?y
            Maximum clients:9
            Bot limit:12
            Server title:BCServer
            Type bla.cfg
            maxclients 9
            serverbotlimit 12
            1 publicserver 0
            serverdesc "BCServer"
            C:\test>
            USA

            BC_Programmer


              Mastermind
            • Typing is no substitute for thinking.
            • Thanked: 1140
              • Yes
              • Yes
              • BC-Programming.com
            • Certifications: List
            • Computer: Specs
            • Experience: Beginner
            • OS: Windows 11
            Re: Batch file write variables to file
            « Reply #10 on: June 10, 2010, 01:50:02 PM »
            The  escape (^) idea suggested by BC provides no output:

            Yes it does. In the script you posted, which, for posterity:

            Code: [Select]
            @echo off
            setlocal enabledelayedexpansion
            set /p yorn=Would you like to setup the server name etc.(y/n)?
            if /i %yorn%==y (
                    set /p in=Maximum clients:
                    echo maxclients !in! > bla.cfg
                    set /p in2=Bot limit:
                    echo serverbotlimit !in2! >> bla.cfg
                    echo 1 publicserver 0 >> bla.cfg
                    echo 2 publicserver ^0>>bla.cf
                    set /p in3=Server title:
                    echo serverdesc "!in3!" >> bla.cfg
                    echo Type bla.cfg
                    type bla.cfg
            ) else (
                    echo said no.
            )

            you'll note, after looking very carefully, that, completely by mistake, (I'm sure) the exact line used to "test" the escape method just happens to misspell the output filename as bla.cf.

            Additionally, note how thereafter this "bla.cf" filename is not referenced. you do type out the contents of bla.cfg which, not surprisingly, doesn't contain the output that was redirected to bla.cf.

            however, if you were to look in bla.cf, it would contain:

            Code: [Select]
            2 publicserver 0

            without a space after the 0, just as the OP is asking for.





            I was trying to dereference Null Pointers before it was cool.

            marvinengland



              Hopeful

              Thanked: 11
              Re: Batch file write variables to file
              « Reply #11 on: June 10, 2010, 03:04:58 PM »

              I want the file to contain "publicserver 0" with no space at the end. So how can I make it work?

              Your space was removed.


              C:\test>type  lin.bat
              Code: [Select]
              @echo off
              setlocal enabledelayedexpansion
              set /p yorn=Would you like to setup the server name etc.(y/n)?
              if /i %yorn%==y (
                      set /p in=Maximum clients:
                      echo maxclients !in! > bla.cfg
                      set /p in2=Bot limit:
                      echo serverbotlimit !in2! >> bla.cfg
                      echo publicserver 0 >> bla.cfg
                      echo publicserver 0 > bla1.txt
                      echo wc -c counts characters in line
                      wc -c bla1.txt
                      pause
                      echo publicserver ^0>>bla.cfg
                      echo publicserver ^0>bla2.txt
                      echo wc -c counts characters in line
                      wc -c bla2.txt
                      pause
                      set /p in3=Server title:
                      echo serverdesc "!in3!" >> bla.cfg
                      echo Type bla.cfg
                      type bla.cfg
              ) else (
                      echo said no.
              )

              Output:

              C:\test>lin.bat
              Would you like to setup the server name etc.(y/n)?y
              Maximum clients:9
              Bot limit:12
              wc -c counts characters in line
                    16   bla1.txt
              Press any key to continue . . .
              wc -c counts characters in line
                    15   bla2.txt
              Press any key to continue . . .
              Server title:BCServer
              Type bla.cfg
              maxclients 9
              serverbotlimit 12
              publicserver 0
              publicserver 0
              serverdesc "BCServer"
              C:\test>
              USA

              Salmon Trout

              • Guest
              Re: Batch file write variables to file
              « Reply #12 on: June 10, 2010, 03:33:36 PM »
              suppose he ain't got wc.exe? And can't install it?

              marvinengland



                Hopeful

                Thanked: 11
                Re: Batch file write variables to file
                « Reply #13 on: June 10, 2010, 05:11:56 PM »

                I want the file to contain "publicserver 0" with no space at the end. So how can I make it work?

                USA

                Salmon Trout

                • Guest
                Re: Batch file write variables to file
                « Reply #14 on: June 11, 2010, 12:04:12 AM »
                answer my question

                marvinengland



                  Hopeful

                  Thanked: 11
                  Re: Batch file write variables to file
                  « Reply #15 on: June 11, 2010, 01:16:23 PM »
                  The problem is that it does not write the variables to the file. It only partially works when I remove the whole if statement. . I thought this was because I have a space before the >>, but when I remove the space it doesn't work at all. So can someone that understands this convoluted language help me figure this out?

                  There is no problem. A trailing space for a redirect ( >> )
                  does not cause a problem.  There is no need to remove
                  the trailing space. This  complete thread is nonsense.


                  USA

                  Salmon Trout

                  • Guest
                  Re: Batch file write variables to file
                  « Reply #16 on: June 11, 2010, 01:21:47 PM »
                  This complete thread is nonsense.

                  The parts that you contributed are. Why don't you leave? Please.

                  Linux711

                    Topic Starter


                    Mentor

                    Thanked: 59
                    • Yes
                    • Programming Blog
                  • Certifications: List
                  • Computer: Specs
                  • Experience: Experienced
                  • OS: Windows 7
                  Re: Batch file write variables to file
                  « Reply #17 on: June 11, 2010, 05:47:14 PM »
                  Quote
                  There is no problem. A trailing space for a redirect ( >> )
                  does not cause a problem.  There is no need to remove
                  the trailing space.

                  Don't you realize that there might be something other than human eyes reading this file.



                  @BC_Programmer

                  Thank you. The escape ^ works. I adjusted the code a little. Removed the !in! !in2! !in3! and just made everything !in! because there is no point in using a new variable each time. I also fixed the file name issue.

                  Here is the final code. It works perfectly. No need for any more arguments.

                  Code: [Select]
                  @echo off
                  setlocal enabledelayedexpansion
                  set /p yorn=Would you like to setup the server name etc.(y/n)?
                  if /i %yorn%==y (
                          set /p in=Maximum clients:
                          echo maxclients !in!> bla.cfg
                          set /p in=Bot limit:
                          echo serverbotlimit !in!>> bla.cfg
                          echo publicserver ^0>>bla.cfg
                          set /p in=Server title:
                          echo serverdesc "!in!">> bla.cfg
                  ) else (
                          echo said no.
                  )

                  PS: Does anyone know how to mark resolved on this forum?
                  YouTube

                  "Genius is persistence, not brain power." - Me

                  "Insomnia is just a byproduct of, "It can't be done"" - LaVolpe

                  Helpmeh



                    Guru

                  • Roar.
                  • Thanked: 123
                    • Yes
                    • Yes
                  • Computer: Specs
                  • Experience: Familiar
                  • OS: Windows 8
                  Re: Batch file write variables to file
                  « Reply #18 on: June 11, 2010, 06:36:02 PM »
                  A trailing space for a redirect ( >> )
                  does not cause a problem.  There is no need to remove
                  the trailing space.
                  @echo off
                  echo Comparing requires no trailing spaces > file.txt
                  set /p var=<file.txt
                  if not "Comparing requires no trailing spaces"=="%var%" echo FAIL!
                  pause

                  It is also affects FTP scripts, too. Just so you know.
                  Where's MagicSpeed?
                  Quote from: 'matt'
                  He's playing a game called IRL. Great graphics, *censored* gameplay.