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

Author Topic: Using "for" as a seperator  (Read 5168 times)

0 Members and 1 Guest are viewing this topic.

hibyy

    Topic Starter


    Rookie

    • Experience: Familiar
    • OS: Windows 8
    Using "for" as a seperator
    « on: December 26, 2012, 01:14:32 PM »
    I've been working on an batch script which will take a string and using a for command will separate that string into several other strings and so far this is the best script I got it to work with...

    Code: [Select]
    @echo off
    :a
    if "%ln%"=="" echo.
    if not "%ln%"=="" echo %ln%
    echo.
    set /p Line=
    cls
    if "%Line%"=="Done" goto b
    set /a TC=%TC%+1
    set T=%T%%TC%,
    set T%TC%=echo %%%TC%
    set Ln=%ln%%Line%-
    goto a
    :b
    echo.
    for /f "tokens=%T:~0,-1% delims=-" %%1 in ("%Ln%") do call :c
    echo.
    pause>nul
    exit
    :c
    set /a NC=%NC%+1
    call set Op=%%T%NC%%%
    for /f %%a in ("string") do %Op%
    if "%NC%"=="%TC%" exit /b
    goto c


    does anyone know a better way to do this or an alternative solution?

    Salmon Trout

    • Guest
    Re: Using "for" as a seperator
    « Reply #1 on: December 26, 2012, 02:04:04 PM »
    Could you try explaining, in words, what you are trying to do?

    hibyy

      Topic Starter


      Rookie

      • Experience: Familiar
      • OS: Windows 8
      Re: Using "for" as a seperator
      « Reply #2 on: December 26, 2012, 04:01:35 PM »
      Well I thought I did, but I'll show an example in words.

      Take this string...

      Hello-my name is...-some random text

      This will be split up for where ever there is a "-" a new line will start so it will become

      Hello
      my name is...
      some random text

      that is what the above batch script does I'm asking if there is another way to do that, or in other words an alternative way.

      EDIT: Also the code above uses the for command in order to separate the string into multiple strings hence the title of the topic. I'm sorry if this is confusing in anyway.
      « Last Edit: December 26, 2012, 04:12:08 PM by hibyy »

      foxidrive



        Specialist
      • Thanked: 268
      • Experience: Experienced
      • OS: Windows 8
      Re: Using "for" as a seperator
      « Reply #3 on: December 26, 2012, 07:51:55 PM »
      @echo off
      set /p "Line=Enter your text, with two - in it: "
      for /f "tokens=1,2,3 delims=-" %%a in ("%line%") do (
      echo %%a
      echo %%b
      echo %%c
      )
      pause

      Salmon Trout

      • Guest
      Re: Using "for" as a seperator
      « Reply #4 on: December 27, 2012, 12:25:15 AM »
      Arbitrary number of tokens

      @echo off

      REM Get a string to split
      set /p String="Enter a string "

      :Loop

      REM Split string into 1st token and the remainder
      for /f "tokens=1* delims=-" %%A in ("%String%") do (

         REM Echo 1st token
         echo %%A

         REM Assign remainder to %String%
         set String=%%B

         )

      REM If %String% is a blank then we are finished
      if "%String%"=="" goto Done

      REM Otherwise go round again
      goto Loop

      :Done


      Result:

      C:\>Split-hyphen.bat
      Enter a string 1-2-3-4-5-6-7-8-9
      1
      2
      3
      4
      5
      6
      7
      8
      9
      C:\>Split-hyphen.bat
      Enter a string cat-dog-horse-bird-tree
      cat
      dog
      horse
      bird
      tree
      C:\>Split-hyphen.bat
      Enter a string I ran - I fell - I got up again
      I ran
       I fell
       I got up again
      C:\>


      Coming soon: Hybrid batch/VBScript





      « Last Edit: December 27, 2012, 12:47:22 AM by Salmon Trout »

      Salmon Trout

      • Guest
      Re: Using "for" as a seperator
      « Reply #5 on: December 27, 2012, 12:56:33 AM »
      @echo off
      if exist SplitString.vbs del SplitString.vbs
      >> SplitString.vbs Echo StringToSplit = Wscript.Arguments(0)
      >> SplitString.vbs Echo   CharToSplit = Wscript.Arguments(1)
      >> SplitString.vbs Echo SplitArray = Split (StringToSplit, CharToSplit, -1, 1)
      >> SplitString.vbs Echo For j = 0 to Ubound (SplitArray)
      >> SplitString.vbs Echo    Wscript.Echo SplitArray (j)
      >> SplitString.vbs Echo Next
      set /p String="String to split?     "
      set /p SplitC="Char(s) to split on? "
      for /f "delims=" %%A in ('cscript //nologo SplitString.vbs "%String%" "%SplitC%"') do echo %%A



      C:\>VSplit-hyphen.bat
      String to split?     1-2-3-4-5-6-7-8-9
      Char(s) to split on? -
      1
      2
      3
      4
      5
      6
      7
      8
      9
      C:\>VSplit-hyphen.bat
      String to split?     cat-dog-horse-bird-tree
      Char(s) to split on? -
      cat
      dog
      horse
      bird
      tree
      C:\>VSplit-hyphen.bat
      String to split?     I ran - I fell - I got up again
      Char(s) to split on? -
      I ran
       I fell
       I got up again
      C:\>VSplit-hyphen.bat
      String to split?     apple++Pear++Orange
      Char(s) to split on? ++
      apple
      Pear
      Orange
      C:\>


      Note: VBSCript allows splitting on a string of multiple characters

      C:\>VSplit-hyphen.bat
      String to split?     I ran - I fell - I got up again
      Char(s) to split on? - <--- There is a space here
      I ran
      I fell
      I got up again








      foxidrive



        Specialist
      • Thanked: 268
      • Experience: Experienced
      • OS: Windows 8
      Re: Using "for" as a seperator
      « Reply #6 on: December 27, 2012, 01:01:14 AM »
      Nice work.

      Here's another simple method of displaying it to the console.

      Quote
      @echo off
      setlocal enabledelayedexpansion
      set "var=One, Two-Buckle my shoe.-Three Four,-Walk out the door."
      :: keep the blank line in below.
      (set LF=^

      )
      echo %var:-=!LF!%
      pause


      One, Two
      Buckle my shoe.
      Three Four,
      Walk out the door.

      Press any key to continue . . .

      hibyy

        Topic Starter


        Rookie

        • Experience: Familiar
        • OS: Windows 8
        Re: Using "for" as a seperator
        « Reply #7 on: December 27, 2012, 05:49:14 AM »
        Thank you all for the replies this has helped me shorten the script I was working on significantly. :)