Hi,

I need to create multiple TXT files from splitting 1 single TXT file that should look something like this:
xyz;0;12345;data;data;data;data
xyz;0;12345;data;data;data;data
xyz;0;67890;data;data;data;data
xyz;0;67890;data;data;data;data
xyz;0;54321;data;data;data;data
xyz;0;54321;data;data;data;data

Let's say the original file was like above, and named original.txt, I would like the resulting files to be like so with the program generating the file names.

filename : 1.txt
xyz;0;12345;data;data;data;data
xyz;0;12345;data;data;data;data
filename : 2.txt
xyz;0;67890;data;data;data;data
xyz;0;67890;data;data;data;data
filename : 3.txt
xyz;0;54321;data;data;data;data
xyz;0;54321;data;data;data;data

The original filename will be constant(hardcoded) and could be automatically deleted(sent to recycle BIN) when split is done succesfully.

I hope this is enough explanation and anything I can start with would be greatly appreciated. I am an old CLIPPER programmer just starting with VB.

Thanks

Recommended Answers

All 3 Replies

what is the criteria or the variable that determines how the original file is split? in the example it is the 3rd set of data that is grouped and split into different .txt files. are you looking for a code example that you can alter for your needs or what are you looking for?

what is the criteria or the variable that determines how the original file is split? in the example it is the 3rd set of data that is grouped and split into different .txt files. are you looking for a code example that you can alter for your needs or what are you looking for?

You are correct , the third field is what determines the file split area. My example had 2 records for each client let's say, but it could be multiple records for a given client and maybe only one for others. I have reasonable knowledge of programming concepts but am not familiar with VB at all. I would have wanted an example witht he correct syntax if possible. Here is, in words, what I think would be a working program:

declare vars
ThisLine = chars or string or other data type???
counter = integer = 1
temp = chars or string or other data type???
open source text file for reading
do while not eof()
read first line of text into ThisLine
client = substring(third field)
do while current_line's third field = client and not eof()
add ThisLine into temp
next
open new file for writing called counter.txt
write temp to counter.txt
close counter.txt
counter = counter+1
next
close source text file
kill source text file ;delete original file

Like I had posted initialy, anything with correct VB syntax to start from will be better than nothing.

Regards

this is a simple copy and paste thing. it works you just need to change the file locations. if you hav any questions email me at YCprogrammer@verizon.net or on here.

'Declarations
Dim inti As Integer 'count for strSet
Dim strSet() As String 'dynamic variable
Dim intCnt As Integer
Dim intCnt2 As Integer
Dim intFile As Integer
Dim intSemi(3) As Integer

Private Sub Command1_Click()
Open "C:\original.txt" For Input As #1 'open original 'replace with location of original
inti = 0 'Resets counter
Do While EOF(1) = False 'if the end of file is not found
inti = inti + 1 'number of strSet
ReDim Preserve strSet(inti) As String
Line Input #1, strSet(inti)
Loop
Close #1 'close original
'now that all the lines are read and in strings we can separate them
Dim intFile As Integer
intFile = 1
For intCnt = 1 To inti
Dim intj As Integer
intj = 0 'resets counter of sections
If strSet(intCnt) > "" Then 'if line still exists
For intCnt2 = 1 To Len(strSet(intCnt))
If Mid(strSet(intCnt), intCnt2, 1) = ";" Then
intj = intj + 1
intSemi(intj) = intCnt2 'finds dividers
If intj = 3 Then 'if the 3 dividers have been found
Dim decVar As String
decVar = Mid(strSet(intCnt), intSemi(2) + 1, intSemi(3) - intSemi(2) - 1) 'gets the 3 set of data between the 2nd and 3rd divider
GoTo FoundDiv
End If
End If
Next intCnt2
Else: GoTo EndLoop
End If
FoundDiv:
iFile = FreeFile
Open "C:\" & intFile & ".txt" For Output As #iFile 'replace c:\ with location of save location
For intCnt2 = 1 To inti
If Mid(strSet(intCnt2), intSemi(2) + 1, Len(decVar)) = decVar Then 'if the file has the same 3rd set of data
Print #iFile, strSet(intCnt2) 'writes data into file
strSet(intCnt2) = "" 'deletes line so it isnt checked again
End If
Next intCnt2 'checks next set
intFile = intFile + 1
Close #iFile
EndLoop:
Next intCnt
Kill "C:\original.txt" 'replace with location of file
End Sub

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.