Solved

Check for file during archive - vb from excel

Posted on 2008-06-16
4
137 Views
Last Modified: 2010-05-03
I have a macro which copies files from one directory to another only if the following condition exists:
Costs*.xls.  If there are no files in the source directory, the vb script produces an error message that I do not want the users to see.  Is there any way to display a message box that says there are no files that meet the archive criterion?  I have attached my macro for your review.
Sub ArchiveFiles()
    Dim fso As Object
    
    Dim sSourceDir As String
    Dim sTargetDir As String
    
    sSourceDir = "C:\my documents\"
    sTargetDir = "C:\my documents\backup\"
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    fso.movefile Source:=sSourceDir & "Costs*.xls", Destination:=sTargetDir
    fso.movefile Source:=sSourceDir & "Status*.xls", Destination:=sTargetDir
    
End Sub

Open in new window

0
Comment
Question by:ilrosebud26
  • 3
4 Comments
 
LVL 10

Accepted Solution

by:
peetm earned 125 total points
ID: 21794713
The filesystemobject doesn't have a files collection that uses wildcards as far as I know - so, you'd first have to test using dir$, e.g.,

If Len(Dir$(fso.buildpath(sSourceDir, "Costs*.xls")) > 0 then

   ' do the move.

    fso.movefile Source:=sSourceDir & "Costs*.xls", Destination:=sTargetDir

else

    MsgBox "No matching files"

endif
0
 

Author Comment

by:ilrosebud26
ID: 21794831
Peetm-
    Thanks, I will try this after lunch and let you know.

Rosemary
0
 

Author Comment

by:ilrosebud26
ID: 21797390
That worked great!

Thanks!
Rosemary

0
 

Author Closing Comment

by:ilrosebud26
ID: 31467655
Thanks for getting back so quickly.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
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…

821 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