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

November 22, 2018 at 05:40:57
Specs: Windows 7
I have a text file of 1000+ lines of text and I would like to select the first 100 rows of text and copy/move this text to a new text file called export1.txt. I have found the below from another user on this forum and this work great for the first 100 lines of text. However, is it possible to select the next line of 100 and copy/move to a new file? In other words the file with 1000+ rows of text and I want to extract 100 rows each time (not the same 100 rows eg. if the rows were a-z I would like rows a-k extracted and them rows l-z extracted) and save to a new file called export1, export2 etc. The below copies the text and not moves it. If it can be moved to a new filename that would be great.

This is the example I have found.

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


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

Reply ↓  Report •

✔ Best Answer
November 25, 2018 at 22:15:07
Ah, my bad. I wrote script for commandline input for filename. Let's simplify things and just use "myfile.txt". The other consideration is to make sure the script runs in the same subdirectory as myfile.txt, or else to fix the script to provide fully qualified path to that, and direct it's output to your intended location. (I'm not proficient in using GUI with batch - I've always run from the command prompt).
:: ------- begin trial #2
set brk=50
set fnam=myfile.txt
set fbase=myfile
set fext=.txt
if not defined fbase (
echo usage: %0 file_name.ext
echo file will be split into sets of 50 lines named with the same base-name
echo no file given
goto :eof
)
:: edited to fix my goof: ""call :xx  fixed to: "" call :xx in following line
for /f "tokens=1* delims=[]" %%a in ('find /v /n ""^<%fnam%') do if "%%b" neq "" call :xx %%a "%%b"
PAUSE
goto :eof

:xx
set /a n=(%1-1)/brk+1
echo output to:fbase%%n%%fext%: --- content: %~2
>>%fbase%%n%%fext% echo.%~2

::-------------- end batch

this should create a set of files serially numbered: myfile1.txt, myfile2.txt... in the location from which it ran, or elsewhere if you provide an althernate path for output. I recommend running from the command-prompt (the "black box") for debugging/testing/analysis. GUI doesn't give much feedback or control for debugging.
And, no, the first couple lines, "rem'd out" were just left in for reference. Also, if you DO run from GUI, that last "pause" should hold the screen for you to see what went wrong.
I hope this helps.

message edited by nbrane



#1
November 22, 2018 at 07:54:03
can be done, give me about 45min for scripting and testing

i5-6600K[delid]@4.8GHz/4.4GHz@1.424v LLC=6 | 2x4GB Crucial-DDR4-2133CL15@14-14-14-30 1T 2800MHz@1.37v
MSI Armor RX 570 4GB@1410Mhz core@1.2v/1920MHz


Reply ↓  Report •

#2
November 22, 2018 at 08:39:36
I cant remember how to do it properly with findstr, it ignores empty lines and messes up the line counting.

This will have to wait till monday, unless someone else wants to jump in.

P.S. pretty sure there is a script like this posted here some months ago....

edit: maby nbrane remembers

i5-6600K[delid]@4.8GHz/4.4GHz@1.424v LLC=6 | 2x4GB Crucial-DDR4-2133CL15@14-14-14-30 1T 2800MHz@1.37v
MSI Armor RX 570 4GB@1410Mhz core@1.2v/1920MHz

message edited by hidde663


Reply ↓  Report •

#3
November 22, 2018 at 08:48:04
hidde663 many thanks for the taking the time and the quick reply. I wasn't expecting a reply that quick. No problem. It can wait till Monday. If it helps, the list I have of 1000+ lines won't have any empty lines in as I remove them anyway. (Top and Bottom) Thanks again.

Reply ↓  Report •

Related Solutions

#4
November 22, 2018 at 15:23:24
Hi hidde663 I have found the following post on what I am after and it seems to work. (edited to my needs - moving out 50 lines for testing)

https://www.computing.net/answers/p...

@echo off & setlocal enabledelayedexpansion
set fnam=mylist.txt
for %%z in (%fnam%) do set fbase=%%~nz & set fext=%%~xz
set /a n=-1
for /f "tokens=1* delims=]" %%a in ('find /v /n "" %fnam%') do (
set /a n+=1
set /a n1="n%%50"
if !n1! equ 0 (
set /a n2=n/50+1
set n2=0!n2!
set out=!fbase!!n2:~-2!!fext!
)
>>!out! echo.%%b
)

