VB code to Copy a Folder to another Location

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AR Ratheesh
    New Member
    • Jul 2009
    • 25

    VB code to Copy a Folder to another Location

    Dear all,
    How to copy files or folders from one location to another.
    Thank you in advance..
  • Guido Geurs
    Recognized Expert Contributor
    • Oct 2009
    • 767

    #2
    Dear,

    You can copy, move, ... files and folders with FileCopy, Foldercopy,... with a FileSystemObjec t.

    example: copies files from a list to a path entered in a textbox
    =============== =============== =============== =
    Code:
    Private Declare Function CopyFile Lib "kernel32" Alias "CopyFileA" _
       (ByVal lpExistingFileName As String, _
       ByVal lpNewFileName As String, ByVal _
       bFailIfExists As Long) As Long
    -------------------------------------------------------------------------------------------
    Code:
    Private Sub Com_save_gekozene_Click()
    Dim i As Integer
    Dim TO_FILE As String
    Dim fso As New FileSystemObject
    '§ folder exist?
       If Not fso.FolderExists(Text_to_path.Text) Then
          MsgBox ("The folder don't exist!")
          Exit Sub
       End If
       With List_gekozene
          For i = 0 To .ListCount - 1
             TO_FILE = Text_to_path.Text & "\" & Mid(.List(i), InStrRev(.List(i), "\") + 1)
    '§ file exist ?
             If fso.FileExists(TO_FILE) Then
                MsgBox TO_FILE & " file exist !"
             Else
                fso.CopyFile .List(i), TO_FILE
                Label_status_copy.Caption = TO_FILE & " = GECOPIEERD"
                DoEvents
             End If
          Next
       End With
       Label_status_copy.Caption = ""
    End Sub
    Last edited by debasisdas; Oct 13 '09, 12:00 PM. Reason: Formatted using code tags

    Comment

    • Guido Geurs
      Recognized Expert Contributor
      • Oct 2009
      • 767

      #3
      dear,

      You don't need to declare the function if You add the "Microsoft Scripting Runtime" reference to your project !

      Comment

      • smartchap
        New Member
        • Dec 2007
        • 236

        #4
        May have a look at the following link:

        Comment

        • Crystaltech
          New Member
          • Jul 2007
          • 6

          #5
          .. May be this will help you TO COPY FILE OR FOLDER

          Dim txtdbpath As String 'this is your database file
          Dim txtdbpathcopy As String 'this is your copy path
          Dim intResponse, fs
          ' the following double checks, Are you sure?
          intResponse = MsgBox("Are you sure that you wish to make a copy of your file now?", vbYesNo + vbDefaultButton 1 + vbQuestion, "Crystal Technosoft")
          If (intResponse = 6) Then
          Set fs = CreateObject("S cripting.FileSy stemObject")
          ' txtdbpath = frmMain.cdgDial og.FileName
          ' txtdbpath = "D:\ABC.mdb "
          txtdbpath = App.Path & "\" & "ABC.mdb"
          txtdbpathcopy = "D:\"
          fs.CopyFile txtdbpath, txtdbpathcopy, True
          MsgBox "Done Successfully", vbInformation, "Crystal Technosoft"
          Else
          MsgBox "Not Done", vbCritical, "Crystal Technosoft"
          End If

          Comment

          • AR Ratheesh
            New Member
            • Jul 2009
            • 25

            #6
            Thank you for all.
            Rathesh

            Comment

            Working...