Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find Newest Files In Directory

Status
Not open for further replies.

2marshall8

Technical User
Jul 29, 2006
18
0
0
US
I am need of finding the newest file within a directory and assigning this to a variable which I can then use to send via email to myself. I found this script on the net which helps in finding the newest file in a specified directory but I'm not able to fix the error that occurs. I know this must be easy but my vbs knowledge isn't upto par like marcdmac's and a few of the rest of ya.

Code:
    Dim fsoFile         'As File
    Dim fsoFolder       'As Folder
    Dim dOldDate        'As Date
    Dim strFileName     'As String
    Dim fso             'As FileSystemObject
    
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fsoFolder = fso.GetFolder("C:\")

    For Each fsoFile In fsoFolder.Files
        Select Case Left$(fsoFile.Name, 10)
            Case "XYZ_123_A-"
                If DateDiff("s", dOldDate,   fsoFile.DateCreated) > 0 Then
                    dOldDate = fsoFile.DateCreated
                    strFileName = fsoFile.Name
                End If
        End Select
    Next

    Set fsoFile = Nothing
    Set fsoFolder = Nothing
    Set fso = Nothing
    


'After loop is done strFileName will hold the newest file in the folder 
'for files beginning with "XYZ_123_A-"
'dOldDate will hold the date it was created

The error I receive is on line 13, character 25. and the reason is Invalid character.

thanks for any help you can provide.
 
>Select Case Left$(fsoFile.Name, 10)
vbs implementation decided to leave out Left$() and use only Left().
[tt]Select Case Left(fsoFile.Name, 10)[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top