computing
  • 5

Solved DOS Batch To Copy First 100 Lines Of a Text File

  • 5

Hello. I would like to copy the first x lines of a HUGE text file to another text file. Is there a way to do this with a batch script?

source.txt (contains millions of lines)
output.txt (contains x number of lines)

Share

1 Answer

  1. I believe this will do what you want. Please let me know if you need anything else.

    @ECHO OFF
    setlocal enabledelayedexpansion
    
    SET /P maxlines=Enter number of lines to be moved to new txt document: 
    SET /A linecount=0
    
    FOR /F "delims=" %%A IN (textfile1.txt) DO ( 
      IF !linecount! GEQ %maxlines% GOTO ExitLoop
      ECHO %%A >> C:\users\username\desktop\textfile2.txt
      SET /A linecount+=1
    )
    
    :ExitLoop
    ECHO All Done.
    ECHO.
    ECHO Press any key to close this window.
    PAUSE>NUL
    EXIT

    UPDATE: Notice I updated Line 4 of my code. I made a mental note to fix it before posting, but forgot. All I did was add a ‘:’ with a space after it so when you type in the number while running the batch file it looks cleaner.

    Law of Logical Argument: Anything is possible if you don’t know what you’re talking about.

    • 0