none
VBS Script to copy subfolders and files from source to destination RRS feed

  • Question

  • I am trying to create a VBS script to copy certain subfolders and files from one source path to destination path.  Here is the scenario:

    - Source Path: C:\Test1\RegionA\Statements\
    - Destination Path: C:\Test2\RegionA\Statements\

    In the source path, there are 2 subfolders.  Subfolders are …\Names\Years\

    - I need to copy all files within “Year” subfolder where the year is 2014-2017.  I do not need files prior to 2014.

    - The name of the year folder is exactly as the year.  i.e. 2014, 2015, 2016...

    - There are no subfolders in the destination path.  Therefore, subfolders will need to be created if it doesn’t exist.


    I found this script online and modified the file paths.  It stops the when copy the first file because it (looks like) is copying/pasting into the same file path (source path).  Does anyone know what might be the issue or have a better script?  Thanks for your help!!

    -----------------

    Sub CopyFiles(ByVal strPath As String, ByVal strTarget As String)
    Dim FSO As Object
    Dim FileInFromFolder As Object
    Dim FolderInFromFolder As Object
    Dim Fdate As Long
    Dim intSubFolderStartPos As Long
    Dim strFolderName As String

    strPath = "C:\Test1\RegionA\Statements\"
    strTarget = "C:\Test2\RegionA\Statements\"

    Set FSO = CreateObject("scripting.filesystemobject")
    'First loop through files
        For Each FileInFromFolder In FSO.GetFolder(strPath).Files
            Fdate = Int(FileInFromFolder.DateLastModified)
            'If Fdate >= Date - 1 Then
                FileInFromFolder.Copy strTarget
            'end if
        Next

        'Next loop throug folders
        For Each FolderInFromFolder In FSO.GetFolder(strPath).SubFolders
            'intSubFolderStartPos = InStr(1, FolderInFromFolder.Path, strPath)
            'If intSubFolderStartPos = 1 Then

            strFolderName = Right(FolderInFromFolder.Path, Len(FolderInFromFolder.Path) - Len(strPath))
            MkDir strTarget & "\" & strFolderName

            CopyFiles FolderInFromFolder.Path & "\", strTarget & "\" & strFolderName & "\"

        Next 'Folder

    End Sub


    • Moved by Bill_Stewart Wednesday, September 13, 2017 8:59 PM This is not "scripts on demand"
    Friday, July 21, 2017 5:30 PM

All replies