CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Dec 2011
    Posts
    4

    Exclamation Copy over 4GB files with progress problem.

    So far i have made some routines that copy files over 2gb but corrupted, add some extra bytes at the end and can't copy files bigger than 4GB if you can help me will appreciate it because can't find solution for long time

    I realy need a working copy function for files over 4GB please help me


    TotalFileSize = fso.GetFile(SourceFile).Size

    'Open file
    Dim sFile As Long
    Dim dFile As Long
    sFile = CreateFile(SourceFile, GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, 0&, OPEN_EXISTING, 0, 0)
    dFile = CreateFile(Destination, GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, 0&, CREATE_NEW, 0, 0)

    Dim Buffer$
    Dim bytesWritten As Long

    If sFile <> INVALID_HANDLE_VALUE Then
    'move file pointer
    SetFilePointer sFile, 0, ByVal 0, FILE_BEGIN

    Do
    'check if user cancel copy
    If mvariCancel Then
    GoTo CLoseFiles
    End If

    CurPos = SetFilePointer(sFile, 0, ByVal 0, FILE_CURRENT)

    If Left(CStr(CurPos), 1) = "-" Then
    CurPos = TotalFileSize - Abs(CurPos)
    If Form1.Text3 = "" Then Form1.Text3 = CurPos
    End If
    CurPosOn = CurPos / 100
    Totalto = TotalFileSize / 100
    mvarPercentREady = (CurPosOn * 100) / Totalto
    Debug.Print CurPos, CurPosOn, Totalto, mvarPercentREady

    DoEvents

    If TotalFileSize - CurPos < ReadChunkSize& Then
    ReadChunkSize& = TotalFileSize - CurPos ' get to the end of the file
    BufferSize& = ReadChunkSize& * 2
    End If

    r$ = String$(ReadChunkSize&, Chr$(0))
    retval = ReadFileNO(sFile, ByVal r$, Len(r$), iEOF, 0)
    Buffer$ = Buffer$ + r$

    If Len(Buffer$) >= BufferSize& Then
    WriteFile dFile, ByVal Buffer$, Len(Buffer$), bytesWritten, ByVal 0&
    Buffer$ = ""
    End If

    Loop Until iEOF = 0

    End If

    CLoseFiles:
    CloseHandle sFile
    CloseHandle dFile

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Copy over 4GB files with progress problem.

    If you can't even make a Long pointer past 4GB, how do you think you can do it? What do you do when you run out of fingers?
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Dec 2011
    Posts
    4

    Re: Copy over 4GB files with progress problem.

    Can you please give me a solution i dont realy know what to do ((

  4. #4
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Copy over 4GB files with progress problem.

    FSO is not the way to go for sure.

    I have not tried this personally but a simple binary access routine should work just fine on any file size supported by the OS/File System.

    Basically something like this.

    Open the file source file for binary read
    Open Target file for Binary write
    Do ' until eof
    Get chunk from source
    Put chunk to target
    loop
    close files

    You should also be able to call a windows API function to copy a file with the windows progress dialog shown as well.
    Last edited by DataMiser; December 28th, 2011 at 10:27 PM.
    Always use [code][/code] tags when posting code.

  5. #5
    Join Date
    Dec 2011
    Posts
    4

    Re: Copy over 4GB files with progress problem.

    Quote Originally Posted by DataMiser View Post
    FSO is not the way to go for sure.

    I have not tried this personally but a simple binary access routine should work just fine on any file size supported by the OS/File System.

    Basically something like this.

    Open the file source file for binary read
    Open Target file for Binary write
    Do ' until eof
    Get chunk from source
    Put chunk to target
    loop
    close files

    You should also be able to call a windows API function to copy a file with the windows progress dialog shown as well.

    i have try that already but geting error on Loc(infil) with big files

    infil = FreeFile
    Open SourceFile For Binary Access Read Lock Write As #infil
    outfil = FreeFile '
    Open Destination For Binary Access Write Lock Write As #outfil



    Dim Buffer$

    Do 'While Not Loc(infil) = LOF(infil)

    If mvariCancel Then
    GoTo CLoseFiles
    End If

    r$ = String$(ReadChunkSize&, Chr$(0))
    Get #infil, , r$
    Buffer$ = Buffer$ + r$

    If Len(Buffer$) >= BufferSize& Then
    Put #outfil, , Buffer$
    Buffer$ = ""
    End If

    CurPosOn = Loc(outfil) / 100
    Totalto = TotalFileSize / 100
    mvarPercentREady = (CurPosOn * 100) / Totalto

    DoEvents

    If TotalFileSize - Loc(infil) < ReadChunkSize& Then
    ReadChunkSize& = TotalFileSize - Loc(infil)
    End If

    Loop Until EOF(infil)

    If Len(Buffer$) <> 0 Then
    Put #outfil, , Buffer$
    End If

    Close #infil, #outfil

  6. #6
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Copy over 4GB files with progress problem.

    Both Lof and Loc result in a long which has a max value of 2,147,483,647. You would get an error if you try to use one of these functions on a file containing more bytes that this var can hold.

    Have a look here
    http://www.codeguru.com/cpp/w-d/doc_...cle.php/c12917
    and here
    http://www.vbforums.com/showthread.php?t=531321
    Always use [code][/code] tags when posting code.

  7. #7
    Join Date
    Dec 2011
    Posts
    4

    Re: Copy over 4GB files with progress problem.

    I found the solution thanks for replies!
    Last edited by ALANi2k11; December 29th, 2011 at 02:36 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured