4

I have a text file A.txt

Pinging [xx.xx.xx.xx] with 32 bytes of data:

Request timed out.

Ping statistics for xx.xx.xx.xx:
Packets: Sent = 1, Received = 0, Lost = 1 (100% loss)

I would like to copy the first 2 lines of this file into another file B.txt, i.e.

Pinging [xx.xx.xx.xx] with 32 bytes of data:
Request timed out.

I know I can use FOR /F to loop through the lines in the file. I know how to skip the first two lines, but not to only read them. I've also tried to do it by using FOR /F with a DO ECHO and related FIND command and also a straight FINDSTR command (in both cases searching for "Pinging" and "Request") but I cannot get either to work properly.

1
  • you could use gnuwin32 the head command, type file.a | head -n 2 but that's really a *nix command. but it's interesting to know if it can be done in batch/windows.
    – barlop
    Jul 12, 2011 at 20:07

1 Answer 1

6

I've posted the entire code below, but the real meat is here:

:: set counter
set c=0
for /f "delims=|" %%i in (%1) do (
:: increment counter for each line read
  set /a c=!c!+1
  if !c! leq %3 echo %%i >> %2
)

Basically you set the counter variable c to 0, then increment it for each line read from the text file. You test the counter against the max lines and echo it to the output file if it's less than or equal.

The "delims=|" parameter in the for loop keeps it from breaking the line into tokens at space characters and thus only outputting a partial line. The unusual !c! variable is how to reference variables that are using delayed expansion. If you just used %c%, the value would never change inside the for loop.

You provide the script with three parameters: input file, output file and # of lines to output. The %1,%2 and %3 represent each of these input parameters in the script.

 @echo off

REM ======================================================================
REM
REM NAME: 
REM
REM AUTHOR: Scott McKinney
REM DATE  : 
REM
REM PURPOSE: 
REM COMMENT: 
REM DEPENDENCIES:
REM
REM Revisions:
REM
REM ======================================================================

setlocal ENABLEEXTENSIONS
setlocal ENABLEDELAYEDEXPANSION

set a=%1
if "%1"=="" goto HELP
if "%a:~0,2%"=="/?" goto HELP
if "%a:~0,2%"=="-?" goto HELP
if "%a:~0,2%"=="/h" goto HELP
if "%a:~0,2%"=="/H" goto HELP
if "%a:~0,2%"=="-h" goto HELP
if "%a:~0,2%"=="-H" goto HELP
if "%a:~0,3%"=="--h" goto HELP
if "%a:~0,3%"=="--H" goto HELP

:: set counter
set c=0
for /f "delims=|" %%i in (%1) do (
:: increment counter for each line read
  set /a c=!c!+1
  if !c! leq %3 echo %%i >> %2
)
goto END

:HELP
echo.
echo Usage: %0 ^<input file^> ^<output file^> ^<n lines^>
echo.
echo. Outputs the first ^<n^> lines from ^<input file^> to ^<output file^>.
echo.
:END
6
  • Just a note for future reference, but if you enable command extentions (setlocal enableextensions) you can use if /i to do a case-insensitive match. That will cut a few lines of code for you and make things easier to maintain.
    – Bacon Bits
    Jul 13, 2011 at 3:16
  • @Bacon Bits i'd add to what you said, cmd extensions are enabled by default. btw delayed expansion is not..
    – barlop
    Jul 13, 2011 at 15:20
  • +1 I have tested this answer and it seems to work and I see the logic of it
    – barlop
    Jul 13, 2011 at 15:42
  • Thanks Scott. Dare I ask what "Delayed Expansion" is?
    – SC.
    Jul 13, 2011 at 16:36
  • 1
    Normally, when an environment variable is used in a batch file the value is expanded once before execution. It is not checked subsequently to see if it has changed every time it is called. Particularly with loops you can run into problems with variables not changing. Delayed expansion (setlocal enabledelayedexpansion) allows you to tell the system to wait to expand the variable. You specify a delayed variable with ! instead of %, so you would say !variable! instead of %variable%. See setlocal /? and set /?. They're technically slower to process, but I've never noticed.
    – Bacon Bits
    Jul 14, 2011 at 8:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .