Class Copy Directory

Status
Niet open voor verdere reacties.

bonnowagt

Gebruiker
Lid geworden
7 dec 2006
Berichten
445
Hallo,


De bedoeling is om een hoofddirectory en subdirectory te kopieren naar een ander directory, dus als voorbeeld:

Video(hoofddir)---films(subdir) titels van films(files)
Video----films-----de film.avi

Op formulier1 heb ik en button om het kopieren te starten.
Verder heb ik een class zie hieronder:

Code:
Imports System
Imports System.IO

Class DirectoryCopyExample

    Shared Sub Main()
        ' Copy from the current directory, include subdirectories.
        DirectoryCopy(Form1.ListBox1.SelectedItem, Form1.CompleetPad.Text, True)
    End Sub

    Private Shared Sub DirectoryCopy(
        ByVal sourceDirName As String,
        ByVal destDirName As String,
        ByVal copySubDirs As Boolean)

        ' Get the subdirectories for the specified directory.
        Dim dir As DirectoryInfo = New DirectoryInfo(Form1.ListBox1.SelectedItem)

        If Not dir.Exists Then
            Throw New DirectoryNotFoundException(
                "Source directory does not exist or could not be found: " _
                + Form1.ListBox1.SelectedItem)
        End If

        Dim dirs As DirectoryInfo() = dir.GetDirectories()
        ' If the destination directory doesn't exist, create it.
        If Not Directory.Exists(destDirName) Then
            Directory.CreateDirectory(destDirName)
        End If
        ' Get the files in the directory and copy them to the new location.
        Dim files As FileInfo() = dir.GetFiles()
        For Each file In files
            Dim temppath As String = Path.Combine(destDirName, file.Name)
            file.CopyTo(temppath, False)
        Next file

        ' If copying subdirectories, copy them and their contents to new location.
        If copySubDirs Then
            For Each subdir In dirs
                Dim temppath As String = Path.Combine(destDirName, subdir.Name)
                DirectoryCopy(subdir.FullName, temppath, copySubDirs)
            Next subdir
        End If
    End Sub
End Class


Wat plaats ik nu onder de button om deze class te benaderen.
Misschien doe ik het ook allemaal verkeerd en kan het eenvoudiger.
Ik heb hier wel naar gegoogled maar kom alleen hierop uit

Vriendelijke groet
 
je probeert nu de class direct aan te roepen met lokale informatie. dat is natuurlijk niet de bedoeling.

Het is de bedoeling dat je een class maakt die dingen volledig stand-alone uit kan voeren en dan de class aanmaken waar die nodig is of je kunt procedureel werken. In dat geval kun je de code die je nu in "directorycopy" vrijwel geheel in je code plakken. je moet dus kiezen
 
Copy Directorie

Bedankt voor je reactie. Ik het het nu als volgt:

Code:
Public Class Form1
    Dim nummer
    Dim copySubDirs As Boolean
    Dim destDirName As String
    Dim ourceDirName As String
    Private Shared Sub DirectoryCopy(
        ByVal sourceDirName As String,
        ByVal destDirName As String,
        ByVal copySubDirs As Boolean)
    End Sub

Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
 Dim dir As DirectoryInfo = New DirectoryInfo(ListBox1.SelectedItem)
                If Not dir.Exists Then
                    Throw New DirectoryNotFoundException(
                        "Source directory does not exist or could not be found: " _
                        + ListBox1.SelectedItem)
                End If

                destDirName = CompleetPad.Text & "\" & Label10.Text
                Dim dirs As DirectoryInfo() = dir.GetDirectories()
                ' If the destination directory doesn't exist, create it.
                If Not Directory.Exists(destDirName) Then
                    Directory.CreateDirectory(destDirName)
                End If

                ' Get the files in the directory and copy them to the new location.
                Dim files As FileInfo() = dir.GetFiles()
                For Each file In files
                    Dim temppath As String = Path.Combine(destDirName, file.Name)
                    file.CopyTo(temppath, False)
                Next file

                ' If copying subdirectories, copy them and their contents to new location.
                If copySubDirs Then
                    For Each subdir In dirs
                        Dim temppath As String = Path.Combine(destDirName, subdir.Name)
                        DirectoryCopy(subdir.FullName, temppath, copySubDirs)
                    Next subdir
                End If
end sub

Het werkt in zoverre dat hij de hoofddir. en files pakt maar niet de subdirs weergeeft
 
