Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Splitting a file into multiple files?

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I have a question about splitting a file.
I have a big textfile that should be splitted in 56 smaller files. The file contains 55 times the word "deletecode" (between the real content of the file). Those 55 strings should be the splitting points. Is there a program that can do this automatically? I tried to do it by creating a macro for "Ultra Edit 32". But that macro didn't support some "save" commands. Do you guys know how I can handle this?
 
The VB code below opens the file 'C:\bigfile.txt' and starts writing to the file 'C:\outfile1'. When it encounters the line 'deletecode' it closes(saves) outfile1 and opens 'c:\outfile2' and so on, until the end of bigfile.txt.
Code:
Dim FileCount As Long
Dim FileLine As String

FileCount = 1

Open "C:\bigfile.txt" For Input As #1

Open "C:\outfile" & FileCount For Output As #2

While Not EOF(1)
    Line Input #1, FileLine
    If Trim(FileLine) = "deletecode" Then
        FileCount = FileCount + 1
        Close #2
        Open "C:\outfile" & FileCount For Output As #2
    Else
        Print #2, FileLine
    End If
Wend
Close #2
Ruairi

Could your manufacturing facility benefit from real time process monitoring? Would you like your employees to be able to see up to the minute goal and actual production?
For innovative, low cost solutions check out my website.
 
Here's a different method that should split files of any type, not just text files:[tt]

Open "C:\bigfile.any" For Binary As #1
G$ = String$(LOF(1), 0)
Get #1, 1, G$
Close #1
StartByte& = 1
FileCount = 1
Srch$ = "deletecode"
Do
EndByte& = InStr(StartByte&, G$, Srch$)
If EndByte& = 0 Then
EndByte& = Len(G$)
Else
EndByte& = EndByte& + Len(Srch$)
End If
P$ = Mid$(G$, StartByte&, EndByte& - StartByte& + 1)
Open "C:\outfile" & FileCount For Binary As #1
Put #1, 1, P$
Close #1
FileCount = FileCount + 1
If EndByte& = Len(G$) Then Exit Do
StartByte& = EndByte& + 1
Loop
[/tt]
This just reads the input file into one enormous string and then parses it into smaller strings delimited by "deletecode", writing them to the output files using Ruairi's FileCount increment.

The only real advantage here is in speed. Instead of reading and writing the files one line at a time, it performs one read operation on the input file and then one write operation for each of the output files.

Just an option... :)
VCA.gif

Alt255@Vorpalcom.Intranets.com

"What this country needs is more free speech worth listening to."[tt]
Hansell B. Duckett[/tt]​
 
I have a quick question in regards to Alt255's post.

What happens if the file is on the order of hundreds of MB in size, does the computer crash?

Troy Williams B.Eng.
fenris@hotmail.com

 
Depends on RAM and available HD space. I have read the Windows swap file into a string with one Get. The action doubled the size of the swap file but the machine didn't crash. It just slowed it down a little...

Normally, one wouldn't try to read a 500mb file in one pass. Also, one cannot read binary files after opening them in Input mode.

VCA.gif

Alt255@Vorpalcom.Intranets.com

"What this country needs is more free speech worth listening to."[tt]
Hansell B. Duckett[/tt]​
 
Hey, thanks for you help. That worked really well. Man, life would be so much easier with knowledge of visual basic... :)
I have one more question.
In that big file there's also 55 times the word "endoflist", "emptycode", "startoflist".
I want to have those 3 words replaced by the words "other","empty","dataset". This replacing is of course quite easy to do with a text editor. But if it can be done within the same vbs script, that would save me some time again. Can you give me the VB code for this? As you can see, I'm quite new to VB (in fact I only have the program, and I haven't ever created something by myself...). So please forgive me for asking you stupid questions like this... ;-)

Mike
 
One more question: Is it also possible to have that word "startoflist" replaced by the word "dataset" & the current date?
 
Sure, Mike. You can do all of those things. Use the Replace function....
[tt]
Open "C:\bigfile.any" For Binary As #1
G$ = String$(LOF(1), 0)
Get #1, 1, G$
Close #1
G$ = Replace(G$, "endoflist", "other")
G$ = Replace(G$, "emptycode", "empty")
G$ = Replace(G$, "startoflist", "dataset")
Kill "C:\bigfile.any"

Open "C:\bigfile.any" For Binary As #1
Put #1, 1, G$
Close #1
[/tt]
...Just be careful and make a backup of any file you want to treat this way. In this case, the file decreases in size, so in order to write the final version properly you have to use the Kill statement to delete it and create a new version, rather than overwriting the old version.

I'll allow you to answer your second question by researching the Replace function and the VB Date function. It shouldn't be too hard to work out with a little experimentation.

Good luck.
VCA.gif

Alt255@Vorpalcom.Intranets.com

"What this country needs is more free speech worth listening to."[tt]
Hansell B. Duckett[/tt]​
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top