4

I'm looking for a DOS batch program that takes a file:

First input line
Second input line
Third input line...

And outputs "First input line"

ripper234
  • 222,824
  • 274
  • 634
  • 905

3 Answers3

14

you can just get the first line like this

set /p firstline=<file
echo %firstline%
ghostdog74
  • 327,991
  • 56
  • 259
  • 343
  • That’s because the echo will only show the first line; using it line `if "%firstline%"=="foo"` will use all of it… – mirabilos Apr 07 '21 at 02:58
11

Assuming you mean the Windows cmd interpreter (I'd be surprised if you really were still using DOS), the following script will do what you want:

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

With an input file of infile.txt as:

line 1
line 2
line 3

this will output:

line 1

This will still process all the lines, it just won't print those beyond line 1. If you want to actually stop processing, use something like:

@echo off
setlocal enableextensions enabledelayedexpansion
for /f "delims=" %%i in (infile.txt) do (
    echo %%i
    goto :endfor
)
:endfor
endlocal

Or you could just go get your hands on Cygwin or GnuWin32 and use the head program. That's what I'd do. But, if that's not an option (some workplaces don't allow it), you can create a similar cmd file in Windows itself as follows (winhead.cmd):

@echo off
setlocal enableextensions enabledelayedexpansion

if x%1x==xx goto :usage
if x%2x==xx goto :usage

set /a "linenum = 0"
for /f "usebackq delims=" %%i in (%1) do (
    if !linenum! geq %2 goto :break1
    echo %%i
    set /a "linenum = linenum + 1"
)
:break1
endlocal
goto :finish

:usage
echo.winhead ^<file^> ^<numlines^>
echo.   ^<file^>
echo.      is the file to process
echo.      (surround with double quotes if it contains spaces).
echo.   ^<numlines^>
echo.      is the number of lines to print from file start.
goto :finish

:finish
endlocal
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • Oh, you only need `for` if you either need all or the last input line. For getting the first line using `set /p` is much easier. – Joey Oct 04 '09 at 13:51
  • You can also avoid needing delayed expansion by testing for `first` with `if not defined first` and un-setting it after the first line. – Joey Oct 04 '09 at 13:52
  • Avoid needing it? Why? It's the best thing since sliced bread! Just about every one of my scripts starts with that setlocal line; it's brilliant. – paxdiablo Oct 05 '09 at 02:10
  • Well, I avoid using it if I don't desperately need it as it still incurs some thinking when writing and reading a script as to where delayed expansion is necessary and when not. – Joey Nov 10 '10 at 17:39
-2

why not use the more +1 command via a pipe?

e.g.

type something | more +1

  • 1
    That displays everything **from** the first line, the OP needs **up until** the first line. See `more /?`: "+n -> Start displaying the first file at line n" – TWiStErRob Jun 18 '15 at 21:10