Batch that will cut/paste into another txt file
Moderator: DosItHelp
-
- Posts: 6
- Joined: 19 Aug 2010 21:44
Batch that will cut/paste into another txt file
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
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
-
- Posts: 319
- Joined: 12 May 2006 01:13
Re: Batch that will cut/paste into another txt file
download coreutil for windows and then you can use split command
Code: Select all
c:\test> split -l 50 file
-
- Expert
- Posts: 442
- Joined: 01 Aug 2010 17:13
- Location: Canadian Pacific
- Contact:
Re: Batch that will cut/paste into another txt file
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.
This will not skip blank lines in mytext.txt.
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
)
-
- Posts: 6
- Joined: 19 Aug 2010 21:44
Re: Batch that will cut/paste into another txt file
ghostmachine4 wrote:download coreutil for windows and then you can use split commandCode: 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.
-
- Posts: 6
- Joined: 19 Aug 2010 21:44
Re: Batch that will cut/paste into another txt file
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.
-
- Posts: 6
- Joined: 19 Aug 2010 21:44
Re: Batch that will cut/paste into another txt file
ghostmachine4 wrote:download coreutil for windows and then you can use split commandCode: 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
-
- Expert
- Posts: 442
- Joined: 01 Aug 2010 17:13
- Location: Canadian Pacific
- Contact:
Re: Batch that will cut/paste into another txt file
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.
Add pause to the end of the script if you want to see what's in the window.
-
- Posts: 319
- Joined: 12 May 2006 01:13
Re: Batch that will cut/paste into another txt file
deibertine wrote:ghostmachine4 wrote:download coreutil for windows and then you can use split commandCode: 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.
Re: Batch that will cut/paste into another txt file
Two vbs script to cut text files by n lines.
Parameters of the Batya's script: <LIST_of_files> [n]
Parameters of the Batya's script: <LIST_of_files> [n]