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

Author Topic: Parsing string from file twice  (Read 8854 times)

0 Members and 1 Guest are viewing this topic.

Tigers!

    Topic Starter


    Rookie

    Parsing string from file twice
    « on: October 22, 2009, 06:33:44 PM »
    Gudday all
    I wish to read the lines of a file one at a time and store them as a variable. Then parse that same variable to extract a token to be stored as another variable.
    I can do the first part ok using the for command but not the 2nd. I have trying using for and findstr without success.
    Here is my code
    Code: [Select]
    @echo off
    cls

    for /f "delims=" %%a in (moveTIFF.dat) do (
     echo %%a
    )
    for /f "tokens=4 delims=\" %%b in ("%a%") do (
     echo %%b
    )
    The code runs ok but the 2nd for command has no output displayed. This means of course that either there is nothing to display or I cannot display the var %%b.
    Findstr is not really suitable as you need to know the output 1st and the output will be variable. Perhaps for can be used to parse a string but I don't know to do it if it be possible.
    Sample of input
    Code: [Select]
    \TestingTiffMoves\AU\PICKLIST\0000544B.TIF
    \TestingTiffMoves\AU\PICKLIST\0000544C.TIF
    \TestingTiffMoves\AU\PICKLIST\0000544D.TIF
    \TestingTiffMoves\AU\PICKLIST\0000544E.TIF
    \TestingTiffMoves\AU\PICKLIST\index.dat
    It is the TIF name that will always vary. The path is constant.
    All help gratefully accepted.

    Helpmeh



      Guru

    • Roar.
    • Thanked: 123
      • Yes
      • Yes
    • Computer: Specs
    • Experience: Familiar
    • OS: Windows 8
    Re: Parsing string from file twice
    « Reply #1 on: October 22, 2009, 06:36:43 PM »
    You should understand the following.

    If you didn't set a variable named a to %%a then a will be what it was previously. %%a doesn't magically become %a%. 
    Where's MagicSpeed?
    Quote from: 'matt'
    He's playing a game called IRL. Great graphics, *censored* gameplay.

    Tigers!

      Topic Starter


      Rookie

      Re: Parsing string from file twice
      « Reply #2 on: October 22, 2009, 06:41:49 PM »
      Helpmeh
      Thank you for the reply.
      1. Have I set up %%a properly so that it may be used later?
      2. Can I pass %%a (or its correct form) to for for the 2nd parsing?
      3. If for is not the best command to do my 2nd parse is there a better command?
      4. What is the difference between %%a and %a%? i am still learning and have much to go.

      gh0std0g74



        Apprentice

        Thanked: 37
        Re: Parsing string from file twice
        « Reply #3 on: October 22, 2009, 06:46:41 PM »
        you can definitely use for loop to parse your file and i will let the "batch" gurus show you how. I will show you easier to understand method...using vbscript (i assume you are not using prehistoric Windows)
        Code: [Select]
        Set objFS=CreateObject("Scripting.FileSystemObject")
        strFile = "c:\test\file"
        Set objFile = objFS.OpenTextFile(strFile)
        Do Until objFile.AtEndOfLine
        strLine = Split(objFile.ReadLine,"\") 'split the line into a array
        For i=0 To UBound(strLine)
        WScript.Echo "Token "&i&" "&strLine(i) 'print out each token
        Next
        strLine = Join(strLine,"/") 'join back using another delimiter
        WScript.Echo "Join back: " &strLine ' join back to become string
        Loop
        objFile.Close
        output
        Code: [Select]
        C:\test>cscript /nologo test.vbs
        Token 0
        Token 1 TestingTiffMoves
        Token 2 AU
        Token 3 PICKLIST
        Token 4 0000544B.TIF
        Join back: /TestingTiffMoves/AU/PICKLIST/0000544B.TIF
        Token 0
        Token 1 TestingTiffMoves
        Token 2 AU
        Token 3 PICKLIST
        Token 4 0000544C.TIF
        Join back: /TestingTiffMoves/AU/PICKLIST/0000544C.TIF
        Token 0
        Token 1 TestingTiffMoves
        Token 2 AU
        Token 3 PICKLIST
        Token 4 0000544D.TIF
        Join back: /TestingTiffMoves/AU/PICKLIST/0000544D.TIF
        Token 0
        Token 1 TestingTiffMoves
        Token 2 AU
        Token 3 PICKLIST
        Token 4 0000544E.TIF
        Join back: /TestingTiffMoves/AU/PICKLIST/0000544E.TIF
        Token 0
        Token 1 TestingTiffMoves
        Token 2 AU
        Token 3 PICKLIST
        Token 4 index.dat
        Join back: /TestingTiffMoves/AU/PICKLIST/index.dat

        if you want to learn more, see my sig

        Tigers!

          Topic Starter


          Rookie

          Re: Parsing string from file twice
          « Reply #4 on: October 22, 2009, 06:50:08 PM »
          you can definitely use for loop to parse your file and i will let the "batch" gurus show you how. I will show you easier to understand method...using vbscript (i assume you are not using prehistoric Windows)
          :-[ Its terribly being prehistoric.
          I have never used vb before.  :'(

          Hope one of the batch gurus shows up.

          gh0std0g74



            Apprentice

            Thanked: 37
            Re: Parsing string from file twice
            « Reply #5 on: October 22, 2009, 07:12:13 PM »
            :-[ Its terribly being prehistoric.
            how prehistoric?? window 3.1 and below?? vbscript can be used win9.x onwards

            Quote
            I have never used vb before.  :'(
            even before you knew batch, you have not learnt about it, but now you do. isn't that right?



            billrich

            • Guest
            Re: Parsing string from file twice
            « Reply #6 on: October 22, 2009, 08:50:03 PM »
            C:\>type tiff.bat
            Code: [Select]
            @echo off
            rem  cls

            for /f "delims=" %%a in (tiff.txt) do (
             echo %%a
            )
            for /f "tokens=4 delims=\" %%b in (tiff.txt) do (
             echo %%b
            )

            OUTPUT:

            C:\>tiff.bat
            \TestingTiffMoves\AU\PICKLIST\0000544B.TIF
            \TestingTiffMoves\AU\PICKLIST\0000544C.TIF
            \TestingTiffMoves\AU\PICKLIST\0000544D.TIF
            \TestingTiffMoves\AU\PICKLIST\0000544E.TIF
            \TestingTiffMoves\AU\PICKLIST\index.dat


            0000544B.TIF
            0000544C.TIF
            0000544D.TIF
            0000544E.TIF
            index.dat
            C:\>

            Input:


            C:\>type tiff.txt
            \TestingTiffMoves\AU\PICKLIST\0000544B.TIF
            \TestingTiffMoves\AU\PICKLIST\0000544C.TIF
            \TestingTiffMoves\AU\PICKLIST\0000544D.TIF
            \TestingTiffMoves\AU\PICKLIST\0000544E.TIF
            \TestingTiffMoves\AU\PICKLIST\index.dat
            C:\>

            Tigers!

              Topic Starter


              Rookie

              Re: Parsing string from file twice
              « Reply #7 on: October 22, 2009, 10:14:28 PM »
              Billrich
              Thank you for your post.
              Reading your post I realise that I have not been quite as clear as I needed to be.

              I would like to look at each line of the file.
              Whilst looking at that line I need to parse it twice before moving onto the next line
              1st step:
              get whole line and store in a variable, say %%a
              2nd step:
              parse to extract the file name (4th token) from the line again or from the variable (%%a) stored in step one and put this token into another variable, say %%b
              3rd step:
              Use variables %%a & %%b in further operation(s).
              4th step:
              Go to next line and start at 1st step again.

              Is that more clear (useful) or are the waters now so muddy as to be mud?

              Salmon Trout

              • Guest
              Re: Parsing string from file twice
              « Reply #8 on: October 23, 2009, 12:45:48 AM »
              Code:

              Code: [Select]
              @echo off
              for /f "tokens=1,2,3,4* delims=\" %%a in (tiff.txt) do (
               echo Whole line: \%%a\%%b\%%c\%%d
               echo Filename: : %%d
               echo.
              )

              Output:

              Code: [Select]
              Whole line: \TestingTiffMoves\AU\PICKLIST\0000544B.TIF
              Filename  : 0000544B.TIF

              Whole line: \TestingTiffMoves\AU\PICKLIST\0000544C.TIF
              Filename  : 0000544C.TIF

              Whole line: \TestingTiffMoves\AU\PICKLIST\0000544D.TIF
              Filename  : 0000544D.TIF

              Whole line: \TestingTiffMoves\AU\PICKLIST\0000544E.TIF
              Filename  : 0000544E.TIF

              Whole line: \TestingTiffMoves\AU\PICKLIST\index.dat
              Filename  : index.dat
              « Last Edit: October 23, 2009, 01:12:34 AM by Salmon Trout »

              billrich

              • Guest
              Re: Parsing string from file twice
              « Reply #9 on: October 23, 2009, 10:21:54 AM »
              see SL's posts
              « Last Edit: October 23, 2009, 11:37:59 AM by billrich »

              billrich

              • Guest
              Re: Parsing string from file twice
              « Reply #10 on: October 23, 2009, 10:27:25 AM »
              see  SL's posts
              « Last Edit: October 23, 2009, 11:38:30 AM by billrich »

              Salmon Trout

              • Guest
              Re: Parsing string from file twice
              « Reply #11 on: October 23, 2009, 11:22:26 AM »
              try this if you want to actually use named variables

              Code: [Select]
              @echo off
              setlocal enabledelayedexpansion
              for /f "tokens=1,2,3,4* delims=\" %%a in (tiff.txt) do (
               set full_line=\%%a\%%b\%%c\%%d
               set file_name=%%d
               echo Full line: !full_line!
               echo File name: !file_name!
               echo.
              )

              « Last Edit: October 23, 2009, 12:29:49 PM by Salmon Trout »

              billrich

              • Guest
              Re: Parsing string from file twice
              « Reply #12 on: October 23, 2009, 11:36:33 AM »

              Code: [Select]
              @echo off
              setlocal enabledelayedexpansion
              for /f "tokens=1,2,3,4* delims=\" %%a in (tiff.txt) do (
               set full_line=\%%a\%%b\%%c\%%d
               set file_name=%%d
               echo Full line: !full_line!
               echo File name: !file_name!
               echo.
              )


              Yes, your are right I was trying to get code Tiger(OP)  furnished to work or discover why it would not work. I will delete those recent posts.

              Here is tiger's code:

              @echo off
              cls

              for /f "delims=" %%a in (moveTIFF.dat) do (
               echo %%a
              )
              for /f "tokens=4 delims=\" %%b in ("%a%") do (
               echo %%b


              Why doesn't it work?

              Salmon Trout

              • Guest
              Re: Parsing string from file twice
              « Reply #13 on: October 23, 2009, 12:29:03 PM »
              Quote
              Why doesn't it work?

              For one thing, %a% is undefined.

              Sorry for being rude before.

              The OP does not seem to understand the difference between:

              one-sided loop variables, which can be (officially) %%a to %%z and %%A to %%Z, that is (in a batch) two percent signs %% followed by a single character, which only exist within the loop, and

              two-sided ordinary variables which are created using set, e.g. set file=C:\test\readme.txt, and expanded using a percent sign before and after e.g. echo %file% or type %file% or copy %file% d:\folder. These names can be any (reasonable) number of characters in length (I expect there is some kind of system limit but it is not obvious what this is). They exist throughout the batch file. Special conditions apply in loops for double sided variables but I shall not go into that here.

              « Last Edit: October 23, 2009, 01:14:37 PM by Salmon Trout »

              Helpmeh



                Guru

              • Roar.
              • Thanked: 123
                • Yes
                • Yes
              • Computer: Specs
              • Experience: Familiar
              • OS: Windows 8
              Re: Parsing string from file twice
              « Reply #14 on: October 23, 2009, 03:01:45 PM »
              I fiind it funny that the op has started to learn for loops before he or she has really learned the set command.
              Where's MagicSpeed?
              Quote from: 'matt'
              He's playing a game called IRL. Great graphics, *censored* gameplay.