Command for /f: retrieving data from a file
Solved
vayleme
Posted messages
91
Status
Membre
-
vayleme Posted messages 91 Status Membre -
vayleme Posted messages 91 Status Membre -
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:
But I can't find the commands for the first two lines.
I hope you can help me.
--
Best regards,
Vayleme
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 réponses
@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
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:
To retrieve the 72 into variable c, here is the command:
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.
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.
yep it works, just two small errors to correct:
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.
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.
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.
returns
1
2
3
4
and if I do
only the "4" is in the variable. I only want the "2" and another command to keep only the "1".