Batch File that copies the first 2 lines of a text file into another text file

4

2

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.

SC.

Posted 2011-07-12T03:12:04.110

Reputation: 100

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 – 2011-07-12T20:07:53.343

Answers

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

Scott McKinney

Posted 2011-07-12T03:12:04.110

Reputation: 884

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 – 2011-07-13T03:16:24.563

@Bacon Bits i'd add to what you said, cmd extensions are enabled by default. btw delayed expansion is not.. – barlop – 2011-07-13T15:20:30.370

+1 I have tested this answer and it seems to work and I see the logic of it – barlop – 2011-07-13T15:42:01.427

Thanks Scott. Dare I ask what "Delayed Expansion" is? – SC. – 2011-07-13T16:36:35.950

1Normally, 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 – 2011-07-14T08:08:12.813

yes set /? explains it with examples. I just found out about it recently.. The default, without delayed expansion, is the most ridiculous behaviour that appears when doing %blah% in an IF or FOR statement. If you have a background in programming you'll see.. from that documentation. Turn delayed expansion on and it behaves sensibly! and you then use !var! instead of %var%, though people still tend to use %blah% when they can get away with it, and !blah! when they need to. – barlop – 2011-07-14T11:59:16.843