Copy directory with ProgressBar and BackgroundWorker

Projekt do pobrania: CopyDirectory_with_ProgressBar_andBackgrW

Kopiowanie całych folderów z Progressbarem jest nieco trudniejsze niż kopiowanie tylko jednego pliku. Aby użyć File stream’a do kopiowania plików należy zbadać folder w celu ich odnalezienia i określić ile sub folderów znajduje się wewnątrz folderu, następnie zbadać znalezione subfoldery w poszukiwaniu plików i kolejnych subfolderów, trzeba również zachować hierarchie folderów i plików. Stworzyłem algorytm który spełnia te kryteria. Jego opis publikuje poniżej:

drawingNajpierw ułuż elementy tak jak na obrazku poniżej

CopyDirectory_with_ProgressBar_andBackgrW_1Następnir dodajemy BackgroundWorker i FolderBrowser x2 do naszej formy

CopyDirectory_with_ProgressBar_andBackgrW_2Jeśli wszystko poszło dobrze zobaczymy je w polu pod formą

CopyDirectory_with_ProgressBar_andBackgrW_3Zmieniamy tekst poszczególnych elementów tak jak na obrazku

CopyDirectory_with_ProgressBar_andBackgrW_6Uzupełniamy kod:

 

 

 

Permalink do tego artykułu: https://visualmonsters.cba.pl/copy-directory-with-progressbar-and-backgroundworker/

10 Komentarzy

Skip to comment form

    • Musyanon on 22 października 2016 at 18:31
    • Odpowiedz

    Nice work ! It’s possible to converte to move folder instead of copy ?

    1. Using this loop you can’t do that. Becouse you don’t manipulate main file, you just read and copy file by byte. FIlestream can’t delete main file. You can add comand „System.IO.File.Delete( FileToDelete )” to the loop, when file has been copied then it will delete it.

  1. how to copy hidden files with this?

    thanks.

    1. you can check the file attribute using:
      If Not fileSystemInfo.Attributes = Nothing AndAlso fileSystemInfo.Attributes = System.IO.FileAttributes.Hidden Then …
      or check directory attribute using the same loop.

  2. Nice Job!!

    How to select multiple folders?

    Thanks

    1. Hello,
      I do not quite understand what you want to do
      Try to list all directories using:
      For Each Dir As String In Directory.GetDirectories(„your directory”)
      ” Copy metod
      Next
      and call copy method for each folders

  3. Fantastic Work, Impressive

    Please tell me how do I copy only selected file formats from the source folder to destination folders using your above code, Sometimes 2 destination folders

    Ex:
    Source Folder have *.dat, *.abq, *.zas, *.slp, *.log, *.zipp, *.mat and *.mno files,
    I want to copy only *.abq, *.dat, *.log, and *.mno files to Destination folder ( Sometimes 2 destination folders)

    Thank You

    1. Hi,
      Thank you for the comment 😀
      first you need to define an Array, this will help you easyli manage all extensions:
      Dim extensions As String() = {„.abq”, „.dat”, „.log”}
      then put a logic below the line 83
      83: If TypeOf fileSystemInfo Is System.IO.FileInfo Then
      84: If extensions.Contains(fileSystemInfo.Extension) Then
      85: …..
      That should do the work 😉
      But remember to change the file count logic to set the progressbar properly

  4. HI Sir,

    I just started learning programing and your code helps me a lot. I was only wondering how to add estimated remaining time and transfer rate of the copying files.

    Thank you in advance

    • Baz Kelly on 23 stycznia 2022 at 01:56
    • Odpowiedz

    Hi Admin,

    Thank you, this is awesome. I have customised this after going through the code. However there is one thing which stands out for me, yes this copys every folder (directory) perfectly, but It doesn’t copy Documents (My Documents) directory, yes it copys individual folder within it, but it does copy all of the folder (Directory). With out folders (Directory) no problem. Why not all of Documents (My Documents).

    Thank you inadvance Baz

Dodaj komentarz

Twój adres email nie będzie publikowany.