Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi,
I don't have a clue how to do this....
I need to make a script that will cut-down a large(r) file (plain text-file) into smaller pieces.
Example: I have 1 file with 153 lines (this is random) of text. I want to break that file down into small files of 50 lines, so I will have file01.txt (50 lines), file02.txt (50 lines), file03.txt (50 lines) and file04.txt (3 lines).
Can this be done with plain DOS commands?
Any help would be appreciated!
Kind regards,
Igor.

Yes, it can be done and I'll do as soon as possible (Windows 2000 required NOT real plain DOS 16 bit environment of course).

Hi IVO.
Amazing! Sometimes you make me wonder what CANNOT be done in DOS environment?!!
;)
Promise me: do take some freetime inbetween your posts; sometimes I wonder if you ever sleep/rest/not-script?? EHEheheeh :)
Take care!
IgorInWaitingMode....

The reason why I did not post the script is related to its complexity that may lead to misunderstands. But here is at your own risk:
@Echo Off
:: CTXT.BAT Syntax: CTxt [Unit:][PathName]FileName
:: Cut-down the specified file in pieces of 50 lines each
:: naming FileName_nn.ext starting from 01.
:: Win NT/2K/XP required; blanks not allowed in FNameIf not %1.==[]. (
Cmd /V:On /C Call %0 [] %1
GoTo :EOF)Shift
Echo.
If %1.==. (Echo File missing & GoTo :EOF)
If not exist %~f1 (
Echo File %~f1 not found
GoTo :EOF)Set Piece=50
Set Count=1
Set FileN=1
Set FileS=0!FileN!Echo Cutting-down %~f1 in pieces of !Piece! lines...
Echo.
If exist %~dpn1_*%~x1 Del %~dpn1_*%~x1For /F "tokens=* delims=" %%A in (%1) Do (
Echo %%A>> %~dpn1_!FileS!%~x1
Set /A Count=!Count!+1
If !Count! gtr !Piece! (
Set Count=1
Set /A FileN=!FileN!+1
Set FileS=!FileN!
If !FileS! lss 10 Set FileS=0!FileS!
)
)
Dir /B %~dpn1_*%~x1
Echo.
Echo Processing completed - !FileN! files writtenSet Count=
Set FileN=
Set FileS=
Set Piece=

Hi IVO,
Works like a charm! (And I ALMOST understand how the batchfile works....)
Have spent quite some time to test and did some kind of stress tests. I made a batchfile to fill a test file with over 100000 lines (autonumbered) and then processed this file with Piece set to 1 in your batchfile.... Amazing how fast it works!
* I Tried to alter the number to have a 4-digit counter instead of the current 2-digit, but don't (yet) understand that part of the batchfile.
** I have made a minor change to write the output in a separate 'ctxt_out' directory for followup processing.
Your helping hand is getting me involved in batch-solutions more-and-more now....
ciao!
Igor.

To have a 4-digit counter replace
Set FileS=0!FileN!
with
Set FileS=000!FileN!and
If !FileS! lss 10 Set FileS=0!FileS!
with
If !FileS! lss 10 Set FileS=0!FileS!
If !FileS! lss 100 Set FileS=0!FileS!
If !FileS! lss 1000 Set FileS=0!FileS!and the same applies if you want 5-digit counter and more...
Anyway avoid to use in Set /A statetements numbers with leading zeros, as they are interpreted as octal numbers, so 08 and 09 lead to errors breaking the batch. This is why I use a raw counter to do true arithmetic (FileN) and another variable (FileS) to edit the result.

This is exactly what I'm looking for. It works on smaller files, but I have a 6gb file I need to break up. I get a "file not found" error when I try it on the big file. Again, it works fine on smaller ones.
Where is the limitation? Computer ram perhaps?
Thanks

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |