tags:

views:

44355

answers:

11

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.

A: 

You can get ports of standard UNIX utilities (specifically 'head') to MS platforms. I don't know if there's a built-in way though.

Luke Halliwell
+5  A: 

You might give this a try:

@echo off

for /f %%a in (sample.txt) do (
  echo %%a
  exit /b
)
The Talking Walnut
This gave me the clue I needed but was not quite right. Not sure what the proper procedure was but I incorporated this solution into the final solution. see http://stackoverflow.com/questions/130116#130209
Jesse
This solution's problem is that it delimits on space instead of newline, and you can't have a filename with spaces. You can fix these issues with the delims and usebackq options in the for loop.
indiv
A: 

Try GNU32 "head" utility. Don't think what you are after will be easily accomplished by just DOS Batch.

nsr81
A: 

Thanks to thetalkingwalnut with answer http://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
)
Jesse
+2  A: 

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.
Ray Hayes
+17  A: 

Again adding to other answers, and combining them into a utility that mimics the gnu head utility, which I named "head.bat":

@echo off
setlocal enabledelayedexpansion
if [%1] == [] goto usage
if [%2] == [] goto usage

SET /a counter=0

for /f "usebackq delims=" %%a in (%2) do (
if "!counter!"=="%1" goto exit
echo %%a
set /a counter+=1
)

goto exit

:usage
echo Usage: head.bat COUNT FILENAME

:exit

Test runs:

Z:\>head "test test.c"
Usage: head.bat COUNT FILENAME
Z:\>head 1 "test test.c"
#include <stdlib.h>

Z:\>head 2 "test test.c"
#include <stdlib.h>
#include <stdio.h>

Z:\>head 3 "test test.c"
#include <stdlib.h>
#include <stdio.h>
int q;

Z:\>head 2 t.c
#include <stdlib.h>
#include <stdio.h>

Z:\>head 0 t.c
indiv
FYI: "GOTO :EOF" That's a special label that will exit the script without having to define a special ":exit" label. It's also useful when defining subroutines in the batch ( what's that you say? subroutines? Yep )
Steven
This seems to bomb out on my several GB text files... On one file it gave me an "Out of Memory" error when trying to return 10 lines, on the other file it just returned a single blank line when asking it to return 10 lines. Any ideas why this happens?
Dan
+1  A: 

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

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

Any ideas how to echo or type the last 10 lines of a txt file?

I'm running a server change log script to prompt admins to state what they're doing, so we can track changes. I'm trying to get the script to show the last 10 entries or so to give an idea of what's been happening recently, but this script above deals with the first line. I'm after the last 10 lines or so.

Any thoughts? I'm totally stuck.

This is a question not an answer to a question. You should post this as its own question if you truly are looking for an answer.
Ray Vega
A: 

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.

kevinjansz
A: 

Here are a couple of quick commands to list lines from a text file.

First read the file into a string variable.

var str content
cat "C:/file.txt" > $content

Now, you are ready to list any lines. To list first line

lex "1" $content

Want to list first 10 lines ?

lex "10]" $content

The last line ?

lex "l" $content      # It is Lower Case Ell (l) inside the double quotes.

Patrick

P M
This is *nix based. No good on Windows.
i5m
A: 

Unfortunately the chosen answer doesn't work in TCC/LE - 4nt :(

Stuart Axon