Windows batch command(s) to read first line from text file

Batch FileCmd

Batch File Problem Overview


How can I read the first line from a text file using a Windows batch file? Since the file is large I only want to deal with the first line.

Batch File Solutions


Solution 1 - Batch File

uh? imo this is much simpler

  set /p texte=< file.txt  
  echo %texte%

Solution 2 - Batch File

Here's a general-purpose batch file to print the top n lines from a file like the GNU head utility, instead of just a single line.

@echo off

if [%1] == [] goto usage
if [%2] == [] goto usage

call :print_head %1 %2
goto :eof

REM
REM print_head
REM Prints the first non-blank %1 lines in the file %2.
REM
:print_head
setlocal EnableDelayedExpansion
set /a counter=0

for /f ^"usebackq^ eol^=^

^ delims^=^" %%a in (%2) do (
        if "!counter!"=="%1" goto :eof
        echo %%a
        set /a counter+=1
)

goto :eof

:usage
echo Usage: head.bat COUNT FILENAME

For example:

Z:\>head 1 "test file.c"
; this is line 1

Z:\>head 3 "test file.c"
; this is line 1
    this is line 2
line 3 right here

It does not currently count blank lines. It is also subject to the batch-file line-length restriction of 8 KB.

Solution 3 - Batch File

Uh you guys...

C:\>findstr /n . c:\boot.ini | findstr ^1:

1:[boot loader]

C:\>findstr /n . c:\boot.ini | findstr ^3:

3:default=multi(0)disk(0)rdisk(0)partition(1)\WINNT

C:\>

Solution 4 - Batch File

You might give this a try:

@echo off

for /f %%a in (sample.txt) do (
  echo %%a
  exit /b
)

edit Or, say you have four columns of data and want from the 5th row down to the bottom, try this:

@echo off

for /f "skip=4 tokens=1-4" %%a in (junkl.txt) do (
  echo %%a %%b %%c %%d
)

Solution 5 - Batch File

Thanks to thetalkingwalnut with answer https://stackoverflow.com/questions/130116/dos-batch-commands-to-read-first-line-from-text-file#130154 I came up with the following solution:

@echo off
for /f "delims=" %%a in ('type sample.txt') do (
echo %%a
exit /b
)

Solution 6 - Batch File

Slightly building upon the answers of other people. Now allowing you to specify the file you want to read from and the variable you want the result put into:

@echo off
for /f "delims=" %%x in (%2) do (
set %1=%%x
exit /b
)

This means you can use the above like this (assuming you called it getline.bat)

c:\> dir > test-file
c:\> getline variable test-file
c:\> set variable  
variable= Volume in drive C has no label.

Solution 7 - Batch File

powershell Get-Content file.txt -Head 1

This one is much quicker than the other powershell examples above, where the full file is read.

Solution 8 - Batch File

One liner, useful for stdout redirect with ">":

@for /f %%i in ('type yourfile.txt') do @echo %%i & exit

Solution 9 - Batch File

Try this

@echo off
setlocal enableextensions enabledelayedexpansion
set firstLine=1
for /f "delims=" %%i in (yourfilename.txt) do (
    if !firstLine!==1 echo %%i
    set firstLine=0
)
endlocal

Solution 10 - Batch File

To cicle a file (file1.txt, file1[1].txt, file1[2].txt, etc.):

START/WAIT C:\LAERCIO\DELPHI\CICLADOR\dprCiclador.exe C:\LAERCIUM\Ciclavel.txt

rem set/p ciclo=< C:\LAERCIUM\Ciclavel.txt:
set/p ciclo=< C:\LAERCIUM\Ciclavel.txt

rem echo %ciclo%:
echo %ciclo%

And it's running.

Solution 11 - Batch File

The problem with the EXIT /B solutions, when more realistically inside a batch file as just one part of it is the following. There is no subsequent processing within the said batch file after the EXIT /B. Usually there is much more to batches than just the one, limited task.

To counter that problem:

