Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I need help to know how can i read a text file from bottom up, and save each line in different variables until i find a line in the file starting with a certain character (-).
i am using some "read last x lines" code, which is very useful but i just cant figure out how to stop reading when i find the line starting with the specific character
thnx a lot

Why is everyone so interested in CMD command scripts? I just don't get it.
To answer your question, I think something like this would work:
for /f "tokens=*" %%a in ('findstr "^-" myFile.txt') do set line=%%a

Try this.
[Works on files with less than 100 lines.]
::== r5.bat
@echo off
setLocal EnableDelayedExpansioncall :clear
for /f "tokens=1,* delims=[]" %%a in ('find /n /v "" ^< myfile') do (
if %%a LSS 10 (set N=0%%a) else (set N=%%a)
echo !N! %%b >> myfile.num
)
sort /r < myfile.num > myfile.rev
for /f "tokens=1,* delims= " %%a in (myfile.rev) do (
echo %%b | findstr /b /c:"-"
):clear
for %%f in (.num .rev .new) do if exist myfile%%f del myfile%%f
::==
=====================================
If at first you don't succeed, you're about average.M2

Hi Razor2.3
"Why is everyone so interested in CMD command scripts? I just don't get it."
It must be the thrill of victory and the agony of defeat thing.
=====================================
If at first you don't succeed, you're about average.M2

"It must be the thrill of victory and the agony of defeat thing."
lol, I like that, but the truth is probably more like:
The thrill of agony and the agony of why didn't I do I write the first time.

Ya, It was meant to be a joke...I keep forgetting to put in the smilies.
I'm sure that the OP has other requirements, but here's 2 (1 liner) perl solutions.
===============================================================================
C:\>perl -MTie::File -e "tie @f, 'Tie::File', shift; for(reverse @f){print $_ and last if /^\s*-/ }" file.txt
C:\>perl -MFile::ReadBackwards -e "tie *F, 'File::ReadBackwards', shift; while(<F>){print and last if /^\s*-/}" file.txt

For completeness, I guess I should show a shorter command, but if you're processing a large file, this is less efficient because it uses more resources.
====================================================
C:\test>perl -e "@f = reverse <>; for(@f){print and last if /^\s*-/ }" file.txt
Note: none of these Perl commands are limited to a 100 line file like M2's batch script.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |