Solved

deleting 20 bytes from a file

Posted on 2002-07-19
16
237 Views
Last Modified: 2010-05-02
I want to delete the first 20 bytes from a file
(not replacing them with blanks. the file must be
 smaller in 20 bytes).
 
Currently I’m doing it by opening the file and using INPUT to read it into a string.
Then I write the string (without the first 20 bytes) into a new file, using PUT (in binary mode).
I’ve tried to read different sizes of blocks (up to the whole file at once) and timed the operation.

My problem is the speed.

The fastest code I’ll get here will get the points with an A grade.

For a test file I use a 3.8MB mp3 file. The best result I got on this machine is 30 seconds which is very slow.

I consider it very slow because winzip compress the same file in 5 seconds on same machine. Since winzip does not make mp3 files smaller, and it writes a new file – it means that it use a totally different approach then mine. I am not talking about compression here. I’m only talking about the action of rewriting the file.

I’ll wait for answers until someone will come with a code that will be close to Winzip performance. Since it’s VB here – I don’t expect winzip’s 5 seconds, but I sure expect something below 10 seconds.

Please , don’t send verbal suggestions which you have not checked yourselves.
Such amount of points worth CODE that have been tested.

Thank you.
0
Comment
Question by:AnswerTheMan
  • 4
  • 4
  • 2
  • +4
16 Comments
 
LVL 4

Expert Comment

by:gencross
ID: 7164867
Listening...
0
 
LVL 1

Expert Comment

by:MrRoper
ID: 7164916
Hi,

An idea of the system you are using would be helpful.

Just FYI, I can knock the first 20 bytes of a 3,169 KB file
in less than a second

If your PC is near my spec then i can try with other types of files if you want

0
 
LVL 1

Expert Comment

by:MrRoper
ID: 7164932
a quick update...

I can knock the first 20 bytes off a 9 meg file in approx 3 seconds
0
 
LVL 3

Expert Comment

by:schworak
ID: 7164996
The only way to delete data from a file is to re-write the file.


Try this....


dim fn as integer
dim dat as string

fn=freefile
Open "YOURFILE" for binary as #fn
dat=input(lof(fn),fn)
close fn
open "YOURFILE" for output as #fn
print #fn,mid(dat,21) ' This drops the first 20 bytes
close fn




ALL DONE!
0
 
LVL 18

Expert Comment

by:deighton
ID: 7165014


Private Sub copier(sFrom As String, sTo As String)

    Dim b() As Byte
    Dim b2() As Byte
   
    Dim iFile As Integer
    Dim sFile As String
   
   
   
   
    iFile = FreeFile
    Open sFrom For Binary Access Read As iFile
   
    ReDim b(1 To LOF(iFile))
   
    Get iFile, , b
   
    Close iFile
   
   
   
    ReDim b2(21 To UBound(b))
   
    CopyMemory b2(21), b(21), UBound(b) - 20
   
    iFile = FreeFile
    Open sTo For Binary Access Write As iFile
   
    Put iFile, , b2

    Close iFile

End Sub
0
 
LVL 18

Expert Comment

by:deighton
ID: 7165015
Option Explicit
Private Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)



Private Sub copier(sFrom As String, sTo As String)

    Dim b() As Byte
    Dim b2() As Byte
   
    Dim iFile As Integer
    Dim sFile As String
   
   
   
   
    iFile = FreeFile
    Open sFrom For Binary Access Read As iFile
   
    ReDim b(1 To LOF(iFile))
   
    Get iFile, , b
   
    Close iFile
   
   
   
    ReDim b2(21 To UBound(b))
   
    CopyMemory b2(21), b(21), UBound(b) - 20
   
    iFile = FreeFile
    Open sTo For Binary Access Write As iFile
   
    Put iFile, , b2

    Close iFile

End Sub

Private Sub Form_Load()
Dim t

t = Timer
copier "c:\download\stones.wav", "c:\download\fred66.txt"

MsgBox Timer - t


End Sub
0
 
LVL 18

Expert Comment

by:deighton
ID: 7165017
stones.wav is 10MB and took 3.3 seconds
0
 
LVL 4

Expert Comment

by:gencross
ID: 7165042
deighton, good job.  If you would've waited another 5 minutes I would have posted similar :)

A 4 meg file will run in about .5 second.

MrRoper, not sure why you didn't post your code earlier.  It is a little more helpful to let someone know how you are doing it than just letting everyone know you can.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 26

Expert Comment

by:EDDYKT
ID: 7165046
Option Explicit

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pTo As Any, uFrom As Any, ByVal lSize As Long)
Private Declare Function lstrlenW Lib "kernel32" (ByVal PointerToString As Long) As Long

Private Sub Command1_Click()
    Dim ff As Integer, Filename As String, s As String
    Dim Buffer() As Byte
    Dim nLen As Long
    Dim StrPointer As Long
   
    Debug.Print Now
    Filename = "yourinputfile"
    ff = FreeFile
    Open Filename For Binary Access Read Lock Read Write As #ff
    s = Space$(LOF(ff))
    Get #ff, , s
    Close ff
    StrPointer = StrPtr(s)
    ReDim Buffer(0 To (Len(s) * 2 - 40)) As Byte
    CopyMemory Buffer(0), ByVal (StrPointer + 20), Len(s) * 2 - 40
    s = Buffer
       
   
    Filename = "youroutputfile"
    ff = FreeFile
    Open Filename For Binary Access Write Lock Read Write As #ff
    Put #ff, , s
    Close ff
    Debug.Print Now
