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

16,567

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
Share:
16,567

Related videos on Youtube

SC.
Author by

SC.

My first computer was an 8-bit 48k RAM ZX Spectrum+. Ever since I've have trying to keep up with Technology as it quickly changes. Finance professional with some programming skills and an interest in Machine Learning.

Updated on September 18, 2022

Comments

  • SC.
    SC. over 1 year

    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.

  • Bacon Bits
    Bacon Bits almost 13 years
    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.
  • barlop
    barlop almost 13 years
    @Bacon Bits i'd add to what you said, cmd extensions are enabled by default. btw delayed expansion is not..
  • barlop
    barlop almost 13 years
    +1 I have tested this answer and it seems to work and I see the logic of it
  • SC.
    SC. almost 13 years
    Thanks Scott. Dare I ask what "Delayed Expansion" is?
  • Bacon Bits
    Bacon Bits almost 13 years
    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.
  • barlop
    barlop almost 13 years
    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.