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

Author Topic: Sring handling problem  (Read 3512 times)

0 Members and 1 Guest are viewing this topic.

Frank

    Topic Starter


    Intermediate

    Thanked: 3
    Sring handling problem
    « on: December 17, 2014, 04:42:30 PM »
    Hello,
    I have a file with lines of text such as:
    "C:\Program Files\something\app.exe" int.ini

    I have been able to extract each line and place the text of each line into a variable.
    Now comes my problem. I would like to split the two 'words' of text "C:\Program Files\something\app.exe" and int.ini into separate variables.
    I've looked on the internet and found solutions that don't work. an example is:

     for /f "tokens=1,2 delims=;" %%a in ("%name%") do (
       set first=%%a&set second=%%b
       echo %first% and %second%
     )
    where %name% contains my string with a ';' delimiter instead of a space.
    This doesn't seem to work.
    Could someone offer an alternative with a space for the delimiter that does work.
    Thanking you

    Lemonilla



      Apprentice

    • "Too sweet"
    • Thanked: 70
    • Computer: Specs
    • Experience: Experienced
    • OS: Windows 7
    Re: Sring handling problem
    « Reply #1 on: December 17, 2014, 05:00:45 PM »
    One of the issues is that there is no ; in the string you provided.  the other is that delayed expansion is not on, so the %first% and %second% will be processed before they are assigned a value. You may have to manually replace the separation with a (;).  If you do this should work.

    Code: [Select]
    setlocal EnableDelayedExpansion
    for /f "tokens=1,2 delims=;" %%a in ("%name%") do (
       set first=%%a
       set second=%%b
       echo !first! and !second!
    )
    Quote from: patio
    God Bless the DOS Helpers...
    Quote
    If it compiles, send the files.

    Squashman



      Specialist
    • Thanked: 134
    • Experience: Experienced
    • OS: Other
    Re: Sring handling problem
    « Reply #2 on: December 17, 2014, 05:09:43 PM »
    You can't split on a space because your file path has spaces. So you need to use a double quote as your delimiter.
    Code: [Select]
    @echo off

    for /f usebackq^ tokens^=1^,2^ delims^=^" %%a in ("file.txt") do (
    set first=%%a
    set second=%%b
    )
    echo %first%
    echo %second%
    pause
    output
    Code: [Select]
    C:\JFW\batch files\quote_delim>type file.txt
    "C:\Program Files\something\app.exe" int.ini

    C:\JFW\batch files\quote_delim>quote.bat
    C:\Program Files\something\app.exe
     int.ini
    Press any key to continue . . .

    dbenham



      Greenhorn

      Thanked: 3
      • Experience: Expert
      • OS: Windows 7
      Re: Sring handling problem
      « Reply #3 on: December 17, 2014, 05:15:07 PM »
      Beat me to it by a matter of seconds  :)

      You might want to strip leading spaces. One way is to use another FOR /F
      Code: [Select]
      for /f usebackq^ tokens^=^ 1* %%A in ("file.txt") do for /f "tokens=*" %%C in ("%%B") do (
        set "first=%%A"
        set "second=%%B"
      )
      echo %first%
      echo %second%
      Dave Benham

      Squashman



        Specialist
      • Thanked: 134
      • Experience: Experienced
      • OS: Other
      Re: Sring handling problem
      « Reply #4 on: December 17, 2014, 05:24:46 PM »
      Beat me to it by a matter of seconds  :)
        :P
      Gotta be quick over here Dave. Thanks for addition.  I saw the space in the 2nd token but figured they could figure out how to strip it.

      Squashman



        Specialist
      • Thanked: 134
      • Experience: Experienced
      • OS: Other
      Re: Sring handling problem
      « Reply #5 on: December 17, 2014, 05:26:33 PM »
      Shouldn't that be second=%%C.
      Code: [Select]
      @echo off
      for /f usebackq^ tokens^=1^*^ delims^=^" %%A in ("file.txt") do for /f "tokens=*" %%C in ("%%B") do (
        set "first=%%A"
        set "second=%%C"
      )
      echo %first%
      echo %second%
      pause

      Frank

        Topic Starter


        Intermediate

        Thanked: 3
        Re: Sring handling problem
        « Reply #6 on: December 17, 2014, 05:45:01 PM »
        Lemonilla, thank you for fixing my example. I would like to use a space for the delimiter. Unfortunately, this may be too difficult.

        Squashman, Thank you for offering another way. That is, going back one step and extracting the text form the file and splitting the text in one go. However, I notice that it only works if there is a space in the first path. There may be, in my case, where there is no space or more than one space in the first path and the script fails. As mentioned above, I might need to change my delimiter.


        Squashman



          Specialist
        • Thanked: 134
        • Experience: Experienced
        • OS: Other
        Re: Sring handling problem
        « Reply #7 on: December 17, 2014, 07:04:29 PM »
        I am not understanding you. It works with the example you provided. If it is not working then provide a non obfuscated example of your data that it is not working with.

        Frank

          Topic Starter


          Intermediate

          Thanked: 3
          Re: Sring handling problem
          « Reply #8 on: December 17, 2014, 07:39:22 PM »
          OK, yes it does work with the example I supplied. But, as I said if there are more or less spaces in the path it fails.
          I intended to enter more than one line in the file.txt file and process each line in turn.
          A couple of real examples:

          D:\MyProgs\DriveGleam\drivegleam.exe /startup
          "C:\Program Files (x86)\DropMyRights\DropMyRights.exe" "C:\Program Files (x86)\Mozilla Thunderbird\thunderbird.exe"

          Just to show a couple.

          Squashman



            Specialist
          • Thanked: 134
          • Experience: Experienced
          • OS: Other
          Re: Sring handling problem
          « Reply #9 on: December 17, 2014, 08:13:10 PM »
          Shouldn't have any problems with spaces as the code is using the double quote as the delimiter. But, you need to be consistent with your input. One example you just showed has no quotes.

          Frank

            Topic Starter


            Intermediate

            Thanked: 3
            Re: Sring handling problem
            « Reply #10 on: December 17, 2014, 08:34:24 PM »
            Thank you. I thought that script was using 2 spaces as the delimiter. Goes to show how well I understand batch scripting.
            I'll use double quotes.

            Squashman



              Specialist
            • Thanked: 134
            • Experience: Experienced
            • OS: Other
            Re: Sring handling problem
            « Reply #11 on: December 17, 2014, 08:44:06 PM »
            Thank you. I thought that script was using 2 spaces as the delimiter. Goes to show how well I understand batch scripting.
            I'll use double quotes.
            I did specifically say in my first post I was using a double quote as the delimiter.

            foxidrive



              Specialist
            • Thanked: 268
            • Experience: Experienced
            • OS: Windows 8
            Re: Sring handling problem
            « Reply #12 on: December 17, 2014, 11:26:04 PM »
            Thank you. I thought that script was using 2 spaces as the delimiter.
            I'll use double quotes.

            if you can control the delimiter and are using filename\paths then using a pipe character is a good choice as a delimiter. 
            or a * or ? or < or > as none of them can be in a path\filename.

            Quotes are a poison character in batch scripts in many ways.


            Squashman



              Specialist
            • Thanked: 134
            • Experience: Experienced
            • OS: Other
            Re: Sring handling problem
            « Reply #13 on: December 18, 2014, 07:43:54 AM »
            if you can control the delimiter and are using filename\paths then using a pipe character is a good choice as a delimiter. 
            I am in 100% agreement with you on that. It works flawlessly for us where I work.  I do data automation for a living and we have all of our customers send in their data PIPE delimited.