7 Replies - 7768 Views - Last Post: 30 September 2019 - 12:51 PM Rate Topic: -----

#1 shawarmah   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 30-July 10

VB6 Read And Copy File one Byte at a time

Posted 30 July 2010 - 12:39 PM

When i click "showcontents" it should display something like this in the label IF the file is non-text.
The problem is it would not show some non-text files.. or it would only display a few characters like "MZ"

Another is the copy button doesnt work. It should copy a file from the source to destination one byte at a time.

Posted Image


heres the copy and read part of my code, any help would be very much appreciated.. :D :D :D

Dim thefile, sAscii, thefile2, dAscii As String
Dim bUnicode() As Byte
Dim aUnicode() As Byte
Dim g1 As Integer
Dim g2 As Integer
Dim i, ff As Integer
Dim j, l As Integer


Public Sub Binary_read()

thefile = File1.Path + "\" + File1.FileName
'get file id
ff = FreeFile
Open thefile For Binary As #ff
    ReDim bUnicode(LOF(ff))
    Get #ff, , bUnicode
Close #ff

sAscii = StrConv(bUnicode, vbUnicode) 'Key function!

lbl_content.Caption = sAscii
End Sub


Public Sub Binary_copy()

thefile = File1.Path + "\" + File1.FileName
thefile2 = Dir2.Path
'get file id
g1 = FreeFile
Open thefile For Binary As #g1
g2 = FreeFile
Open thefile2 For Binary As #g2
Do Until EOF(g1)
    ReDim aUnicode(LOF(g2))
    Get #g1, , aUnicode
    Put #g2, , aUnicode
Loop
Close #g1
Close #g2
End Sub


This post has been edited by shawarmah: 30 July 2010 - 12:41 PM


Is This A Good Question/Topic? 0
  • +

Replies To: VB6 Read And Copy File one Byte at a time

#2 raziel_   User is offline

  • Like a lollipop
  • member icon

Reputation: 469
  • View blog
  • Posts: 4,281
  • Joined: 25-March 09

Re: VB6 Read And Copy File one Byte at a time

Posted 30 July 2010 - 02:10 PM

i dont understand the show contain part but why you copy the file this way? use file system object. add the reference of Microsoft Scripting Runtime and use the file system object to just copy the file like so:
Private Sub Command1_Click()
    Dim FSO As New FileSystemObject
    'Copy the file'
    FSO.CopyFile "C:\MySourceFileFolder\File.txt", "C:\DestinationFileFolder\File.txt"
End Sub


Was This Post Helpful? 1
  • +
  • -

#3 shawarmah   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 30-July 10

Re: VB6 Read And Copy File one Byte at a time

Posted 30 July 2010 - 02:22 PM

thanks for the advice, but our professor said we cant use fso.. it should copy byte per byte..
=)

when i click "showcontents" the contents of the file selected at the filelist should be shown on the label below..
its purpose is like notepad.. an example would be when you open a *.jpeg image on notepad..
it would display a series of characters.

any ideas on how to do that? :sweatdrop:
Was This Post Helpful? 0
  • +
  • -

#4 raziel_   User is offline

  • Like a lollipop
  • member icon

Reputation: 469
  • View blog
  • Posts: 4,281
  • Joined: 25-March 09

Re: VB6 Read And Copy File one Byte at a time

Posted 30 July 2010 - 02:45 PM

use a rich text box instead. it will show you the special chars like the notepad. add the rich text box reference. simple textbox and labels cut the string till they find special symbol. and your copy must work. why dose it not work.

This post has been edited by NoBrain: 30 July 2010 - 02:47 PM

Was This Post Helpful? 1
  • +
  • -

#5 thava   User is offline

  • D.I.C Lover
  • member icon

Reputation: 180
  • View blog
  • Posts: 1,607
  • Joined: 17-April 07

Re: VB6 Read And Copy File one Byte at a time

Posted 30 July 2010 - 06:21 PM

if you want to copy byte by byte then remove the array and use a variable remove the redim statement also
Was This Post Helpful? 1
  • +
  • -

#6 shawarmah   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 30-July 10

Re: VB6 Read And Copy File one Byte at a time

Posted 31 July 2010 - 03:05 AM

Posted Image

rich text box worked! thank you so much mr. nobrain.. :genius: :tt1:


@mr. thava.. im sorry.. could you please expain further..? what do you mean remove the array and use a variable instead.. ^_^


i'm thinking of copying what is written on the richtextbox, the problem is how am i supposed to save it in the same extension and will the file remain intact.. any suggestions?
Was This Post Helpful? 0
  • +
  • -

#7 shawarmah   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 30-July 10

Re: VB6 Read And Copy File one Byte at a time

Posted 31 July 2010 - 04:39 AM

i did it!! thanks for all the help nobrain and thava.. :rockon:
Was This Post Helpful? 0
  • +
  • -

#8 Logman7585   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 30-September 19

Re: VB6 Read And Copy File one Byte at a time

Posted 30 September 2019 - 12:51 PM

I'm having a problem where after using the code from this thread it's leaving NUL characters in the file. I'm using Notepad2. This maybe wouldn't be a problem for text since I can turn them off from Notepad2, but I have to read a file in chunks of 512 bytes and then transmit them over a COM port for a firmware update, so I can't have any null characters or my firmware may be corrupt. Is there something specific I can do to eliminate this? I'm hoping I don't have to go byte-by-byte.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1