Command for /f: retrieving data from a file

Solved
vayleme Posted messages 91 Status Member -  
vayleme Posted messages 91 Status Member -
Hello,

I can't retrieve data contained in a line of a text file.
Here is what the file looks like.

data : 1
) data : 2
- data : 3
$ data : 4

the value at the end of the line is unknown, I can retrieve the data from the last line and the second to last one with these commands:

for /f "tokens=2 delims=:" %%a in (file.txt) do set a=%%a
for /f "tokens=2 delims=: eol=$" %%b in (file.txt) do set b=%%b


But I can't find the commands for the first two lines.
I hope you can help me.

--
Best regards,
Vayleme

2 answers

  1. barnabe0057 Posted messages 14329 Registration date   Status Contributor Last intervention   4 930
     
    Hello,

    There is a space between the special character and the data, so it should be simple.

    for /f "eol=: tokens=2 delims= " %%a in (file.txt) do set var=%%a


    “Artificial intelligence is defined as the opposite of natural stupidity.”
    1
    1. vayleme Posted messages 91 Status Member 16
       
      Hello,
      you're really helping me a lot these days Barnabé ^^ thanks again for your response, but unfortunately your command retrieves the data from the last line.
      here is the output of the command.
      for /f "eol=: tokens=2 delims= " %%a in (fichier.txt) do echo %%a

      returns
      1
      2
      3
      4

      and if I do
      for /f "eol=: tokens=2 delims= " %%a in (fichier.txt) do set var=%%a

      only the "4" is in the variable. I only want the "2" and another command to keep only the "1".
      0
      1. barnabe0057 Posted messages 14329 Registration date   Status Contributor Last intervention   4 930 > vayleme Posted messages 91 Status Member
         
        I don't quite understand which part you want to recover, a real sample of a txt file would be welcome.
        0
      2. vayleme Posted messages 91 Status Member 16 > barnabe0057 Posted messages 14329 Registration date   Status Contributor Last intervention  
         
        The txt file contains the 4 lines I wrote above, the goal is to retrieve in 4 different variables the values at the end of each line (1, 2, 3, 4) knowing that these values are not static. But I just found out at the moment, but it's a lot of patchwork, I will put my solution in the post and leave the subject open for a day in case someone has a better idea.
        0
  2. vayleme Posted messages 91 Status Member 16
     
    @echo off
    cls

    ::retrieve last line
    for /f "tokens=2 delims=:" %%a in (fichier.txt) do set a=%%a

    ::copy the file without the last line
    for /f "tokens=* delims=: eol=$" %%e in (fichier.txt) do echo %%e >> %cd%\temp.txt

    ::pause file creation
    timeout /nobreak /t 003 >nul

    ::retrieve the penultimate line
    for /f "tokens=2 delims=:" %%b in (%cd%\temp.txt) do set b=%%b

    ::copy the temp file without the last line
    for /f "tokens=* delims=: eol=-" %%f in (%cd%\temp.txt) do echo %%f >> %cd%\tempp.txt

    ::pause file creation
    timeout /nobreak /t 003 >nul

    ::retrieve the second line
    for /f "tokens=2 delims=:" %%c in (%cd%\tempp.txt) do set c=%%c

    ::retrieve the first line
    for /f "tokens=2 delims=: eol=)" %%d in (%cd%\tempp.txt) do set d=%%d

    ::pause before file deletion
    timeout /nobreak /t 002 >nul

    ::delete files
    erase %cd%\temp.txt %cd%\tempp.txt

    echo press any key to see the values!
    pause >nul

    echo %a%
    echo %b%
    echo %c%
    echo %d%

    echo.
    echo press any key to exit.
    pause >nul
    exit


    Best regards,
    Vayleme
    0
    1. barnabe0057 Posted messages 14329 Registration date   Status Contributor Last intervention   4 930
       
      I'm still not sure I've understood correctly, does that help you:
      for /f "eol=$ tokens=1 delims=-)" %%a in (fichier.txt) do echo %%a

      It doesn't display the last line.
      0
      1. vayleme Posted messages 91 Status Member 16 > barnabe0057 Posted messages 14329 Registration date   Status Contributor Last intervention  
         
        You got it right, almost. Your command retrieves the 3rd line (and not just the number).
        I'll try to summarize what I want to do.

        Here is the content of the file:

        data: 5
        ) data: 18
        - data: 72
        $ data: 0

        The goal is to retrieve the values 5, 18, 72, and 0 into variables such that
        a=5
        b=18
        c=72
        d=0

        To retrieve the 0 (last line) into variable d, here is the command:

        for /f "tokens=2 delims=:" %%d in (file.txt) do set d=%%d


        To retrieve the 72 into variable c, here is the command:

        for /f "tokens=2 delims=: eol=$" %%c in (file.txt) do set c=%%c



        The goal is to find the command to retrieve the "18" and the command to retrieve the "5".

        My solution (shown above) consists of retrieving the last line, then duplicating the file by removing the last line, then retrieving the last line from the duplicate, etc., for the 4 lines.

        I leave the post open to see if there is a solution to retrieve these values without modifying the file.
        0
      2. barnabe0057 Posted messages 14329 Registration date   Status Contributor Last intervention   4 930 > vayleme Posted messages 91 Status Member
         
        Maybe like this :
        for /f "eol=$ tokens=*" %%a in (fichier.txt) do (for /f "eol=- tokens=1,2 delims=):" %%g in ('echo %%a') do (set var=%%h))
        0
      3. vayleme Posted messages 91 Status Member 16 > barnabe0057 Posted messages 14329 Registration date   Status Contributor Last intervention  
         
        yep it works, just two small errors to correct:

        for /f "eol=$ tokens=*" %%a in (fichier.txt) do (for /f "eol=- tokens=2 delims=:" %%g in ('echo %%a') do (set var=%%g)


        Thanks for suggesting this solution, I would have never thought of nesting multiple for loops in the same command, you have taught me something again ^^.
        Thank you for your help!

        An effective solution having been found, I will close this topic.
        0