Link to home
Start Free TrialLog in
Avatar of AnswerTheMan
AnswerTheMan

asked on

deleting 20 bytes from a file

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.
Avatar of gencross
gencross

Listening...
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

a quick update...

I can knock the first 20 bytes off a 9 meg file in approx 3 seconds
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!


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
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
stones.wav is 10MB and took 3.3 seconds
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.
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
deighton :

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

8->
ASKER CERTIFIED SOLUTION
Avatar of MrRoper
MrRoper

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of AnswerTheMan

ASKER

thanks. during next week i'll check all pieces of code here, and give points to the fastest.
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
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
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.