Solved DOS Batch to copy first 100 lines of a text file

July 16, 2013 at 11:58:21
Specs: Windows XP
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)



See More: DOS Batch to copy first 100 lines of a text file

Report •

#1
July 17, 2013 at 13:57:52
✔ Best Answer
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.


Report •

#2
July 17, 2013 at 14:44:33
It works great - thanks for your help!! :)

Report •

#3
July 17, 2013 at 14:46:26
No problem! Be sure to check out the update I made to my original post. It just makes the cmd window look cleaner when running it.

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


Report •
Related Solutions


Ask Question