End Sub
0
 
LVL 26

Expert Comment

by:EDDYKT
ID: 7165050
deighton :

You are fast. You post your answer when I'm still typing and testing.

8->
0
 
LVL 1

Accepted Solution

by:
MrRoper earned 500 total points
ID: 7165066
Sub blockcopy(source As String, dest As String, start As Long, ende As Long)
'Stop
Dim b(64000) As Byte
Dim c() As Byte
Open source For Binary As #1

test = ((ende - start) Mod 64000) - ((ende - start) \ 64000)
ReDim c(test)
Open dest For Binary As #2
Seek #1, start
For i = 0 To ((ende - start) \ 64000) - 1
    Get #1, , b
    Put #2, , b
Next i
'Stop
If ((ende - start) Mod 64000) > 0 Then
    Get #1, , c
    Put #2, , c
End If
Close #1
Close #2
End Sub

Private Sub Command2_Click()
Dim temp As Long
Debug.Print Now()
temp = FileLen("C:\test\Retail-CLRBkR78CVG.zip")
blockcopy "source", "dest", 21, temp
Debug.Print Now()

End Sub
0
 
LVL 18

Expert Comment

by:deighton
ID: 7165115
Private Sub copier(sFrom As String, sTo As String)

    Dim b() As Byte
   
    Dim iFile As Integer
    Dim sFile As String
   
    iFile = FreeFile
    Open sFrom For Binary Access Read As iFile
   
    ReDim b(21 To LOF(iFile))
   
    Get iFile, 21, b
   
    Close iFile
   
   
    iFile = FreeFile
    Open sTo For Binary Access Write As iFile
   
    Put iFile, , b

    Close iFile

End Sub
0
 
LVL 5

Author Comment

by:AnswerTheMan
ID: 7165175
thanks. during next week i'll check all pieces of code here, and give points to the fastest.
0
 
LVL 1

Expert Comment

by:MrRoper
ID: 7165234
Sub blockcopy(source As String, dest As String, start As Long, ende As Long)
'Stop
Dim b(64000) As Byte
Dim c() As Byte
Open source For Binary As #1

test = ((ende - start) Mod 64000) - ((ende - start) \ 64000)
ReDim c(test)
Open dest For Binary As #2
Seek #1, start
For i = 0 To ((ende - start) \ 64000) - 1
    Get #1, , b
    Put #2, , b
Next i
'Stop
If ((ende - start) Mod 64000) > 0 Then
    Get #1, , c
    Put #2, , c
End If
Close #1
Close #2
End Sub

Private Sub Command2_Click()
Dim temp As Long
Debug.Print Now()
temp = FileLen("C:\test\Retail-CLRBkR78CVG.zip")
blockcopy "source", "dest", 21, temp
Debug.Print Now()

End Sub
0
 
LVL 2

Expert Comment

by:egsemsem
ID: 7165848
create the new file (4 mb) in around a second:


'Decleration

Const FILE_SHARE_READ = &H1
Const FILE_SHARE_WRITE = &H2
Const CREATE_NEW = 1
Const OPEN_ALWAYS = 4
Const OPEN_EXISTING = 3
Const GENERIC_READ = &H80000000
Const GENERIC_WRITE = &H40000000

Private Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, ByVal lpOverlapped As Any) As Long
Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, ByVal lpOverlapped As Any) As Long
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function GetFileSize Lib "kernel32" (ByVal hFile As Long, lpFileSizeHigh As Long) As Long
Private Declare Function GetTickCount& Lib "kernel32" ()


'Main function
Function CopyFile(src As String, dest As String)
   
    Dim hOrgFile As Long, hNewFile As Long, bBytes() As Byte
    Dim nSize As Long, Ret As Long
     
    'Open files
    hNewFile = CreateFile(dest, GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_ALWAYS, 0, 0)
    hOrgFile = CreateFile(src, GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, 0, 0)

    'Get the file size
    nSize = GetFileSize(hOrgFile, 0)
 
    ReDim bBytes(1 To Int(nSize)) As Byte
    'Read from the file
   
    ReadFile hOrgFile, bBytes(1), UBound(bBytes), Ret, ByVal 0&
    'Check for errors
    If Ret <> UBound(bBytes) Then MsgBox "Error reading file ..."

    'Write to the file
    WriteFile hNewFile, bBytes(21), nSize - 20, Ret, ByVal 0&
   
    'Close the files
    CloseHandle hOrgFile
    CloseHandle hNewFile

End Function


'Example on calling the function
Dim start As Long
   
    start = GetTickCount
   
    CopyFile "c:\test.txt", "c:\test1.txt"
       
    MsgBox "Done in: " & Round((GetTickCount - start) / 1000, 2) & " seconds."
       



cheers,

Osama
0
 
LVL 5

Author Comment

by:AnswerTheMan
ID: 7166763
OK. i checked all code(s) posted here.

Here are the results of the code(s) you sent by the chronical order of arriving .
every record was checked 3 times. results are the average.
all was tested on same machine, same HardDisk, same open programs.

expert     3.6MB file        30.4MB file
       result in sec         result in sec    
------------------------------------------
deighton       4                 36
EDDYKT         5                 57  + mistake in bytes
                                       calculation
MrRoper        1                 27
deighton       3                 34
egsemsem       4                 37


i'm glad i didn't have to check milliseconds to decide.....
the result is clear : MrRoper's code is the fastest and
i'm giving him the points with an A.
his approach is totally diffrent from the others and obviously the operation is best optimized his way.

thank you all.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now