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

Author Topic: Batch file to split a string into multiple strings  (Read 16249 times)

0 Members and 1 Guest are viewing this topic.

flytothemoon

    Topic Starter


    Rookie

    Batch file to split a string into multiple strings
    « on: June 10, 2010, 03:22:30 PM »
    Hi. I need a script that will take a string and split it up by " %% " with no quotes. It would be very easy if you could output them on different lines (displayed).

    Thank you in advance.

    Salmon Trout

    • Guest
    Re: Batch file to split a string into multiple strings
    « Reply #1 on: June 10, 2010, 03:32:46 PM »
    Please explain a bit more what you want, and preferably give an example of input and desired output.

    flytothemoon

      Topic Starter


      Rookie

      Re: Batch file to split a string into multiple strings
      « Reply #2 on: June 10, 2010, 03:51:43 PM »
      I want so that when I input "Hello %% Person %%"(no quotes) , it would give me the following:
      Hello
      Person

      Salmon Trout

      • Guest
      Re: Batch file to split a string into multiple strings
      « Reply #3 on: June 11, 2010, 11:32:28 AM »
      Will there always be 2 names?

      flytothemoon

        Topic Starter


        Rookie

        Re: Batch file to split a string into multiple strings
        « Reply #4 on: June 11, 2010, 12:25:36 PM »
        No. There is no way to tell.

        Salmon Trout

        • Guest
        Re: Batch file to split a string into multiple strings
        « Reply #5 on: June 11, 2010, 12:28:02 PM »
        Is it possible to use VBScript?

        flytothemoon

          Topic Starter


          Rookie

          Re: Batch file to split a string into multiple strings
          « Reply #6 on: June 11, 2010, 12:31:44 PM »
          I would prefer to have them as one file, but it is ok to use it.

          Also, the input needs to come from the batch file.
          « Last Edit: June 11, 2010, 12:41:58 PM by flytothemoon »

          Salmon Trout

          • Guest
          Re: Batch file to split a string into multiple strings
          « Reply #7 on: June 11, 2010, 01:01:22 PM »
          Batch script that creates and runs VBscript in same folder

          Code: [Select]

          @echo off
          echo MyString=wscript.arguments(0)>splitter.vbs
          echo MyArray = Split(MyString, "%%%%", -1, 1)>>splitter.vbs
          echo last=UBound(MyArray)>>splitter.vbs
          echo For j = 0 To last>>splitter.vbs
          echo wscript.echo MyArray(j)>>splitter.vbs
          echo Next>>splitter.vbs

          REM Testing example string
          REM note that a % sign is a control character in batch so needs to be escaped
          REM by another % sign (i.e. doubled)

          set Longstring=Cat%%%%Dog%%%%Horse%%%%Bird%%%%Fish%%%%Chicken

          for /f "delims=" %%A in ( 'cscript //nologo splitter.vbs "%Longstring%" ' ) do echo %%A


          REM at end of batch (if desired)
          del splitter.vbs

          flytothemoon

            Topic Starter


            Rookie

            Re: Batch file to split a string into multiple strings
            « Reply #8 on: June 11, 2010, 01:41:07 PM »
            Thank you, but it just says "Echo is off".

            Salmon Trout

            • Guest
            Re: Batch file to split a string into multiple strings
            « Reply #9 on: June 11, 2010, 02:10:43 PM »
            Thank you, but it just says "Echo is off".

            I ran this batch file...

            Code: [Select]
            @echo off
            echo MyString=wscript.arguments(0)>splitter.vbs
            echo MyArray = Split(MyString, "%%%%", -1, 1)>>splitter.vbs
            echo last=UBound(MyArray)>>splitter.vbs
            echo For j = 0 To last>>splitter.vbs
            echo wscript.echo MyArray(j)>>splitter.vbs
            echo Next>>splitter.vbs
            set Longstring=Cat%%%%Dog%%%%Horse%%%%Bird%%%%Fish%%%%Chicken
            echo Longstring=%Longstring%
            for /f "delims=" %%A in ( 'cscript //nologo splitter.vbs "%Longstring%" ' ) do echo %%A
            del splitter.vbs

            and this was the output...

            Code: [Select]
            Longstring=Cat%%Dog%%Horse%%Bird%%Fish%%Chicken
            Cat
            Dog
            Horse
            Bird
            Fish
            Chicken

            If you will show me the code you have used I will try to advise. Where are you getting the long string from?



            flytothemoon

              Topic Starter


              Rookie

              Re: Batch file to split a string into multiple strings
              « Reply #10 on: June 11, 2010, 02:22:11 PM »
              It changed on me. Now it is working. But one problem. I am getting the longstring from a file that I know exists (I entered the path in windows explorer) but it says the file is not found. Any ideas?

              Salmon Trout

              • Guest
              Re: Batch file to split a string into multiple strings
              « Reply #11 on: June 11, 2010, 02:26:01 PM »
              Any ideas?

              Be thorough and careful and check what you are doing.