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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Spliting a text file

Status
Not open for further replies.

simoncpage

Programmer
Apr 4, 2002
256
GB
Can anyone help me with this....I want to split a text file in to 255 character segments consecutively.

TO get the first 255, Left$(string, 255) but suppose the file is in made up of 1029....

i.e 255, 255, 255, 255, 9 - how do I get the next 255 in the file???

Any help would be appreciated thanks

Simon
 
I suppose a simple loop would do the trick


if bytes_left (string) < 255 do
begin
putfile (Left$(string, 255))
end
else putfile (string)

Don't know the exact VBA syntax but this should be no problem...
---
It's never too late to do the Right thing
 
Dont think you have got the idea.

I want to take the first 255 and stick in a string. then I want the next 255 in the file (after the first 255) and put this into a different string etc until the whole file is split into size 255 strings.

 
I don't think a string would be usefull...

A simple array would be much easier. You could keep a variable with an index to count how many different strings the array holds..

That way you could address every single string with a simple routine... ---
It's never too late to do the Right thing
 
Project: Standard EXE
Controls: txtFile (Textbox), txtSize (Textbox), txt (Textbox, MultiLine set to True), cmb (ComboBox), lblOut (Label), cmdBrowse (Command button), cmdSplit (Command button), cmdMerge (Command button), dlg (CommonDialog Control)
Code:

Dim nLen As Double
Dim strFName As String
Dim b() As Byte

Private Sub cmdBrowse_Click()
dlg.ShowOpen
txtFile = dlg.FileName
strFName = dlg.FileTitle
End Sub

Private Sub cmdMerge_Click() ' merge previously splitted file to its split direcory
Dim n As Double: Dim i As Long: Dim bOpen As Boolean: Dim d As Double

For i = 0 To File1.ListCount - 1
If IsNumeric(Right(File1.List(i), 3)) Then
If Not bOpen Then
Open lblOut & &quot;\&quot; & Left(File1.List(i), Len(File1.List(i)) - 4) For Binary As 2
bOpen = True
End If
n = FileLen(lblOut & &quot;\&quot; & File1.List(i))
Open lblOut & &quot;\&quot; & File1.List(i) For Binary As 1
ReDim b(n - 1)
Get #1, , b()
Put #2, d + 1, b()
Close #1
d = d + n
End If
Next
Close #2
File1.Refresh
End Sub

Private Sub cmdSplit_Click() 'split file on two equal files
If Trim(txtFile) = &quot;&quot; Or Dir(txtFile) = &quot;&quot; Then
MsgBox &quot;File doesn't exist&quot;
cmdBrowse.SetFocus
Exit Sub
End If
If Val(txtSize) < 1 Then
MsgBox &quot;Incorrect split size&quot;
txtSize.SetFocus
Exit Sub
End If
On Error GoTo er1
lblOut = txtFile & &quot;_split&quot;
lblOut.Refresh
MkDir lblOut
File1.Path = lblOut
File1.Visible = True
txt.Visible = False
If cmb.ListIndex = -1 Then cmb.ListIndex = 2
Dim i As Long 'number of file
Dim ss As Double 'split size
If cmb.ListIndex = 0 Then
ss = 1024
ss = ss * 1024
ElseIf cmb.ListIndex = 1 Then
ss = 1024
Else
ss = 1
End If
ss = Round(Val(txtSize) * ss, 0)
nLen = FileLen(txtFile)
Open txtFile For Binary As 1
While nLen > ss
ReDim b(ss - 1)
Get #1, ss * i + 1, b()
Open lblOut & &quot;\&quot; & strFName & &quot;.&quot; & Format(i, &quot;000&quot;) For Binary As 2
Put #2, , b()
Close #2
File1.Refresh
i = i + 1
nLen = nLen - ss
Wend
ReDim b(nLen - 1)
Get #1, ss * i + 1, b()
Open lblOut & &quot;\&quot; & strFName & &quot;.&quot; & Format(i, &quot;000&quot;) For Binary As 2
Put #2, , b()
Close #2
File1.Refresh
Beep
Close #1
Exit Sub
er1:
Select Case Err
Case 75
If MsgBox(&quot;Split folder already exist. Would you like to overwrite it?&quot;, vbYesNo) = vbYes Then
Dim j As Long
File1.Path = lblOut
For j = File1.ListCount - 1 To 0 Step -1
Kill lblOut & &quot;\&quot; & File1.List(j)
Next
RmDir lblOut
Resume
Else
lblOut = &quot;Output Directory&quot;
End If
Case Else
MsgBox Err.Number & &quot;: &quot; & Err.Description
End Select
End Sub

