Computing.Net > Forums > Disk Operating System > Cut large txt-file to multiplefiles

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

Cut large txt-file to multiplefiles

Reply to Message Icon

Original Message
Name: Igor
Date: December 2, 2003 at 08:03:52 Pacific
Subject: Cut large txt-file to multiplefiles
OS: Windows 2000-DOS
CPU/Ram: PIII/256Mb
Comment:

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.


Report Offensive Message For Removal


Response Number 1
Name: IVO
Date: December 2, 2003 at 09:08:22 Pacific
Reply:

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).


Report Offensive Follow Up For Removal

Response Number 2
Name: ikerstges
Date: December 2, 2003 at 10:24:22 Pacific
Reply:

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....


Report Offensive Follow Up For Removal

Response Number 3
Name: IVO
Date: December 3, 2003 at 06:13:48 Pacific
Reply:

Check your in-box for the script.


Report Offensive Follow Up For Removal

Response Number 4
Name: dagul
Date: December 3, 2003 at 08:07:18 Pacific
Reply:

i'm interested too. can you please post it here?


Report Offensive Follow Up For Removal

Response Number 5
Name: IVO
Date: December 3, 2003 at 08:38:12 Pacific
Reply:

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 FName

If 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_*%~x1

For /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 written

Set Count=
Set FileN=
Set FileS=
Set Piece=


Report Offensive Follow Up For Removal


Response Number 6
Name: Igor
Date: December 3, 2003 at 15:05:47 Pacific
Reply:

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.


Report Offensive Follow Up For Removal

Response Number 7
Name: IVO
Date: December 5, 2003 at 02:35:06 Pacific
Reply:

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.


Report Offensive Follow Up For Removal

Response Number 8
Name: turboquattro
Date: January 21, 2004 at 13:32:32 Pacific
Reply:

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


Report Offensive Follow Up For Removal






Post Locked

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


Go to Disk Operating System Forum Home



Results for: Cut large txt-file to multiplefiles

Printing to a .TXT file from a DOS
    Summary: First, let me say I know almost nothing about DOS. (I'm a Mac user) I am a member of an radio controlled car club and we're using some DOS software to count laps for our races. The races are stru...
www.computing.net/answers/dos/printing-to-a-txt-file-from-a-dos-/11692.html

read txt file to variables
    Summary: i am using dos on a win 98 machine. i have a bat file that will store information in a readme.txt file. it will store 1 word on the first line of this file, and 1 word on the second. so inside the fil...
www.computing.net/answers/dos/read-txt-file-to-variables/10994.html

Reading Txt file in dos mode
    Summary: If you can change the TXT file to anything you want, you could change it to a BATCH file. Then CALL the text.bat file. In the file, start with an @ECHO OFF, then at the start of each line, add an ECHO...
www.computing.net/answers/dos/reading-txt-file-in-dos-mode/13836.html






Which MP3 player do you have?

iPod/iPhone
Zune
Something Else
None


View Results

Poll Finishes In 4 Days.
Discuss in The Lounge
Poll History






Data Recovery Software