This works 99% correctly and is what I am after. However, to make it 100% the only thing that is a little annoying (but I can live with unless it can be changed to work) is that the first output file called mylist 01.txt has the first line blank and the last line blank. If I open mylist 02.txt the first line is 50 but the last line is a blank line.
So for testing on the above code (and to see what I did if you want to recreate my steps) is I made a textfile called mylist.txt and the first line had 1 the second line 2 etc all the way to 1000 with 1000 being the last line (no blank row). I then ran the above batch and it then created 21 text files with 50 lines of text (I used numbers to check it wasn't missing any lines) however, mylist 21.txt only has the text 1000 in (I can see what it's doing but I can't see why). If this can be resolved then that would be great. I can live with blank rows being inserted but if they can be removed that would be great!
Many thanks again.


Reply ↓  Report •

#5
November 24, 2018 at 21:37:39
This worked on my end, with a basic "test file" as you described, with no blank lines:
@echo off & setlocal enabledelayedexpansion
:: this created the test file:
rem (for /l %%a in (1 1 1000) do echo ------ %%a ********) > myfile.txt
::----------- here down
set brk=50
set fnam=%1
set fbase=%~n1
set fext=%~x1
if not defined fbase (
echo usage: %0 file_name.ext
echo file will be split into sets of 50 lines named with the same base-name
echo no file given
goto :eof
)
:: fixed a goof in the following line: ""call :xx  to: "" call :xx
for /f "tokens=1* delims=[]" %%a in ('find /v /n ""^<%fnam%') do if "%%b" neq "" call :xx %%a "%%b"
goto :eof

:xx
set /a n=(%1-1)/brk+1
>>%fbase%%n%%fext% echo.%~2
::-------------- end batch

See if this works better...

message edited by nbrane


Reply ↓  Report •

#6
November 25, 2018 at 10:25:29
nbrane. Many thanks. I will give it a try.

message edited by CJ001


Reply ↓  Report •

#7
November 25, 2018 at 10:29:28
nbrane
Many thanks for the reply. Could you tell me the exact steps you did to make this work? I am testing this in XP. I saved a file with 1000 rows of numbers and then called this file myfile.txt. I then copied your code into notepad and saved this as move.bat, I then double clicked it but it didn't do anything? CMD came up and then disappeared but no files were created. Also when I tried running it from CMD it doesn't run either just shows on screen: usage: move.bat file_name.ext, file will be split into sets of 50 lines named with the same base-name, no file given. I notice that the first line of your code says:

:: this created the test file:
rem (for /l %%a in (1 1 1000) do echo ------ %%a ********) > myfile.txt
::----------- here down

However, I already have the file with 1000 lines in and I would like this to be split into 50 text files. I don't need this line of code in do I?
Have I missed something? Many thanks again.

message edited by CJ001


Reply ↓  Report •

#8
November 25, 2018 at 22:15:07
✔ Best Answer
Ah, my bad. I wrote script for commandline input for filename. Let's simplify things and just use "myfile.txt". The other consideration is to make sure the script runs in the same subdirectory as myfile.txt, or else to fix the script to provide fully qualified path to that, and direct it's output to your intended location. (I'm not proficient in using GUI with batch - I've always run from the command prompt).
:: ------- begin trial #2
set brk=50
set fnam=myfile.txt
set fbase=myfile
set fext=.txt
if not defined fbase (
echo usage: %0 file_name.ext
echo file will be split into sets of 50 lines named with the same base-name
echo no file given
goto :eof
)
:: edited to fix my goof: ""call :xx  fixed to: "" call :xx in following line
for /f "tokens=1* delims=[]" %%a in ('find /v /n ""^<%fnam%') do if "%%b" neq "" call :xx %%a "%%b"
PAUSE
goto :eof

:xx
set /a n=(%1-1)/brk+1
echo output to:fbase%%n%%fext%: --- content: %~2
>>%fbase%%n%%fext% echo.%~2

::-------------- end batch

this should create a set of files serially numbered: myfile1.txt, myfile2.txt... in the location from which it ran, or elsewhere if you provide an althernate path for output. I recommend running from the command-prompt (the "black box") for debugging/testing/analysis. GUI doesn't give much feedback or control for debugging.
And, no, the first couple lines, "rem'd out" were just left in for reference. Also, if you DO run from GUI, that last "pause" should hold the screen for you to see what went wrong.
I hope this helps.

message edited by nbrane


Reply ↓  Report •

#9
November 26, 2018 at 00:22:01
nbrane
Many thanks for the reply. It still doesn't work. The steps I did were: Copy the above line of code between the :: ------- begin trial #2 and ::-------------- end batch. I renamed my list of 1000 rows of numbers to myfile.txt. I saved this batch file in the same directory as my myfile.txt and called it move.bat. I double clicked it and the cmd screen showed quickly and then went off. No files were created. I then went to cmd and changed my path to the directory of the move.bat i.e cd C:\temp\test. I then typed move.bat but this time the cmd screen came up but stayed up but no files were created. In cmd it shows the following:

C:\Temp\test>move.bat

C:\Temp\test>set brk=50

C:\Temp\test>set fnam=myfile.txt

C:\Temp\test>set fbase=myfile

C:\Temp\test>set fext=.txt

C:\Temp\test>if not defined fbase (
echo usage: move.bat file_name.ext
echo file will be split into sets of 50 lines named with the same base-name
echo no file given
goto :eof
)
The syntax of the command is incorrect.

C:\Temp\test>for /f "tokens=1* delims=[]" %a in ('find /v /n ""^<myfile.txt') do
if "%b" neq ""call :xx %a "%b"

C:\Temp\test>

Have I missed something? Many thanks for the help so far.


Reply ↓  Report •

#10
November 26, 2018 at 17:23:54
Once again, my bad. If you revisit post #8, you will witness my shame and embarrassment. As you can see, a space was omitted between the double-quotes and the 'call' command. If you have enough patience to try again, try it with that mistake fixed. All the things you did were correct, but all the things I did were not! (My test-bat did have the space, so who knows how it went AWOL in the brief journey to the forum-post.)

Reply ↓  Report •

#11
November 27, 2018 at 08:34:50
nbrane
Huge thanks for this. It's working!! Many thanks, this will certainly speed my extraction of rows out. Thanks :-)

Reply ↓  Report •

#12
November 28, 2018 at 00:46:24
I'm saving this, will come in handy later :)

i5-6600K[delid]@4.8GHz/4.4GHz@1.424v LLC=6 | 2x4GB Crucial-DDR4-2133CL15@14-14-14-30 1T 2800MHz@1.37v
MSI Armor RX 570 4GB@1415Mhz core@1.2v/1920MHz


Reply ↓  Report •

Ask Question