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

0 Members and 1 Guest are viewing this topic.

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 #45 on: February 02, 2016, 10:55:27 PM »
On last time. Don't put multiple;e lines into a variable.
It is forbidden, not allowed, senseless, worthless and futile.

It is like trying to brush your teeth with  hand soap.

Stop with the lines in a variable .
Tell us what your really need to do.

Putting lines into a variable is not what real men do. do.
Go read a book. Or get a tutor.


zask



    Intermediate

    • Experience: Experienced
    • OS: Other
    Re: Set a multiline variable in batch
    « Reply #46 on: February 02, 2016, 10:59:39 PM »
    On last time. Don't put multiple;e lines into a variable.
    It is forbidden, not allowed, senseless, worthless and futile.

    It is like trying to brush your teeth with  hand soap.

    Stop with the lines in a variable and we will tell you how to do what your want to do/.
    Putting lines into a variable is not what real men do. do.
    Go read a book. Or get a tutor.

    Well im not the one who asked for this, and i didn't really put multiple lines in a variable, just called the "echo." command and turned it into a variable. and added after some regular code, just trying to help. im not a big fan of the whole multi lined variable thing myself. kinda is nonsense really. im sure in some very rare case it could be useful but still it's not like it's the most biggest breakthrough in computer programming ever, and even it did have a purpose, what are the odds and why would it even be that important? multi lined variables are for people who are to lazy to either copy and paste the code or edit it them self's. so i'm with you on this one.

    almost 5 pages too, patio, i think your a wizard.
    « Last Edit: February 02, 2016, 11:12:56 PM by zask »

    camerongray



      Expert
    • Thanked: 306
      • Yes
      • Cameron Gray - The Random Rambings of a Computer Geek
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Mac OS
    Re: Set a multiline variable in batch
    « Reply #47 on: February 03, 2016, 03:46:50 AM »
    On last time. Don't put multiple;e lines into a variable.
    It is forbidden, not allowed, senseless, worthless and futile.

    It is like trying to brush your teeth with  hand soap.

    Stop with the lines in a variable .
    Tell us what your really need to do.

    Putting lines into a variable is not what real men do. do.
    Go read a book. Or get a tutor.
    Wait, what?  I'm not a batch developer but I know that in literally any language I've seen it's totally normal to have multiple lines in a variable, all a new line is is a special character.  Your forum post has multiple lines in it and I'd hazard a guess that at some point in the rendering of this page the body of your post is loaded into a single variable in the backend of this forum.

    multi lined variables are for people who are to lazy to either copy and paste the code or edit it them self's. so i'm with you on this one.
    They are for people who need to handle strings that contain multiple lines.  In the initial question for example they wanted to replace a single line in the input with multiple lines, this is totally standard practice in many programs.  Being lazy doesn't come into it at all, besides, in fact, copying and pasting code is generally seen as the lazy way to do things - https://en.wikipedia.org/wiki/Copy_and_paste_programming

    foxidrive



      Specialist
    • Thanked: 268
    • Experience: Experienced
    • OS: Windows 8
    Re: Set a multiline variable in batch
    « Reply #48 on: February 03, 2016, 04:26:22 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 (
       echo Processing "%%a"
       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.tmp"
    move /y "%dst%\%%a.tmp" "%dst%\%%a" >nul
    )
    echo done
    pause

    A file should now be created for each VRT file.  The redirection to the file was at the wrong level of parentheses and the move command is necessary again.

    Test it on sample files first.

    foxidrive



      Specialist
    • Thanked: 268
    • Experience: Experienced
    • OS: Windows 8
    Re: Set a multiline variable in batch
    « Reply #49 on: February 03, 2016, 04:33:39 AM »
    Wait, what?  I'm not a batch developer but I know that in literally any language I've seen it's totally normal to have multiple lines in a variable

    It's not normal in batch scripts though. :)

    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 #50 on: February 03, 2016, 09:34:15 AM »
    It's not normal in batch scripts though. :)
    Right. Here is the fundamental way the command interpreter works:
    command p1 p2 p3 ...
    Batch script is for a shell tool, console e utility. One line is a full expression. Interactive work  from the a console would not  not normally expect multiple Lines.
    Also, spaces are the default delimiter.
    A batch file is not a word processor, not a database or a universal toolkit.
    And yes, one could brush his teeth with hand soap. But why would he?
    I am outhitting the  thread. Really.  I am rolling my eyes.  ::)
    « Last Edit: February 03, 2016, 10:25:38 AM by Geek-9pm »

    zask



      Intermediate

      • Experience: Experienced
      • OS: Other
      Re: Set a multiline variable in batch
      « Reply #51 on: February 03, 2016, 07:05:24 PM »
      Wait, what?  I'm not a batch developer but I know that in literally any language I've seen it's totally normal to have multiple lines in a variable, all a new line is is a special character.  Your forum post has multiple lines in it and I'd hazard a guess that at some point in the rendering of this page the body of your post is loaded into a single variable in the backend of this forum.
      They are for people who need to handle strings that contain multiple lines.  In the initial question for example they wanted to replace a single line in the input with multiple lines, this is totally standard practice in many programs.  Being lazy doesn't come into it at all, besides, in fact, copying and pasting code is generally seen as the lazy way to do things - https://en.wikipedia.org/wiki/Copy_and_paste_programming

      Yeah, but i think it's different in batch, mainly because it was never really intended for multi lined variables, when i was talking about copy and pasting the code i was referring to pasting a code that he had already made into another file and edit it himself, instead of putting in a variable. Yeah there is a lot of languages that can do it, but it's not a good practice in batch, and there are a lot of reasons why its not such a good idea. One good reason why  is the example that Geek-9pm posted above, luckily the method i posted doesn't actually create a multi lined, but mimics it's technique with single lines instead. But still, in batch i hardly find a point for this . it does nothing but make your code even more difficult to comprehend / more difficult add edits for later use / and just plane up wastes bites in your file. if you wanted to use the command for later use just copy and paste it again, what ever you wanted to change in the text, just change it with regular variables. If you needed to completely store multiple lines of code, just send it to a file and call it inside the batch file. Notice now Geek-9pm states that "it's like brushing your teeth with soap"...? It is possible but there just isn't any point because it makes things more difficult. now more powerful languages like C++ is fine, but batch can only go so far.
      « Last Edit: February 03, 2016, 07:31:22 PM by zask »

      _unknown_

        Topic Starter


        Beginner

        • Experience: Beginner
        • OS: Windows 7
        Re: Set a multiline variable in batch
        « Reply #52 on: February 04, 2016, 06:50:32 PM »
        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 (
           echo Processing "%%a"
           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.tmp"
        move /y "%dst%\%%a.tmp" "%dst%\%%a" >nul
        )
        echo done
        pause

        A file should now be created for each VRT file.  The redirection to the file was at the wrong level of parentheses and the move command is necessary again.

        Test it on sample files first.

        Thanks!