Solved

How do I copy files created on a selected date?

Posted on 2006-06-10
4
171 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

932 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now