Ik heb inmiddels het volgende gdaan
If copySubDirs = true Then
For Each subdir In dirs
Dim temppath As String = Path.Combine(destDirName, subdir.Name)
DirectoryCopy(subdir.FullName, temppath, copySubDirs)
Next subdir
End If


Dit werkt nog niet
 
ik bedoel meer dat in je huidige code "copysubdirs" nooit true kan worden, omdat die nergens wordt aangezet
 
Ja ik begrijp inmiddels wat je bedoelt.
Ik heb al gezocht maar kom er niet uit heb jij iets voor mij?
 
indien je altijd subdirs wil doen kun je toch gewoon de if weghalen? Anders geef even aan waar je precies vastloopt met je aanpak
 
Ik heb if weggehaald maar probleem blijft hetzelfde.

Waar ik vastloop:

Ik plaats dus in mijn listbox1 c:\foto\picto

Zowel onder foto als onder picto staan files.

Maak doelbestand onder j:\ Kopiefoto aan
CompleedPad.text=J:\Kopiefoto\Backup30-01-2016

Vervolgens gebruik ik dan onderstaande code:
Code:
Dim dir As DirectoryInfo = New DirectoryInfo(ListBox1.SelectedItem)
                If Not dir.Exists Then
                    Throw New DirectoryNotFoundException(
                        "Source directory does not exist or could not be found: " _
                        + ListBox1.SelectedItem)
                End If
                destDirName = CompleetPad.Text
                Dim dirs As DirectoryInfo() = dir.GetDirectories()
                ' If the destination directory doesn't exist, create it.
                If Not Directory.Exists(destDirName) Then
                    Directory.CreateDirectory(destDirName)
                End If
                ' Get the files in the directory and copy them to the new location.
                Dim files As FileInfo() = dir.GetFiles()
                For Each file In files
                    Dim temppath As String = Path.Combine(destDirName, file.Name)
                    file.CopyTo(temppath, False)
                Next file
                ' If copying subdirectories, copy them and their contents to new location.
                ' For Each diSourceSubDir As DirectoryInfo In source.GetDirectories()
                'Dim nextTargetSubDir As DirectoryInfo = target.CreateSubdirectory(diSourceSubDir.Name)
                'CopyAll(ListBox1.SelectedItem, CompleetPad.Text)
                ' Next
           
                For Each subdir In dirs
                    Dim temppath As String = Path.Combine(destDirName, subdir.Name)
                    DirectoryCopy(subdir.FullName, temppath, copySubDirs)
                Next subdir
            End If

Vervolgens krijg ik als uitkomst dat bijvoorbeeld op j:\ krijg

J:\Kopie\backup31-1-2016\ alle files van foto verder niets
 
ik zie net dat het origineel echter een recursieve functie is en dat gaat dan helaas niet werken. (origineel: https://msdn.microsoft.com/en-us/li...aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1 )

Bij een recursieve functie moet je namelijk jezelf aan kunnen roepen. In principe is dat niet moeilijk op te lossen:

kopieer de hele originele functie:

Code:
Private Shared Sub DirectoryCopy( _
        ByVal sourceDirName As String, _
        ByVal destDirName As String, _
        ByVal copySubDirs As Boolean)

        ' Get the subdirectories for the specified directory.
        Dim dir As DirectoryInfo = New DirectoryInfo(sourceDirName)

        If Not dir.Exists Then
            Throw New DirectoryNotFoundException( _
                "Source directory does not exist or could not be found: " _
                + sourceDirName)
        End If

        Dim dirs As DirectoryInfo() = dir.GetDirectories()
        ' If the destination directory doesn't exist, create it.
        If Not Directory.Exists(destDirName) Then
            Directory.CreateDirectory(destDirName)
        End If
        ' Get the files in the directory and copy them to the new location.
        Dim files As FileInfo() = dir.GetFiles()
        For Each file In files
            Dim temppath As String = Path.Combine(destDirName, file.Name)
            file.CopyTo(temppath, False)
        Next file

        ' If copying subdirectories, copy them and their contents to new location.
        If copySubDirs Then
            For Each subdir In dirs
                Dim temppath As String = Path.Combine(destDirName, subdir.Name)
                DirectoryCopy(subdir.FullName, temppath, copySubDirs)
            Next subdir
        End If
    End Sub

en zet die ergens in je class (kan gewoon de de form class zijn) bijvoorbeeld boven of onder de click event die je nu hebt. vervolgens roep je hem in je click event aan:
Code:
DirectoryCopy(Form1.ListBox1.SelectedItem, Form1.CompleetPad.Text, True)

Sorry voor de verwarring, ik had eerder niet gezien dat de oplossing recursief was
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan