Results 1 to 7 of 7

Thread: Coping file with progress

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2000
    Location
    Europe, Lithuania
    Posts
    309

    Coping file with progress

    Hi,

    How to copy file with showing progress in progressbar?


    Regards

  2. #2
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    U can use the Animated Copy Functions in Windows.

    Sample from the MSDN

    MSDN
    VB Code:
    1. Option Explicit
    2.  
    3. Private Const FO_COPY = &H2&   'Copies the files specified
    4.                                'in the pFrom member to the
    5.                                'location specified in the
    6.                                'pTo member.
    7.  
    8. Private Const FO_DELETE = &H3& 'Deletes the files specified
    9.                                'in pFrom (pTo is ignored.)
    10.  
    11. Private Const FO_MOVE = &H1&   'Moves the files specified
    12.                                'in pFrom to the location
    13.                                'specified in pTo.
    14.  
    15. Private Const FO_RENAME = &H4& 'Renames the files
    16.                                'specified in pFrom.
    17.  
    18. Private Const FOF_ALLOWUNDO = &H40&   'Preserve Undo information.
    19.  
    20. Private Const FOF_CONFIRMMOUSE = &H2& 'Not currently implemented.
    21.  
    22. Private Const FOF_CREATEPROGRESSDLG = &H0& 'handle to the parent
    23.                                            'window for the
    24.                                            'progress dialog box.
    25.  
    26. Private Const FOF_FILESONLY = &H80&        'Perform the operation
    27.                                            'on files only if a
    28.                                            'wildcard file name
    29.                                            '(*.*) is specified.
    30.  
    31. Private Const FOF_MULTIDESTFILES = &H1&    'The pTo member
    32.                                            'specifies multiple
    33.                                            'destination files (one
    34.                                            'for each source file)
    35.                                            'rather than one
    36.                                            'directory where all
    37.                                            'source files are
    38.                                            'to be deposited.
    39.  
    40. Private Const FOF_NOCONFIRMATION = &H10&   'Respond with Yes to
    41.                                            'All for any dialog box
    42.                                            'that is displayed.
    43.  
    44. Private Const FOF_NOCONFIRMMKDIR = &H200&  'Does not confirm the
    45.                                            'creation of a new
    46.                                            'directory if the
    47.                                            'operation requires one
    48.                                            'to be created.
    49.  
    50. Private Const FOF_RENAMEONCOLLISION = &H8& 'Give the file being
    51.                                            'operated on a new name
    52.                                            'in a move, copy, or
    53.                                            'rename operation if a
    54.                                            'file with the target
    55.                                            'name already exists.
    56.  
    57. Private Const FOF_SILENT = &H4&            'Does not display a
    58.                                            'progress dialog box.
    59.  
    60. Private Const FOF_SIMPLEPROGRESS = &H100&  'Displays a progress
    61.                                            'dialog box but does
    62.                                            'not show the
    63.                                            'file names.
    64.  
    65. Private Const FOF_WANTMAPPINGHANDLE = &H20&
    66.                           'If FOF_RENAMEONCOLLISION is specified,
    67.                           'the hNameMappings member will be filled
    68.                           'in if any files were renamed.
    69.  
    70. ' The SHFILOPSTRUCT is not double-word aligned. If no steps are
    71. ' taken, the last 3 variables will not be passed correctly. This
    72. ' has no impact unless the progress title needs to be changed.
    73.  
    74. Private Type SHFILEOPSTRUCT
    75.    hwnd As Long
    76.    wFunc As Long
    77.    pFrom As String
    78.    pTo As String
    79.    fFlags As Integer
    80.    fAnyOperationsAborted As Long
    81.    hNameMappings As Long
    82.    lpszProgressTitle As String
    83. End Type
    84.  
    85. Private Declare Sub CopyMemory Lib "KERNEL32" _
    86.       Alias "RtlMoveMemory" _
    87.       (hpvDest As Any, _
    88.       hpvSource As Any, _
    89.       ByVal cbCopy As Long)
    90.  
    91. Private Declare Function SHFileOperation Lib "Shell32.dll" _
    92.       Alias "SHFileOperationA" _
    93.       (lpFileOp As Any) As Long
    94.  
    95. Private Sub Form_Load()
    96.    Check1.Caption = "Copy All Files in VB Directory"
    97.    Check2.Caption = "Display Custom Message"
    98.    Command1.Caption = "Copy Files"
    99. End Sub
    100.  
    101. Private Sub Command1_Click()
    102.    Dim result As Long
    103.    Dim lenFileop As Long
    104.    Dim foBuf() As Byte
    105.    Dim fileop As SHFILEOPSTRUCT
    106.  
    107.    lenFileop = LenB(fileop)    ' double word alignment increase
    108.    ReDim foBuf(1 To lenFileop) ' the size of the structure.
    109.  
    110.    With fileop
    111.       .hwnd = Me.hwnd
    112.  
    113.       .wFunc = FO_COPY
    114.          
    115.       ' The files to copy separated by Nulls and terminated by two
    116.       ' nulls
    117.       If Check1.Value = vbChecked Then
    118.             .pFrom = Environ("windir") & "\*.exe"
    119.          .fFlags = FOF_SIMPLEPROGRESS Or FOF_FILESONLY
    120.       Else
    121.          .pFrom = Environ("windir") & "\Explorer.exe" _
    122.                   & vbNullChar _
    123.                   & Environ("windir") & "\WinHelp.exe" _
    124.                   & vbNullChar _
    125.                   & vbNullChar
    126.       End If
    127.  
    128.       .pTo = "C:\testfolder\" & vbNullChar & vbNullChar
    129.  
    130.       If Check2.Value = vbChecked Then
    131.          .fFlags = FOF_SIMPLEPROGRESS Or FOF_NOCONFIRMATION Or _
    132.                    FOF_NOCONFIRMMKDIR
    133.          .lpszProgressTitle = "Your custom dialog string " & _
    134.                               "appears here." & vbNullChar _
    135.                                               & vbNullChar
    136.       End If
    137.    End With
    138.  
    139.    ' Now we need to copy the structure into a byte array
    140.    Call CopyMemory(foBuf(1), fileop, lenFileop)
    141.  
    142.    ' Next we move the last 12 bytes by 2 to byte align the data
    143.    Call CopyMemory(foBuf(19), foBuf(21), 12)
    144.    result = SHFileOperation(foBuf(1))
    145.  
    146.    If result <> 0 Then  ' Operation failed
    147.       MsgBox Err.LastDllError 'Show the error returned from
    148.                               'the API.
    149.       Else
    150.       If fileop.fAnyOperationsAborted <> 0 Then
    151.          MsgBox "Operation Failed"
    152.       End If
    153.    End If
    154. End Sub
    -= a peet post =-

  3. #3
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    MSDN

    MORE INFORMATION
    The SHFileOperation function provides the ability to move, rename, or delete file system objects based on the flags passed to the function. The function requires the address of the SHFILEOPSSTRUCT structure. This structure contains the following members:

    hwnd - Window handle to the dialog box to display information about the status of the file operation.


    wFunc - the operation to perform, as defined by one of the following values:


    FO_COPY - Copies the files specified in the pFrom member to the location specified in the pTo member.


    FO_DELETE - Deletes the files specified in pFrom. (pTo is ignored.)


    FO_MOVE - Moves the files specified in pFrom to the location specified in pTo.


    FO_RENAME - Renames the files specified in pFrom.


    pFrom - Address of a buffer to specify one or more source file names. Multiple names must be null-separated. The list of names must be double null-terminated.


    pTo - Address of a buffer to contain the name of the destination file or directory. The buffer can contain multiple destination file names if the fFlags member specifies FOF_MULTIDESTFILES. Multiple names must be null-separated. The list of names must be double null-terminated.


    fFlags - Flags that control the file operation. This member can be a combination of the following flags:


    FOF_ALLOWUNDO - Preserve Undo information, if possible.


    FOF_CONFIRMMOUSE - Not currently implemented.


    FOF_FILESONLY - Perform the operation on files only if a wildcard file name (*.*) is specified.


    FOF_MULTIDESTFILES - The pTo member specifies multiple destination files (one for each source file) rather than one directory where all source files are to be deposited.


    FOF_NOCONFIRMATION - Respond with Yes to All for any dialog box that is displayed.


    FOF_NOCONFIRMMKDIR - Does not confirm the creation of a new directory if the operation requires one to be created.


    FOF_NOERRORUI - No user interface will be displayed if an error occurs.


    FOF_RENAMEONCOLLISION - Give the file being operated on a new name in a move, copy, or rename operation if a file with the target name already exists.


    FOF_SILENT - Does not display a progress dialog box.


    FOF_SIMPLEPROGRESS - Displays a progress dialog box but does not show the file names.


    FOF_WANTMAPPINGHANDLE - If FOF_RENAMEONCOLLISION is specified, the hNameMappings member will be filled in if any files were renamed.


    fAnyOperationsAborted - Value that receives TRUE if the user aborted any file operations before they were completed, or FALSE otherwise.


    hNameMappings - Handle to a file name mapping object that contains an array of SHNAMEMAPPING structures. Each structure contains the old and new path names for each file that was moved, copied, or renamed. This member is used only if the fFlags member includes the FOF_WANTMAPPINGHANDLE flag. The handle must be freed by using the SHFreeNameMappings function.


    lpszProgressTitle - Address of a string to use as the title of a progress dialog box. This member is used only if fFlags includes the FOF_SIMPLEPROGRESS flag.


    NOTE: The 32-bit version of Visual Basic aligns structures at double-word boundaries, but some API functions, such as the SHFileOperation, expect the data to arrive as byte-aligned. If you pass the SHFILEOPSTRUCT structure without taking this double-word boundary into consideration, the fAnyOperationsAborted, hNameMappings, and the lpszProgressTitle members of the structure will not be passed correctly. If you want the copy dialog box to contain a custom message string as defined by the lpszProgressTitle member, use a byte array to work around this limitation. The use of a byte array is demonstrated in the sample project.

    -= a peet post =-

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2000
    Location
    Europe, Lithuania
    Posts
    309
    Thanks, but is any way to get witch files are and witch aren't copied when I copy a list of files?

  5. #5
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    sorry, I thought u wanted the progress on each file.

    If u know how many files to copy, u just set the statusbar max property to that number.

    then increase by one each time u copy a file.
    -= a peet post =-

  6. #6
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344
    Use this code:

    Dim SourceFile, DestinationFile
    SourceFile = App.Path & "\" & "Engmess.txt" ' Define source file name."
    DestinationFile = txtBackUp.Text ' Define target file name.
    FileCopy SourceFile, DestinationFile ' Copy source to target.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2000
    Location
    Europe, Lithuania
    Posts
    309
    Thanks, I found what I was searching for.

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