Using VBS to copy a folder?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • slenish
    Contributor
    • Feb 2010
    • 283

    #1

    Using VBS to copy a folder?

    Hello everyone,

    I hope I am posting this question in the write forum. I looked through all the other ones and this seemed like the best place. If not i appologize in advance.

    I have a code that I wrote in VBS to copy contents from one folder to another. I made it so that it wont overwrite the folder if it already exists, but now i want to expand on this. I want to make it so that it will copy folders from one to the other but it will update the contents inside the folder. If a file already exisits in the folder dont overwrite but if a file does not exisit then add that file.

    Appreciate any help with this

    thanks!

    Here is what i have so far.
    Code:
    Option Explicit
    Dim strSourceDir, strDestinDir, FSO
    const OverwriteExisting = False
    
    strSourceDir = "I:\TestFolder1\*.*"
    strDestinDir = "I:\TestFolder2\"
    
    on error resume next
    set FSO = CreateObject("Scripting.FileSystemObject") 
    FSO.CopyFolder strSourceDir , strDestinDir , OverWriteExisting
  • Guido Geurs
    Recognized Expert Contributor
    • Oct 2009
    • 767

    #2
    This code checks if a file exists in the destination folder.
    if not, it copys the file.

    PS: you have to add the Reference "Microsoft scripting runtime" for the definition of the type "File" and "Folder" and "FileSystemObje ct" !

    Code:
    Private Sub Command1_Click()
    Dim strDestinDir As String
    Dim FSO As New FileSystemObject
    Dim THE_FILE As File
    Dim START_FOLDER As Folder
       strDestinDir = "c:\xfer\"
       Set START_FOLDER = FSO.GetFolder("c:\temp\")
       On Error Resume Next
       For Each THE_FILE In START_FOLDER.Files
          If FSO.FileExists(strDestinDir & Mid(THE_FILE, InStrRev(THE_FILE, "\") + 1)) Then
             MsgBox ("File exist")
          Else
             FSO.CopyFile THE_FILE, strDestinDir
          End If
       Next
    End Sub

    Comment

    • slenish
      Contributor
      • Feb 2010
      • 283

      #3
      Hi ggeu,

      Sorry it took me a while to write back to you on this. I have been working on other things and I just got back around to working on this again.

      Appreciate the help on this. I took what you changed and pluged it in and so far im having no luck. I made some adjustments and it will still copy the folder if it does not exist but if a folder is missing a file it still will not copy the new files in to the folder. Any ideas?

      Thanks again helping me with this!

      Code:
      Option Explicit 
      Dim strSourceDir, strDestinDir, FSO 
      Dim THE_FILE  
      Dim START_FOLDER 
      
      THE_FILE = ".txt"
      START_FOLDER = "*.Folder"
      
      
      const OverwriteExisting = False 
        
      strSourceDir = "I:\TestFolder1\*.*" 
      strDestinDir = "I:\TestFolder2\" 
        
      on error resume next 
      set FSO = CreateObject("Scripting.FileSystemObject")  
      FSO.CopyFolder strSourceDir , strDestinDir , OverWriteExisting
      
      For Each THE_FILE In START_FOLDER.Files 
            If FSO.FileExists(strDestinDir & Mid(THE_FILE, InStrRev(THE_FILE, "\") + 1)) Then 
               MsgBox ("File exist") 
            Else 
               FSO.CopyFile THE_FILE, strDestinDir 
            End If 
         Next

      Comment

      • Guido Geurs
        Recognized Expert Contributor
        • Oct 2009
        • 767

        #4
        I have a question: has the main folder also sub-folders? or are there only files?

        If there are only files, then we can use my code.

        If there are also sub-folders, then we have to analyze each folder and there files because the function "copyfolder " copy's only FOLDERS and it's CONTENTS in block.

        The const "OverwriteExist ing" checks only if the FOLDER exists !!

        If the main folder exists and OverwriteExisti ng=False then no sub-folders or files will be copied

        The VB help is:

        "overwrite: Optional. Boolean value that indicates if existing folders are to be overwritten. If True, files are overwritten; if False, they are not. The default is True. "

        Comment

        • Guido Geurs
          Recognized Expert Contributor
          • Oct 2009
          • 767

          #5
          If there are subfolders: this is the code to check each subfolder and file (see attachment).

          how it works:

          command1 calls the function for checking the sourcedir on files and subfolders.

          - if there are files, it checks if the files exists in the destindir.
          If not, it copies the file.
          - if there are subfolders: it checks if the subfolder exists in the destindir, if not, it will be created.
          AND it calls the SAME function for all the files and subfolders in this subfolder.
          Attached Files

          Comment

          • slenish
            Contributor
            • Feb 2010
            • 283

            #6
            Hi ggeu,

            I keep coming back around to this just takes me a little bit. Well to let you know there are sub folders in the main folder, then in the sub folders are all of the files. So when i used the orginal code I wrote it would just copy all the sub folders from the main folder to another folder and all the files, but if a file in one of the sub folders was say updated, deleted or added, it would not copy this change over.

            I am looking at the files you had in the zip, wow, that is a lot of stuff. Thank you very much for putting that together. Only question i have now is how does it work?? I cant seem to get it to work so far.

            Thanks again :)

            Comment

            • Guido Geurs
              Recognized Expert Contributor
              • Oct 2009
              • 767

              #7
              I hope this attached Doc or PDF will help you in understanding the workflow.
              Attached Files

              Comment

              Working...