Batch that will cut/paste into another txt file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
deibertine
Posts: 6
Joined: 19 Aug 2010 21:44

Batch that will cut/paste into another txt file

#1 Post by deibertine » 19 Aug 2010 23:11

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 :lol:

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Batch that will cut/paste into another txt file

#2 Post by ghostmachine4 » 19 Aug 2010 23:37

download coreutil for windows and then you can use split command

Code: Select all

c:\test> split -l 50 file 

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Batch that will cut/paste into another txt file

#3 Post by orange_batch » 19 Aug 2010 23:48

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
)

deibertine
Posts: 6
Joined: 19 Aug 2010 21:44

Re: Batch that will cut/paste into another txt file

#4 Post by deibertine » 20 Aug 2010 00:18

ghostmachine4 wrote:download coreutil for windows and then you can use split command

Code: Select all

c:\test> split -l 50 file 


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.

deibertine
Posts: 6
Joined: 19 Aug 2010 21:44

Re: Batch that will cut/paste into another txt file

#5 Post by deibertine » 20 Aug 2010 00:20

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.

deibertine
Posts: 6
Joined: 19 Aug 2010 21:44

Re: Batch that will cut/paste into another txt file

#6 Post by deibertine » 20 Aug 2010 00:30

ghostmachine4 wrote:download coreutil for windows and then you can use split command

Code: Select all

c:\test> split -l 50 file 


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

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Batch that will cut/paste into another txt file

#7 Post by orange_batch » 20 Aug 2010 00:42

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.

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Batch that will cut/paste into another txt file

#8 Post by ghostmachine4 » 20 Aug 2010 01:40

deibertine wrote:
ghostmachine4 wrote:download coreutil for windows and then you can use split command

Code: Select all

c:\test> split -l 50 file 


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.

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: Batch that will cut/paste into another txt file

#9 Post by !k » 20 Aug 2010 07:50

Two vbs script to cut text files by n lines.
Parameters of the Batya's script: <LIST_of_files> [n]

Post Reply