Private Sub Form_Load()
cmb.ListIndex = 0
File1.Visible = False
End Sub

-----This is perfect!!
 
if your goal is to type formulas in seperate columns...then


B C D
=MID(A1,1,255)| =MID(A1,256,255)| =MID(A1,511,255)

 
It should work in VBA as well....


variable_A=mid$(string,1,255)
variable_b=mid$(string,256,255)

etc..

 
Hello Simon,

This seems to work. Just pass it your string and it'll return an array.


Public Function SplitString(ByVal strToSplit As String)
Dim strArray() As String
Dim lngStrLen As Long
Dim lngElements As Long
Dim lngIndex As Long

strLen = Strings.Len(strToSplit)
If strLen > 255 Then
If strLen Mod 255 = 0 Then
lngElements = (strLen / 255) - 1
Else
lngElements = Int(strLen / 255)
End If

ReDim strArray(lngElements) As String

For lngIndex = LBound(strArray()) To UBound(strArray())
strArray(lngIndex) = Strings.Left(strToSplit, 255)
If Strings.Len(strToSplit) > 255 Then
strToSplit = Strings.Right(strToSplit, Strings.Len(strToSplit) - 255)
End If
Next
Else
ReDim strArray(0)
strArray(0) = strToSplit
End If

SplitString = strArray
End Function


Hope this helps,
Pete
 
Thats a really useful function uberpudge. However I have now realised that its actually CByte that I am using so its not 255 chars but 255 bytes.

Would

strByte = CByte(strToSplit)
If strLen > 255 Then
etc....

be the best way to change this..?
 
Hello Simon,

The &quot;Byte&quot; data type is a number ranging from 0 - 255.
8-bit ASCII is a number ranging from 0 to 255.

Given that each character in the string is really an 8-bit ASCII value, I'm not really sure there's any need for modification to the above routine.

For example, the charater &quot;A&quot; is a decimal value of 65.
This is represented in binary as 01000001.

Do you need the string data converted to a numeric value?
If so, which type of number (binary, hexidecimal, decimal)?

Hope this helps,
Pete
 
I understand that but I am converting to Hex so this will alter things??

Heres my hex convert function....
-----------------------------------------------------
Function StrToHex(Text As String, Optional Separator As String = &quot; &quot;) As String

Dim a As Long
Dim Pos As Long
Dim Char As Byte
Dim PosAdd As Long
Dim ByteSize As Long
Dim ByteArray() As Byte
Dim ByteReturn() As Byte
Dim SeparatorLen As Long
Dim SeparatorChar As Byte


If (Not m_InitHex) Then Call InitHex


SeparatorLen = Len(Separator)


ByteSize = (Len(Text) * 2 + (Len(Text) - 1) * SeparatorLen)
ReDim ByteReturn(ByteSize - 1)
Call FillMemory(ByteReturn(0), ByteSize, Asc(Separator))


ByteArray() = StrConv(Text, vbFromUnicode)


PosAdd = 2 + SeparatorLen
For a = 0 To (Len(Text) - 1)
ByteReturn(Pos) = m_ByteToHex(ByteArray(a), 0)
ByteReturn(Pos + 1) = m_ByteToHex(ByteArray(a), 1)
Pos = Pos + PosAdd
Next


StrToHex = StrConv(ByteReturn(), vbUnicode)

End Function
 
Hello Simon,

As I don't know what the following are:
m_InitHex
InitHex
FillMemory

I don't know if it suits your purpose.
If you want a string representation of hex values from the array that my function above returns, feel free to use this:


Public Function ConvertToHex(ByVal varPassed As Variant)
Dim lngElement As Long
Dim lngIndex As Long
Dim strHexString As String
Dim strChar As String

For lngElement = LBound(varPassed) To UBound(varPassed)
For lngIndex = 1 To Strings.Len(varPassed(lngElement))
strChar = Strings.Left(varPassed(lngElement), 1)
varPassed(lngElement) = Strings.Right(varPassed(lngElement), Strings.Len(varPassed(lngElement)) - 1)
strHexString = strHexString + Conversion.CStr(Conversion.Hex(Strings.Asc(strChar)))
Next
varPassed(lngElement) = strHexString
strHexString = &quot;&quot;
Next

ConvertToHex = varPassed
End Function

Caveat:
This returns another array of the exact same number of elements, but as a string representation of a hex value contains double the number of characters, you may want to reassemble the elements into another string and pass the reassembled string back to the SplitString function so you get 255 characters per element again.
Hope this helps,
Pete
 
Sorry for not getting back earlier. I am going to try it now so basically your hex convert will convert the text to hex which in turn will return 255 char string when converted back?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top