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 47011 times)

0 Members and 1 Guest are viewing this topic.

_unknown_

    Topic Starter


    Beginner

    • Experience: Beginner
    • OS: Windows 7
    Set a multiline variable in batch
    « on: January 25, 2016, 07:30:38 PM »
    Tried to use this script from here http://stackoverflow.com/questions/23075953/batch-script-to-find-and-replace-a-string-in-text-file-without-creating-an-extra. When I tried to assign a one long line text to the variable replace, the script run successfully. But when I tried to set a multi line to the variable replace and run the script and it always says The syntax of the command is incorrect. Any help?  :( Thanks!

    Code: [Select]
    @echo off
    set "search=<one>asdfghjklzxcvbnmqwertyuiop</one>
    set "replace=   <roses are ="RED">
         <violets are="blue">1</violets> #this should be in a new line
         <sugar is="sweet">1</sugar>" #this should be in a new line

    set "textFile=A2015336045000.L2_LAC.SeAHABS_chlora.vrt"

        for /f "delims=" %%i in ('type "%textFile%" ^& break ^> "%textFile%" ') do (
            set "line=%%i"
            setlocal enabledelayedexpansion
            set "line=!line:%search%=%replace%!"
            >>"%textFile%" echo(!line!
            endlocal
    )
    « Last Edit: January 25, 2016, 08:24:02 PM by _unknown_ »

    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 #1 on: January 25, 2016, 08:11:03 PM »
    Why are you using the <and > things?
    These are redirection symbols.
    Also, it is not a good idea to embed the new line thing into something. If you have something that takes three lines, just use three variables for three lines.

    Is there a strong reason to put new line into a message? I can't imagine why you would need it.

    _unknown_

      Topic Starter


      Beginner

      • Experience: Beginner
      • OS: Windows 7
      Re: Set a multiline variable in batch
      « Reply #2 on: January 25, 2016, 08:23:16 PM »
      Why are you using the <and > things?

      My strings contains these symbol.

      Quote
      Is there a strong reason to put new line into a message? I can't imagine why you would need it.

      Since I have multiple lines, I don't have any idea on how to put the rest of the lines into a new line.

      _unknown_

        Topic Starter


        Beginner

        • Experience: Beginner
        • OS: Windows 7
        Re: Set a multiline variable in batch
        « Reply #3 on: January 25, 2016, 10:09:33 PM »
        Tried using three variables for the three lines. But the output displays them in only one line.

        foxidrive



          Specialist
        • Thanked: 268
        • Experience: Experienced
        • OS: Windows 8
        Re: Set a multiline variable in batch
        « Reply #4 on: January 26, 2016, 08:14:25 AM »
        If you explain your task in English then you may get a solution.

        Batch files don't like multiple lines in an environment variable.

        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 #5 on: January 26, 2016, 08:52:31 AM »
        Thanks, Foxidrive.
        From what little I understand of his post, I did a short batch file to ilustrate what it looks like to me.
        Code: [Select]
        REM This is a very simple batch file
        REM It is named simple1.bat
        echo off
        set rose=Roses are Red,
        set viol=Violets are blue.
        set poem=If we ever wed,
        set when=Say I love lyou.
        echo %rose%
        echo %viol%
        echo %poem%
        echo %when%
        echo on
        REM Write to a TXT file
        echo off
        REM Frst arrow write new file
        REM two arrows add to file
        @echo %rose% > new.txt
        @echo %viol% >> new.txt
        echo %poem% >> new.txt
        echo %when% >> new.txt
        type new.txt
        pause
        REM All Done!
        Screen Shot attached.


        [attachment deleted by admin to conserve space]

        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: Set a multiline variable in batch
        « Reply #6 on: January 26, 2016, 09:04:57 AM »
        My strings contains these symbol.
        And they have special meaning to the command interpreter. > and < are, as Geek mentions, redirection symbols. You have to escape them if you want to use them, by prefixing them with a Caret. For example:

        Code: [Select]
        C:\>echo <
        The Syntax of the command is incorrect.
        C:\>echo ^<
        <
        Quote
        Since I have multiple lines, I don't have any idea on how to put the rest of the lines into a new line.
        The Command Interpreter doesn't support multi-line environment variables. There are ways to create them but they are rather unpleasant and tend to be more difficult to work with then changing the problem such that it doesn't require processing of multi-line environment variables.




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

        zask



          Intermediate

          • Experience: Experienced
          • OS: Other
          Re: Set a multiline variable in batch
          « Reply #7 on: January 26, 2016, 06:18:09 PM »
          Yes you have to cancel out the sign. here is a example

          @echo off

          echo This is a message inside the file text.txt >> text.txt
          echo @echo off
          echo echo Adding the carrot sign cancels the sign out ^>^> text.bat >> text.txt
          echo pause >> text.txt

          zask



            Intermediate

            • Experience: Experienced
            • OS: Other
            Re: Set a multiline variable in batch
            « Reply #8 on: January 26, 2016, 08:28:50 PM »
            Mybad blonde moment~ correction below

            @echo off
            echo Rem This is a message inside the file text.txt >> text.txt
            echo @echo off ^>^> text.bat >> text.txt
            echo echo Adding the carrot sign cancels the sign out ^>^> text.bat >> text.txt
            echo pause ^>^> text.bat >> text.txt


            foxidrive



              Specialist
            • Thanked: 268
            • Experience: Experienced
            • OS: Windows 8
            Re: Set a multiline variable in batch
            « Reply #9 on: January 27, 2016, 05:59:07 AM »
            From what little I understand of his post, I did a short batch file to ilustrate what it looks like to me.

            I guess I'm getting old and crotchety, but I ask simple questions for a couple of reasons.

            1) is to get a response of some kind - because why would you go to any effort when a poster can paste their question into 12 different forums, and they will not even see what you have written because it was answered somewhere else.

            2) to help avoid misunderstandings.  That just wastes your time when you assume they mean pie and they actually mean PI, if you follow that odd example.


            I didn't even read the fellows question all the way through to be honest.

            What I noticed was words that didn't describe an actual task - so I stopped reading - because of point 2 above.
            « Last Edit: January 27, 2016, 06:09:56 AM by foxidrive »

            _unknown_

              Topic Starter


              Beginner

              • Experience: Beginner
              • OS: Windows 7
              Re: Set a multiline variable in batch
              « Reply #10 on: January 28, 2016, 12:34:44 AM »
              If you explain your task in English then you may get a solution.

              Let's say I have a text file named input.txt. The input.txt contains 5 lines. What I need to do now is insert "multiple lines" on the 2nd line of the input.txt and then save it again with the same file name which is input.txt(overwrite). But the thing is the lines to insert in the input.txt is indented and enclosed with < >.

              input.txt

              Code: [Select]
              This is
              an
              example
              of
              input.txt


              The multi line should be inserted after the line This is.

              Multi line to insert

              Code: [Select]
                 <Insert="Line 1"> 
                   <Insert="Line 2">file1.vrt</Line2> 
                   <Insert="Line 2">1</Line2> 
                   <Insert="Line 3">file2.vrt</Line3> 
                   <Insert="Line 3">1</Line3> 
                   <Another line="Line 4">0</Line4>
                    rem And so on. . .   
                 </Insert>

              Squashman



                Specialist
              • Thanked: 134
              • Experience: Experienced
              • OS: Other
              Re: Set a multiline variable in batch
              « Reply #11 on: January 28, 2016, 11:34:23 AM »
              So you are trying to edit an XML file?

              _unknown_

                Topic Starter


                Beginner

                • Experience: Beginner
                • OS: Windows 7
                Re: Set a multiline variable in batch
                « Reply #12 on: January 28, 2016, 04:34:14 PM »
                So you are trying to edit an XML file?

                The extension name of the file are *.vrt. But it says in  here http://www.gdal.org/drv_vrt.html that it's based in an XML file. Maybe it's an xml file.

                patio

                • Moderator


                • Genius
                • Maud' Dib
                • Thanked: 1769
                  • Yes
                • Experience: Beginner
                • OS: Windows 7
                Re: Set a multiline variable in batch
                « Reply #13 on: January 28, 2016, 05:51:39 PM »
                Uummm...yea.
                " Anyone who goes to a psychiatrist should have his head examined. "

                foxidrive



                  Specialist
                • Thanked: 268
                • Experience: Experienced
                • OS: Windows 8
                Re: Set a multiline variable in batch
                « Reply #14 on: January 28, 2016, 11:18:42 PM »
                Let's say I have a text file named input.txt. The input.txt contains 5 lines. What I need to do now is insert "multiple lines" on the 2nd line of the input.txt and then save it again with the same file name which is input.txt(overwrite).

                Thanks, that's useful detail.

                But where does the multi-line data exist?  Is it inside a file?

                Do you want to put all the information that is inside one file, after line one of the other file?