-
Hello, I'm new here.
Hope you can help with the following problem...
I want to extract a part of a file.
I want to send this part, say the first and last 64 bytes of the file to a function.
Does anyone of you know if that's possible and if so, how to do that in VB6 ??
Ask for more info if you need it.
Kleinvaag
-
Give this a whirl
You can use the "Get" statement to manage files like that. It can return a specified number of charcters from a specifed starting point.
Code:
Option Explicit
Private Function getData(strFile As String, lStartPos As Long, lNumChars As Long) As Byte()
Dim bTemp() As Byte
'redim the byte array to the requested size
ReDim bTemp(lNumChars - 1) As Byte
'open file for binary
Open strFile For Binary As #1
'get the data
Get #1, lStartPos, bTemp
'close the file
Close #1
getData = bTemp
End Function
Private Sub Command1_Click()
Dim strFile As String
strFile = "d:\config.txt"
'get the first 64 bytes
Text1.Text = StrConv(getData(strFile, 1, 64), vbUnicode)
'get the last 64 bytes
Text2.Text = StrConv(getData(strFile, FileLen(strFile) - 63, 64), vbUnicode)
End Sub
-
Thx for the help, it worked.
But, .... My next question is :
How do I make 2 new files of these 2 extracted parts
I tried it with
Put #1, 1,strfile
but that generates a file of 72 bytes ?
It seems to add 8 bytes in the beginning of the file.
Any suggestions ?
Kleinvaag
-
Kleinvaag,
I don't know much about your problem, but I think the extra eight bytes at the beginning of your file was put there by the "put" statement to hold a descriptor of your data. By this, I mean that it first puts (stores) the length of the string and maybe some other info, and then puts the string.
You might find something about this in the help file under the "put statement."
I know this doesn't get you up and going, but it might help you to know why the extra bytes are there.
Best of luck!
Wendy
-
Give this a whirl
This uses the last function i gave you. This writes the data got back from the last function to a new file.
Code:
Private Sub Command1_Click()
'write the first 64 bytes
writeNewFile "e:\test1.txt", "e:\config.txt", 1, 64
'write the last 64 bytes
writeNewFile "e:\test2.txt", "e:\config.txt", FileLen(strFile) - 63, 64
End Sub
Private Sub writeNewFile(strNewFile As String, strOldFile As String, lStartPos As Long, lNumChars As Long)
Dim bTemp() As Byte
'open new file for binary
Open strNewFile For Binary As #2
'get the data usign the other function
bTemp = getData(strOldFile, lStartPos, lNumChars)
'put the chunk into the new file
Put #2, , bTemp
'close the file
Close #2
End Sub
-
Thank you
Iain,
Sorry for my late reply. Holiday and stuff.
I've tried your code and ..... it works.
Thx a lot, you've been a big help
Kleinvaag