3

So I am trying to do something in visual basic I start learning but still that is not enough. Mostly I am using codes from internet. Now I want to copy few files from first folder to second folder and overwrite existing files and I want to see progress on progress bar (all files together are about 2GB)

SOLVED: I found source code for some program and used some parts to make this work

tshepang
  • 12,111
  • 21
  • 91
  • 136
user1959879
  • 43
  • 1
  • 4
  • here is the question u need explained and answered http://stackoverflow.com/questions/1299721/progress-bar-and-file-copying-problem – Alfarem Jan 09 '13 at 02:01
  • Hello. This question is redundant and lacks a specific question. Please look at question posting guidelines. – Shrey Gupta Jan 09 '13 at 02:19

1 Answers1

12

Here is my favorite way of doing it... Using the SHFileOperation API

This API will automatically show the progress as shown in the screenshot below.

Here is an example. Paste this code in a module

Public Declare Function SHFileOperation Lib "shell32.dll" _
Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long

Public Const FO_COPY = &H2
Public Const FOF_SIMPLEPROGRESS = &H100

Public Type SHFILEOPSTRUCT
    hWnd As Long
    wFunc As Long
    pFrom As String
    pTo As String
    fFlags As Integer
    fAnyOperationsAborted As Long
    hNameMappings As Long
    lpszProgressTitle As Long
End Type

Public Sub VBCopyFolder(ByRef strSource As String, ByRef strTarget As String)
    Dim op As SHFILEOPSTRUCT

    With op
        .wFunc = FO_COPY
        .pTo = strTarget
        .pFrom = strSource
        .fFlags = FOF_SIMPLEPROGRESS
    End With

    '~~> Perform operation
    SHFileOperation op
End Sub

and then copy files or folders like this

Private Sub Sample()
    '~~> Copy Files
    Call VBCopyFolder("C:\Sample.Avi", "C:\NewSample.Avi")

    '~~> Copy Folders
    Call VBCopyFolder("C:\Temp1", "C:\Temp2")
End Sub

Screenshot

enter image description here

Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
  • tnx for answer but what I really want is to see progress on progress bar on application which I made – user1959879 Jan 09 '13 at 14:37
  • Would something like this help? http://stackoverflow.com/questions/10782394/pop-up-the-excel-statusbar/10787496#10787496 – Siddharth Rout Jan 09 '13 at 15:22
  • 6 years later and a follow up question...`Public Const FO_Copy = &H2` what is that doing? the &H2 part? – Forward Ed May 30 '19 at 16:51
  • I was trying to use this to copy a file to a sharepoint address and I got an error at `.flags = FOF_SIMPLEPROGRESS`. It say compile error: Variable not defined and then it has FOF_SIMPLEPROGRESS highlighted. Do I need a reference library installed? is this a VB or VBA answer? as you can see I am a tad confused. – Forward Ed May 30 '19 at 17:31
  • @ForwardEd: My apologies. I have added the missing line in the code. Now when you say VB it refers to Visual Basic (VB.Net) But the code that I gave above is for VBA (Visual Basic for Applications) and VB6 (Visual Basic 6). For VB.Net, the code will have to be tweaked. Also I have not tested this with sharepoint address. – Siddharth Rout May 30 '19 at 17:58
  • I was looking for a VBA solution so this sounds fine. I will be testing it shortly! – Forward Ed May 30 '19 at 18:32
  • FYI it works for copying to sharepoint. my problem is I am copying file by file in a loop and that means I have to OK each individual file (They previously exist...only a couple of hundred being over written. I suppose if I was copying the folder I would get the option of apply selection to all. Is there a way to bypass the replace existing dialogue box....you know what this is a follow up question. I will post shortly – Forward Ed May 30 '19 at 18:44
  • 2
    You need to use the `FOF_NOCONFIRMATION` flag. :) Declare `Public Const FOF_NOCONFIRMATION As Long = &H10` at the top and change `.fFlags = FOF_SIMPLEPROGRESS` to `.fFlags = FOF_SIMPLEPROGRESS Or FOF_NOCONFIRMATION` @ForwardEd – Siddharth Rout May 30 '19 at 19:03
  • Works! To a degree. I posted a [follow up Q](https://stackoverflow.com/questions/56384342/copy-file-with-progress-bar-bypass-file-replacement-confirmation) where I show how I am calling the routine, and describe the issue of it not showing for subsequent files being copied. – Forward Ed May 30 '19 at 19:37