Praveen V
Praveen V

Reputation: 11

copy the files with creation date range using VBS (in Sub folder files also)

I tried with this link copy files between a specified date range but i am able to copying only root directory please any one help me

Upvotes: 1

Views: 9840

Answers (1)

HK1
HK1

Reputation: 12210

Here you go. Please note that this can potentially create empty directories because it will create a directory and then check to see if each file falls within the specified date range or not. If no files do, the directory will remain empty.

Obviously, you can comment out or delete the WScript.Echo lines. They are for troubleshooting only.

Option Explicit
dim objFSO, strSource, strTarget

set objFSO = CreateObject("Scripting.FileSystemObject")
strSource = "c:\Folder1\"
strTarget = "c:\Copy of Folder1\"

call RecurseCopy(strSource, strTarget, True, #04/15/2012 00:00:01 AM#, #04/16/2012 00:00:01 AM#)

' // Recursively copy all files and folders
Sub RecurseCopy(strSource, strTarget, blnCopySubfolders, dBeginDate, dEndDate)
    dim objSource, objTarget

    WScript.Echo "Begin RecurseCopy" & vbcrlf & vbcrlf & _
            "strSource: " & strSource & vbcrlf & _
                "strTarget: " & strTarget

    set objSource = objFSO.GetFolder(strSource)

    If objFSO.FolderExists(strTarget) = False Then
        Wscript.Echo "Now going to create folder: " & strTarget
        objFSO.CreateFolder(strTarget)
    End If

    set objTarget = objFSO.GetFolder(strTarget)

    Dim file
    for each file in objSource.files
        If file.DateCreated => dBeginDate AND file.DateCreated =< dEndDate Then
            Wscript.Echo "Copying file: " & file.path & " to " & objTarget.Path
            file.Copy objTarget.Path & "\" & file.name
        Else
            WScript.Echo "File will not be copied because the DateCreated is not within the specified range." & vbcrlf & vbcrlf & _
                        File.Path & " " & file.DateCreated
        End If
    next

    If blnCopySubfolders = True Then
        ' ** For each subfolder of current dir, copy files to target and recurse its subdirs
        Dim subdir
        for each subdir in objSource.subfolders
            call RecurseCopy(objSource.Path & "\" & subdir.Name, objTarget.Path & "\" & subdir.Name, True, dBeginDate, dEndDate)
        Next
    End If

End Sub

Upvotes: 3

Related Questions