Solved

How do I copy files created on a selected date?

Posted on 2006-06-10
4
170 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
Comment Utility
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
Comment Utility
How would I display a count of the number of files that were copied?
0
 
LVL 92

Expert Comment

by:Patrick Matthews
Comment Utility
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
Comment Utility
Great!  Thanks a lot.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

744 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

17 Experts available now in Live!

Get 1:1 Help Now