Computing.Net > Forums > Programming > splitting a text file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

splitting a text file

Reply to Message Icon

Name: bjim217
Date: October 25, 2006 at 10:23:07 Pacific
OS: winxp
CPU/Ram: p4/2GB
Product: dell
Comment:

Hi,
I need to be able to take a text file set up like this

#
name1
xxxxxx
xxxx
xxx
#
name2
yyyy
yy
yyyyyyyy
#

etc

and create new text files called name1.txt, name2.txt containing the entries of data between the # signs

in other words i need to create new text files consisting of every 5 lines of data in a master .txt file and need them to be called by the first line of each set of 5...

Can anyone help this novice out please! Someone mentioned awk to me, but having read up a fair bit am not sure how to progress!!!!



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: October 25, 2006 at 12:33:57 Pacific
Reply:

Do you want the #s excluded?


=====================================
If at first you don't succeed, you're about average.

M2



0

Response Number 2
Name: bjim217
Date: October 25, 2006 at 14:06:56 Pacific
Reply:

I would like groups of 5 lines, so including the # after each bit of data ie.

'name1
xxxxxx
xxxx
xxx
#' in name1.txt


'name2
xxxxxx
xxxx
xxx
#' in name2.txt
etc.

hope you can help!
thanks for reading.


0

Response Number 3
Name: pingy
Date: October 25, 2006 at 17:01:20 Pacific
Reply:

First make sure you have a copy of your original document!

Open Microsoft Word.
(I'm using a dutch version so I can't realy tell you what menu's to use)
Open the textfile.
Start the Macro Recorder (I guess it's in the menu [Extra's][Macro][Record])
Give the macro a name and click [OK].
A little toobar apears
Select and copy your first 5 lines.
Create a new document
Paste the 5 lines.
Save the document as a text file as name#.txt
Close this document.
Stop the macro editor by clicking on the
button with the square in the little toolbar
Go to the menu [Extra's][Macro][Macro's]
Select your macro and click [Edit].
Look at the code and try to understand it.
From here on search the help files and the internet and try to adapt this peace of code.

Start looking for something like:
Foreach Paragraph p in ActiveDocument.Paragraphs etc. etc.

Put some time in it and before you know you can do amazing stuff in Word, Excel and other Office programs.

Good luck.


0

Response Number 4
Name: bjim217
Date: October 25, 2006 at 18:37:30 Pacific
Reply:

I have had some success recording a macro that selects the first 5 lines, creates a new .doc with them in and then saves as .txt BUT I am not able to make the file names of the .txt files the first line of each set of 5.
How can you specify the file name to be the first line of the document itself please?
Also, how can you get the macro to repeat until there are no more paragraphs left in the document...


0

Response Number 5
Name: wizard-fred
Date: October 26, 2006 at 07:43:51 Pacific
Reply:

Assuming the list is a text file I would prefer using a program to read the list and create the individual files. What happens if namex is identical to namey?



0

Related Posts

See More



Response Number 6
Name: pingy
Date: October 26, 2006 at 08:42:41 Pacific
Reply:

In theory Wizard-fred is right but I think it's important that you can maintain the program yourself. These "what if's" that Wizard-fred mentions are often numerous but maybe you could exclude thes at the input side.

I'll see if I have some time in the next days to get you going.

By the way, the foreach statement loops and ends by itself when there are no more pragraphs left.



0

Response Number 7
Name: pingy
Date: October 26, 2006 at 09:58:37 Pacific
Reply:

Okay,
I couldn't resist. Here's the basic code.
This isn't flawless but works with your example. It's allways about the "What if's".
It isn't the way a real programmer should dot but I guess this way it's better for you to understand. I Assumed the lines end with a Carriage Return Linefeed (what if they don't....).

Put this code in a module in your Normal.dot or another template. In the last case you allways should open it first and after that the doc to convert. This is because the program acts on the ActiveDocument.

So, here it is:
------------
Sub ConvertDoc()
Dim p As Paragraph
Dim strCopy As String
Dim strName As String
Dim bStart As Boolean
Dim strPath As String

strPath = "C:\Temp" 'Where to put the new docs
bStart = True 'Indicates that we haven't read any lines yet
strCopy = "" 'Just to be sure

For Each p In ActiveDocument.Paragraphs
If IsSyncLine(p.Range.Text) Then 'Is this line the start of a new doc?
If bStart Then 'Nothing to write yet
bStart = False
Else
MakeNewDoc strPath & "\" & strName & ".txt", strCopy & "#" 'you wanted the #-character to close the doc allthough the original doc starts with it!
End If
strCopy = ""
Else 'Create text to put in new doc
If strCopy = "" Then 'This is the first line containing the name
strName = TrimCR(p.Range.Text)
End If
strCopy = strCopy & TrimCR(p.Range.Text) & Chr(13) & Chr(10) 'Take this line and end with a Cariiage Return Linefeed
End If
Next

End Sub

Function IsSyncLine(s As String) As Boolean
IsSyncLine = False
If Left(s, 1) = "#" Then
IsSyncLine = True
End If
End Function

Sub MakeNewDoc(strName As String, strText As String)
Dim docNew As Document
Dim rngNew As Range
Dim p As Paragraph

Set docNew = Application.Documents.Add()
Set rngNew = ActiveDocument.Range
Set p = docNew.Paragraphs.Add(rngNew)
rngNew.Text = strText
docNew.SaveAs strName, wdFormatText
docNew.Close

End Sub

Function TrimCR(s As String) As String
'Windows can be very irritating when it concerns linefeeds and carriage returns
'This one strips the CR
If Asc(Right(s, 1)) = 13 Then
TrimCR = Left(s, Len(s) - 1)
Else
TrimCR = s
End If
End Function
------------------------
I hope the lines aren't broken up.
If you can't paste it give me your email address.

Good luck



0

Response Number 8
Name: pingy
Date: October 26, 2006 at 10:02:05 Pacific
Reply:

I see it's a bit messy so if you can't read it post your email address and I'll send it it to you.


0

Response Number 9
Name: bjim217
Date: October 26, 2006 at 14:11:03 Pacific
Reply:

Thanks very much indeed pingy!!
It worked a treat, and now I can look over your code and start to piece together how it works.

Very much appreciated!


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: splitting a text file

Help browsing a text file www.computing.net/answers/programming/help-browsing-a-text-file/9929.html

Opening a text file in a textbox www.computing.net/answers/programming/opening-a-text-file-in-a-textbox/285.html

How to create a text file using FSO www.computing.net/answers/programming/how-to-create-a-text-file-using-fso/10319.html