1.

Solve : DOS Viewing of Number of Lines from end of file!?

Answer»

I have a small BATCH file that counts total lines in a file then use more to display from the calculated value. But I would LIKE loop this batch file to display all files in a given folder to the screen or to a file! Can't figure out how to loop thru every file in a folder!
Help!!!!!!!!

Example Below:

@echo off
cls
setlocal
:: Syntax: %1 = displayed Lines
:: %2 = path and input filename.
:: EX: eof 10 \Input_filename.txt ( wildcards may be used )
If {%1} equ {} echo %%1 Missing Data - Re-Enter&&exit /b
If {%2} equ {} echo %%2 Missing Data - Error Data Missing&&exit /b
:@echo on
:start
set infile=%2

for /f "tokens=3" %%A in ('find /v /c "@[emailprotected]" %infile%') do (
set lines=%%A
)

set /a count=%lines%-%1
More +%count% %infile%
:@echo * * * Total Lines in %infile% = %lines% * * *
:endI'm not sure how this would implement into your code, but here is how I would do it:
Code: [Select]@echo off

REM Allow us to alter variables and use the altered value in a for loop
setlocal EnableDelayedExpansion

REM DECLARE total variable
set total=0

REM loop thought the output of the command 'dir /b /a:-d'
REM which OUTPUTS all files withing the current directory
REM that are not folders and only outputs their names
REM aka: makes a list of files in %cd% and loops through them
REM assigning %%A to the current one.
for /f "delims=" %%A in ('dir /b /a:-d') do (

REM clear %working%, which will count our lines in each file
set working=0

REM loop through the file and count the number of lines
for /f "delims=" %%G in (%%A) do set /a working+=1

REM add the current file to the total list
set /a total+=!working!

REM close our loop
)

REM undefine %working%
set "working="

REM report outcome
echo Total lines in %cd% is %total%.

REM delay the closing of the window
pause>nul
Couple of options for you.
1) You could drag and drop the files you want to view onto the batch file and then have it prompt you for input to get the displayed lines.
2) You could drag and drop a folder onto the batch file and then have it prompt you for input to get the the displayed lines and it would then process all the files in the folder.Thanks Guys!!!!!!!!

This is close, looking at my example, on the command line I supply two variables (EX:eof.bat 5 c:\filename & path) then count total of the single file, then subtract 5 from total count, then use more from that number ( total lines - 5 ) to display on the screen ( or file ).
But I'm unable to process more than one file at a time. So I'm really displaying the last 5 lines of each file.Quote from: graymj on April 03, 2014, 04:56:46 PM

But I'm unable to process more than one file at a time. So I'm really displaying the last 5 lines of each file.

What is it that you really want to do?I have a small batch file that counts total lines, subtract a inputted value (5), then use more to display from that line number to the end of file to the screen or file. But I would like loop this batch file (below) to work on all files in a folder Can't figure out how to loop thru multiple files in a folder! ( The code below works fine for a single file, I just need to do more then one at a time )
Help!!!!!!!!

Example Below:

@echo off
cls
setlocal
:: Syntax: %1 = displayed Lines
:: %2 = path and input filename.
:: EX: eof 10 \Input_filename.txt ( wildcards may be used )
If {%1} equ {} echo %%1 Missing Data - Re-Enter&&exit /b
If {%2} equ {} echo %%2 Missing Data - Error Data Missing&&exit /b
:@echo on
:start
set infile=%2

for /f "tokens=3" %%A in ('find /v /c "@[emailprotected]" %infile%') do (
set lines=%%A
)

set /a count=%lines%-%1
More +%count% %infile%
:@echo * * * Total Lines in %infile% = %lines% * * *
:endThis will process every *.txt filename in the folder

It enables delayed expansion within the loop so that filenames with ! in them are processed too.

Code: [Select]@echo off
for %%a in (*.txt) do (
"set file=%%a"
setlocal enabledelayedexpansion
echo filename=!file!
endlocal
pause
)OK, Where do I put my code? I guess I'm not quite sure how to interface the two pieces! Thanks Again for all your help! Code: [Select]@echo off
if "%~2"=="" (
echo Lists the number of lines from the end of files
echo Syntax: %0 NumberOfLines "d:\path\filespec"
echo.
echo example: %0 5 *.txt
echo example: %0 3 "c:\my files folder\*.txt"
pause
goto :EOF
)

setlocal enabledelayedexpansion
for %%a in ("%~2") do (
for /f %%b in ('find /v /c "" ^< "%%a" ') do set "lines=%%b"
set /a count=lines-%~1
if !count! GTR 0 More +!count! "%%a"
)
pausePerfect! Now just trying to figure out why it works! Thanks sooooooooo Much! You're welcome.


Discussion

No Comment Found