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.
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.
You might give this a try:
@echo off
for /f %%a in (sample.txt) do (
echo %%a
exit /b
)
Try GNU32 "head" utility. Don't think what you are after will be easily accomplished by just DOS Batch.
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
)
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.
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
One liner, useful for stdout redirect with ">":
@for /f %%i in ('type yourfile.txt') do @echo %%i & exit
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.
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.
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