JSI Tip 1096. How do I display the first n lines of a text file?


I have scripted ListHead.bat.

To use it:

ListHead <Drive:>\Folder\FileName.Ext Number

Where Number is the number of none null lines you want to display.

ListHead.bat contains:


@echo off
setlocal
if \{%2\}==\{\} goto syntax
if not exist %1 goto syntax
set /a tot=%2
if %tot% EQU 0 goto syntax
set /a cnt=0
set file=%1
for /f "Tokens=1-7* Delims=^>^|^<""" %%i in (%file%) do call :list "%%i" "%%j" "%%k" "%%l" "%%m" "%%n" "%%o" "%%p"
endlocal
goto end
:syntax
@echo Syntax: ListHead filename.ext number
endlocal
goto end
:list
set /a cnt=%cnt% + 1
if %cnt% GTR %tot% goto end
set line=%1%2%3%4%5%6%7%8
set line=%line:"=%
@echo %line%
:end

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish