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

Author Topic: Set a multiline variable in batch  (Read 47012 times)

0 Members and 1 Guest are viewing this topic.

zask



    Intermediate

    • Experience: Experienced
    • OS: Other
    Re: Set a multiline variable in batch
    « Reply #30 on: February 01, 2016, 05:24:36 PM »
    Is this what your asking for?

    @echo off
    set nl=^& echo.
    echo %nl%Multiple%nl%lines%nl%variable%nl%

    or maybe this?

    @echo off
    setlocal EnableDelayedExpansion
    (set nl=^
    %=DONT_CHANGE_THIS_LINE=%
    )

    echo !nl!multiple!nl!lines!nl!in!nl!a!nl!variable!nl!

    its not really in one variable but it should solve your problem.

    You can even add some style to your code if you really wanted to.

    @echo off
    set nl=^& echo.
    echo  ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» %nl% º                              º %nl% º   A better example of this!  º %nl% º    Hopefully This helped?    º %nl% º                              º %nl% ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ %nl%               
    pause 

    rather than this creating a multi lined variable, it creates a variable that represents the new line it's self
    « Last Edit: February 01, 2016, 05:47:24 PM by zask »

    _unknown_

      Topic Starter


      Beginner

      • Experience: Beginner
      • OS: Windows 7
      Re: Set a multiline variable in batch
      « Reply #31 on: February 01, 2016, 07:26:02 PM »
      Is this what your asking for?

      @echo off
      set nl=^& echo.
      echo %nl%Multiple%nl%lines%nl%variable%nl%

      or maybe this?

      @echo off
      setlocal EnableDelayedExpansion
      (set nl=^
      %=DONT_CHANGE_THIS_LINE=%
      )

      echo !nl!multiple!nl!lines!nl!in!nl!a!nl!variable!nl!

      its not really in one variable but it should solve your problem.

      You can even add some style to your code if you really wanted to.

      @echo off
      set nl=^& echo.
      echo  ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» %nl% º                              º %nl% º   A better example of this!  º %nl% º    Hopefully This helped?    º %nl% º                              º %nl% ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ %nl%               
      pause 

      rather than this creating a multi lined variable, it creates a variable that represents the new line it's self

      Thanks but foxidrive's code is working. But the code only works on single file and I forgot to include in my question that I have more file.vrt files.

      foxidrive



        Specialist
      • Thanked: 268
      • Experience: Experienced
      • OS: Windows 8
      Re: Set a multiline variable in batch
      « Reply #32 on: February 02, 2016, 09:57:49 AM »
      Code: [Select]
      @echo off
      set "src=C:\path\to\source\files"
      set "dst=C:\path\to\destination\files"
      pushd "%src%"
      (
      for /f "delims=" %%a in ('dir /b /a-d "*.vrt" ') do (
         set "stop="
            for /f "usebackq delims=" %%b in ("%%a") do (
               echo(%%b
               if not defined stop type "file2.vrt" &  echo(
               set stop=are-we-there-yet?
            )))>"%dst%\%%a"

      That's untested.

      Keep in mind that you make it harder for the volunteers by not saying what you really want to do, and they are spending their own free time to assist you.

      People asking for help with a scripting technique don't need to provide lots of detail, but people who need help writing a complete script do need to say what they are doing!

      i just had numerous files and forgot to include it.

      "forgot" is not what happened here. :D

      patio

      • Moderator


      • Genius
      • Maud' Dib
      • Thanked: 1769
        • Yes
      • Experience: Beginner
      • OS: Windows 7
      Re: Set a multiline variable in batch
      « Reply #33 on: February 02, 2016, 10:07:46 AM »
      I predict 5 pages...
      " Anyone who goes to a psychiatrist should have his head examined. "

      Geek-9pm


        Mastermind
      • Geek After Dark
      • Thanked: 1026
        • Gekk9pm bnlog
      • Certifications: List
      • Computer: Specs
      • Experience: Expert
      • OS: Windows 10
      Re: Set a multiline variable in batch
      « Reply #34 on: February 02, 2016, 12:12:16 PM »
      I predict 5 pages...
      At 3 and counting.   :-\

      patio

      • Moderator


      • Genius
      • Maud' Dib
      • Thanked: 1769
        • Yes
      • Experience: Beginner
      • OS: Windows 7
      Re: Set a multiline variable in batch
      « Reply #35 on: February 02, 2016, 12:26:31 PM »
      Thanx for your contribution...i had already noticed though.
      " Anyone who goes to a psychiatrist should have his head examined. "

      zask



        Intermediate

        • Experience: Experienced
        • OS: Other
        Re: Set a multiline variable in batch
        « Reply #36 on: February 02, 2016, 04:34:15 PM »
        Thanks but foxidrive's code is working. But the code only works on single file and I forgot to include in my question that I have more file.vrt files.

        so your basically trying to do this if i am correct, use the example below

        you have a variable name "a"

        and you basically want %a% to equal everything in specific lines?

        set a=
        line1
        line2
        line3

        so that %a% can reuse both lines 1,2, & 3 from just calling %a%?
        That's not a easy thing to do in batch files, i mean it's possible but it's a complete pain in
        doing.

        maybe you could cheat the code lol.

        command 1 >> "temp"
        command 2 >> "temp"
        command 3 >> "temp"
        set /p OUTPUT= < "temp"
        del "temp"
        echo %OUTPUT%

        Geek-9pm


          Mastermind
        • Geek After Dark
        • Thanked: 1026
          • Gekk9pm bnlog
        • Certifications: List
        • Computer: Specs
        • Experience: Expert
        • OS: Windows 10
        Re: Set a multiline variable in batch
        « Reply #37 on: February 02, 2016, 06:08:55 PM »
        It does not work.
        Zack, why post code that does not work.
        Rather than prove anything, it paints you as a novice.  ::)


        patio

        • Moderator


        • Genius
        • Maud' Dib
        • Thanked: 1769
          • Yes
        • Experience: Beginner
        • OS: Windows 7
        Re: Set a multiline variable in batch
        « Reply #38 on: February 02, 2016, 06:47:23 PM »
         We've known that for quite awhile...
        " Anyone who goes to a psychiatrist should have his head examined. "

        zask



          Intermediate

          • Experience: Experienced
          • OS: Other
          Re: Set a multiline variable in batch
          « Reply #39 on: February 02, 2016, 07:26:10 PM »
          It does not work.
          Zack, why post code that does not work.
          Rather than prove anything, it paints you as a novice.  ::)

          My bad long day, i advised a better solution, the file will turn each individual line into its own variable, which can be recalled throughout multiple lines. also i was hoping that people would know not actualy to type command 1,2, or 3. it was just an example. hope this helps now that my brain is working correctly lol.


          you could select from a specific line from your "file.txt" or whatever you need, just be sure to replace it's file name with a different file name that has the code you need to use.

          @echo off
          setlocal EnableExtensions EnableDelayedExpansion
          for /f "delims=" %%a in (file.txt) do (
          set /a c+=1
          set x[!c!]=%%a
          )
          set x
          pause

          what ever appears in the prompt, add percentage signs around it.

          for example; if you had a line that said "echo hello" inside "file.txt" and you wanted to find that specific line and turn it into a variable, it would display this.

          x[1]=echo hello

          so in your original batch file you would turn it into %x[1]% and it will display "echo hello" from line x[1] inside of "file.txt". if you wanted it to store more lines to make your variable larger you could do so as you wish. you can also put your %x[1]% variable inside the second variable (%[x2]%) if you wanted a single variable with multiple lines. then use my previous command "set nl=^& echo." inside of it as well to avoid two lines of code to appear on the same line. thus resulting in one multi lined variable.

          working on an example so give me a minute
          « Last Edit: February 02, 2016, 08:12:33 PM by zask »

          zask



            Intermediate

            • Experience: Experienced
            • OS: Other
            Re: Set a multiline variable in batch
            « Reply #40 on: February 02, 2016, 07:31:36 PM »
            :/ zzZ so tired

            zask



              Intermediate

              • Experience: Experienced
              • OS: Other
              Re: Set a multiline variable in batch
              « Reply #41 on: February 02, 2016, 09:21:57 PM »
              @echo off
              setlocal EnableExtensions EnableDelayedExpansion
              rem represents the new line
              (set nl=^
              %=DONT_CHANGE_THIS_LINE=%
              )

              echo hello >> file.txt
              echo. >> file.txt
              echo friends >> file.txt

              echo created file.txt with the words "hello" and "friends" inside of it
              pause

              rem turns all lines into a variable
              for /f "delims=" %%a in (file.txt) do (
              set /a c+=1
              set x[!c!]=%%a
              )
              set x
              pause

              echo your variables are
              echo %x[1]%
              echo %x[3]%

              pause

              echo your final multi lined variable

              echo %x[1]%!nl!%x[3]%

              echo well sorta lol.
              « Last Edit: February 02, 2016, 10:08:20 PM by zask »

              Geek-9pm


                Mastermind
              • Geek After Dark
              • Thanked: 1026
                • Gekk9pm bnlog
              • Certifications: List
              • Computer: Specs
              • Experience: Expert
              • OS: Windows 10
              Re: Set a multiline variable in batch
              « Reply #42 on: February 02, 2016, 09:58:15 PM »
              Quote
              echo well sorta lol.
              Sorta is only valid playing horseshoes with my grand daughter.

              zask



                Intermediate

                • Experience: Experienced
                • OS: Other
                Re: Set a multiline variable in batch
                « Reply #43 on: February 02, 2016, 10:09:47 PM »
                Sorta is only valid playing horseshoes with my grand daughter.

                Sorta doesn't imply that the code doesn't work, implies rather that the variable are beside each other than combined into one (not a big deal really). it just a work around. from what ive heard is that he/she wants a variable that contains multiple lines of code, and when he/she runs that variable it runs all the code inside of it just as any other variable with one line would do. it doesn't imply that the code above doesn't work as it does just fine with variables set beside each other, and if you wanted to honestly you could just put the contents of the variable inside the other variable?

                I was implying that it may not be a multi lined variable because its not just one variable, but does just the same so.... idk try it, it should work, that is if this even is what Unknown is asking for. :/ it's still unclear what unknown is even asking for, it would be helpful if he/she would post his/her code. think none of us here are actually going to go through the pain to make a real multi lined variable in batch with hardly any explanation of what it's for, but a work around seems more logical, still think he should explain better so we can find a solution 
                « Last Edit: February 02, 2016, 10:34:03 PM by zask »

                zask



                  Intermediate

                  • Experience: Experienced
                  • OS: Other
                  Re: Set a multiline variable in batch
                  « Reply #44 on: February 02, 2016, 10:38:52 PM »
                  At 3 and counting.   :-\

                  yeah, i hope it doesn't go that long  :-\