user2564753
user2564753

Reputation: 1

Copy folder in visual basic

I'am trying to copy a folder from a textbox a user specify to another location a user specify but this code only copies the file to the destination and not the folder. I'am using Visual Studio 2005.

Here is my code:

    Dim strDate As String
    strDate = DateTime.Now.ToString("yyyy-MM-dd")

    Dim sFolderpath
    Dim dFolderpath
    Dim fs

    fs = CreateObject("Scripting.FileSystemObject")
    sFolderpath = TextBox1.Text
    dFolderpath = TextBox6.Text + "\"
    fs.createfolder(dFolderpath & strDate)
    fs.copyfolder(sFolderpath, dFolderpath & strDate)

It only copies the files to the destination not the folder itself. Value in textbox1 = C:\Test\Test2. Value in textbox6 = K:\Backup

Please help!

Upvotes: 0

Views: 2477

Answers (1)

Idle_Mind
Idle_Mind

Reputation: 39152

It's a weird mix of VB.Net and VBScript...

I ~think~ this is what you're after though:

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim fs As Object = CreateObject("Scripting.FileSystemObject")

    Dim sFolderpath As String = TextBox1.Text
    Dim sourceFolderName As String = System.IO.Path.GetFileName(sFolderpath)

    Dim strDate As String = DateTime.Now.ToString("yyyy-MM-dd")
    Dim dFolderpath As String = System.IO.Path.Combine(TextBox6.Text, strDate)
    fs.createfolder(dFolderpath)
    dFolderpath = System.IO.Path.Combine(dFolderpath, sourceFolderName)
    fs.createfolder(dFolderpath)

    fs.copyfolder(sFolderpath, dFolderpath)
End Sub

You may also be interested in this.

Upvotes: 2

Related Questions