Results 1 to 2 of 2
  1. #1
    New Lounger
    Join Date
    Nov 2004
    Location
    Sacramento, California, USA
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It's been a while and I can't remember the code - but also, someone may have a better solution. I have a list of text files that are named with a "D" and then the date (i.e D20090123, for January 23, 2009). I thought I could set up a do/loop to open the files, then break the file names down to year/month/day and create logic to open only the newest file. Do you know of a better way? So:
    1) Can you give me the code for a do/loop to open all the files (preferably limited to text, there are mixed files); or
    2) Is there a way to determine the most recent file without listing all the file names and just open the newest file?

    Your help is appreciated.

  2. #2
    Silver Lounger
    Join Date
    Jul 2001
    Location
    Ottawa, Ontario, Canada
    Posts
    1,609
    Thanks
    0
    Thanked 1 Time in 1 Post
    [quote name='dsnybnd' post='773818' date='05-May-2009 13:36']2) Is there a way to determine the most recent file without listing all the file names and just open the newest file?[/quote]
    The attached code will find the most recent txt file for you.
    Code:
    Sub OpenFile()
       
       'Set this constant to the directory of interest.
       Const DirSpec = "D:\Trash\"
       
       Dim strFile As String
       Dim LastFile As String
       Dim Saved As Date
       Dim LastSaved As Date
       
    	   strFile = Dir(DirSpec & "*.txt")
    	   Do While strFile <> ""
    		   Saved = FileDateTime(DirSpec & strFile)
    		   If Saved > LastSaved Then
    			   LastFile = strFile
    		   End If
    		   strFile = Dir
    	   Loop
    	   
    	   If LastFile <> "" Then
    		   'Insert code here to do what you wish with the file (DirSpec & LastFile).
    	   End If
       End Sub
    Regards
    Don

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •