Page 1 of 1
Batch that will cut/paste into another txt file
Posted: 19 Aug 2010 23:11
by deibertine
I am trying to write a batch file where it will cut a bulk per line within a txt file, then cut/paste this into another new txt file.
Basically what I am trying to do here is cutting the huge txt file into different txt files (i.e. test0.txt, test1.txt, test2.txt, test3.txt ....)
> open txt file > cut 50 lines > paste 50 lines into new txt file
Cheers, DB

Re: Batch that will cut/paste into another txt file
Posted: 19 Aug 2010 23:37
by ghostmachine4
download
coreutil for windows and then you can use split command
Re: Batch that will cut/paste into another txt file
Posted: 19 Aug 2010 23:48
by orange_batch
Keep in mind that DOS always appends a line return to the end of a line, so there will be a line return at the end of line 50.
This will skip blank lines in mytext.txt.
Code: Select all
setlocal enabledelayedexpansion
:: Starts splitting at 0-index, that is, test0.txt instead of test1.txt.
set filecounter=0
for /f "delims=" %%x in (mytext.txt) do (
set /a linecounter+=1
echo:%%x>>test!filecounter!.txt
if !linecounter!==50 set /a linecounter=0,filecounter+=1
)
This will not skip blank lines in mytext.txt.
Code: Select all
setlocal enabledelayedexpansion
:: Starts splitting at 0-index, that is, test0.txt instead of test1.txt.
set filecounter=0
for /f "delims=] tokens=1*" %%x in ('type mytext.txt ^| find /v /n ""') do (
set /a linecounter+=1
echo:%%y>>test!filecounter!.txt
if !linecounter!==50 set /a linecounter=0,filecounter+=1
)
Re: Batch that will cut/paste into another txt file
Posted: 20 Aug 2010 00:18
by deibertine
Ok, I have heard of this

So how do I input them into different files?
I do c:\test> split -l 50 test1.txt test2.txt .......
This displays invalid argument.
Do I need to separate c:\test1.txt c:\test2.txt in commas?
cheers.
Re: Batch that will cut/paste into another txt file
Posted: 20 Aug 2010 00:20
by deibertine
orange_batch wrote:Keep in mind that DOS always appends a line return to the end of a line, so there will be a line return at the end of line 50.
This will skip blank lines in mytext.txt.
Code: Select all
setlocal enabledelayedexpansion
:: Starts splitting at 0-index, that is, test0.txt instead of test1.txt.
set filecounter=0
for /f "delims=" %%x in (mytext.txt) do (
set /a linecounter+=1
echo:%%x>>test!filecounter!.txt
if !linecounter!==50 set /a linecounter=0,filecounter+=1
)
This will not skip blank lines in mytext.txt.
Code: Select all
setlocal enabledelayedexpansion
:: Starts splitting at 0-index, that is, test0.txt instead of test1.txt.
set filecounter=0
for /f "delims=] tokens=1*" %%x in ('type mytext.txt ^| find /v /n ""') do (
set /a linecounter+=1
echo:%%y>>test!filecounter!.txt
if !linecounter!==50 set /a linecounter=0,filecounter+=1
)
Ok I did this but it quickly displayed the cmd then disappeared without placing the output file in local drive.
I saved this code into a bat file.
Ty.
Re: Batch that will cut/paste into another txt file
Posted: 20 Aug 2010 00:30
by deibertine
Ok I do see on whats going on with this.
Basically doing this c:\test> split -l 50 file
Resulted tons of file.txtaa , file.txab , file.txac so forth...
and also displays the file into one line in each txtaa txtab ....
Is it possible to cut/paste then place the copied text to a file but in a vertical arrangement?
text
text
text
not like this:
texttexttext
Re: Batch that will cut/paste into another txt file
Posted: 20 Aug 2010 00:42
by orange_batch
My code works properly. mytext.txt points to mytext.txt in the same directory as the batch file. It outputs to test#.txt in the same directory. You can change where these point to if you desire.
Add pause to the end of the script if you want to see what's in the window.
Re: Batch that will cut/paste into another txt file
Posted: 20 Aug 2010 01:40
by ghostmachine4
deibertine wrote:Ok I do see on whats going on with this.
Basically doing this c:\test> split -l 50 file
Resulted tons of file.txtaa , file.txab , file.txac so forth...
and also displays the file into one line in each txtaa txtab ....
Is it possible to cut/paste then place the copied text to a file but in a vertical arrangement?
text
text
text
not like this:
texttexttext
a file created in windows using for example notepad, should have \r\n as line endings. When you split it, it should produce the same results.
Occasions when you see your text being lined up like that is you have \n as line endings and you view you file in notepad. use the correct
line ending (\r\n) and you should be alright.
Re: Batch that will cut/paste into another txt file
Posted: 20 Aug 2010 07:50
by !k
Two vbs script to cut text files by n lines.Parameters of the
Batya's script: <LIST_of_files> [n]