Solved

How do I copy files created on a selected date?

Posted on 2006-06-10
4
173 Views
Last Modified: 2010-04-30
I would like to have a script that prompts a user for a date, then copies files from c:\backup  ("ST*.txt" & "LR*.txt") created on the user supplied date to c:\logs_win\wms_data

Ideally, the user could type in the date, or click on an icon to bring up a calendar date selector.
 
Thanks,

Mike
0
Comment
Question by:spellm
  • 2
  • 2
4 Comments
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 250 total points
ID: 16877107
Assuming you set a reference to Microsoft Scripting Runtime...



Sub CopyFiles()

    Dim TheDate As Date
    Dim fso As Scripting.FileSystemObject
    Dim SourceFld As Scripting.Folder
    Dim fil As Scripting.File
   
    Const DestFldPath As String = "c:\logs_win\wms_data\"

    TheDate = InputBox("Enter creation date", "File Date", Format(Now, "d mmm yyyy"))
   
    Set fso = New Scripting.FileSystemObject
    Set SourceFld = fso.GetFolder("c:\backup")
    Set DestFld = fso.GetFolder("c:\logs_win\wms_data")
   
    For Each fil In SourceFld.Files
        If UCase(fil.Name) Like "ST*.TXT" Or UCase(fil.Name) Like "LR*.TXT" Then
            If Format(TheDate, "d mmm yyyy") = Format(fil.DateCreated, "d mmm yyyy") Then
                fil.Copy DestFldPath & fil.Name
            End If
        End If
    Next
   
    Set fil = Nothing
    Set SourceFld = Nothing
    Set fso = Nothing
   
    MsgBox "Done"
   
End Sub
0
 

Author Comment

by:spellm
ID: 16892638
How would I display a count of the number of files that were copied?
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 16892950
Like this:

Sub CopyFiles()

    Dim TheDate As Date
    Dim fso As Scripting.FileSystemObject
    Dim SourceFld As Scripting.Folder
    Dim fil As Scripting.File
    Dim Counter As Long

    Const DestFldPath As String = "c:\logs_win\wms_data\"

    TheDate = InputBox("Enter creation date", "File Date", Format(Now, "d mmm yyyy"))
   
    Set fso = New Scripting.FileSystemObject
    Set SourceFld = fso.GetFolder("c:\backup")
    Set DestFld = fso.GetFolder("c:\logs_win\wms_data")
   
    For Each fil In SourceFld.Files
        If UCase(fil.Name) Like "ST*.TXT" Or UCase(fil.Name) Like "LR*.TXT" Then
            If Format(TheDate, "d mmm yyyy") = Format(fil.DateCreated, "d mmm yyyy") Then
                fil.Copy DestFldPath & fil.Name
                Counter = Counter + 1
            End If
        End If
    Next
   
    Set fil = Nothing
    Set SourceFld = Nothing
    Set fso = Nothing
   
    MsgBox "Done; copied " & Counter & " files"
   
End Sub


Patrick
0
 

Author Comment

by:spellm
ID: 16895422
Great!  Thanks a lot.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question