@echo off & setlocal enableextensions enabledelayedexpansion
set myfile_=C:\_D\TEST\My test file.txt
set FirstLine=
for /f "delims=" %%i in ('type "%myfile_%"') do (
  if not defined FirstLine set FirstLine=%%i)
echo FirstLine=%FirstLine%
endlocal & goto :EOF

(However, the so-called poison characters will still be a problem.)

More on the subject of getting a particular line with batch commands:

> How do I get the n'th, the first and the last line of a text file?" > http://www.netikka.net/tsneti/info/tscmd023.htm<br>

[Added 28-Aug-2012] One can also have:

@echo off & setlocal enableextensions
set myfile_=C:\_D\TEST\My test file.txt
for /f "tokens=* delims=" %%a in (
  'type "%myfile_%"') do (
    set FirstLine=%%a& goto _ExitForLoop)
:_ExitForLoop
echo FirstLine=%FirstLine%
endlocal & goto :EOF

Solution 12 - Batch File

Here is a workaround using powershell:

powershell (Get-Content file.txt)[0]

(You can easily read also a range of lines with powershell (Get-Content file.txt)[0..3])

If you need to set a variable inside a batch script as the first line of file.txt you may use:

for /f "usebackq delims=" %%a in (`powershell ^(Get-Content file.txt^)[0]`) do (set "head=%%a")

Solution 13 - Batch File

Note, the batch file approaches will be limited to the line limit for the DOS command processor - see What is the command line length limit?.

So if trying to process a file that has any lines more that 8192 characters the script will just skip them as the value can't be held.

Solution 14 - Batch File

Another way

setlocal enabledelayedexpansion
@echo off
for /f "delims=" %%i in (filename.txt) do (
if 1==1 (
set first_line=%%i
echo !first_line!
goto :eof
))

Solution 15 - Batch File

In Windows PowerShell below cmd can be used to get the first line and replace it with a static value

powershell -Command "(gc txt1.txt) -replace (gc txt1.txt)[0], 'This is the first line' | Out-File -encoding ASCII txt1.txt"

Reference


https://stackoverflow.com/questions/60034/how-can-you-find-and-replace-text-in-a-file-using-the-windows-command-line-envir

Solution 16 - Batch File

Print 1st line only (no need to read entire file):

set /p a=< file.txt & echo !a!

To print 1st line, then wait for user to press a key for next line:
(After printing required lines, press Ctrl+C to stop.)

for /f "delims=" %a in (downing.txt) do echo %a & pause>nul

To print 1st n lines (without user key presses):

type nul > tmp & fc tmp "%file%" /lb %n% /t | find /v "*****" | more +2

Tested on Win 10 CMD.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJesse VogtView Question on Stackoverflow
Solution 1 - Batch FileSpaceballsView Answer on Stackoverflow
Solution 2 - Batch FileindivView Answer on Stackoverflow
Solution 3 - Batch FileAmit NaiduView Answer on Stackoverflow
Solution 4 - Batch FileRoss FuhrmanView Answer on Stackoverflow
Solution 5 - Batch FileJesse VogtView Answer on Stackoverflow
Solution 6 - Batch FileRay HayesView Answer on Stackoverflow
Solution 7 - Batch FileIngmarView Answer on Stackoverflow
Solution 8 - Batch FilePabloGView Answer on Stackoverflow
Solution 9 - Batch FileSarath KSView Answer on Stackoverflow
Solution 10 - Batch FileLaercioView Answer on Stackoverflow
Solution 11 - Batch FileTimo SalmiView Answer on Stackoverflow
Solution 12 - Batch FilemmjView Answer on Stackoverflow
Solution 13 - Batch FilekevinjanszView Answer on Stackoverflow
Solution 14 - Batch FilehhayView Answer on Stackoverflow
Solution 15 - Batch FileJayabharathi PalanisamyView Answer on Stackoverflow
Solution 16 - Batch FileZimbaView Answer on Stackoverflow