+ Reply to Thread
Results 1 to 2 of 2

Open Latest TextFile with latest TimeStamp

Hybrid View

  1. #1
    Forum Contributor pr4t3ek's Avatar
    Join Date
    10-13-2008
    Location
    Melbourne, Australia
    MS-Off Ver
    2003 & 2007, 2010
    Posts
    483

    Open Latest TextFile with latest TimeStamp

    Hi,

    An application saves a text file in a local folder with a specific textfile name
    The text in the textfiles are exactly the same, they just vary in figures.

    e.g. of textfile names in the folder & timestamps
    RS00A1012009 17/02/2009 5:50PM
    RS00B1012009 17/02/2009 7:09PM
    RS00C1012009 17/02/2009 7:15PM
    RS00A1001009 18/02/2009 5:00AM
    RS00B1001009 18/02/2009 6:00AM

    Now what i would like a macro to do is import the "latest text file" from the above list which has the text "A" in it... so therefore the textfile i would like to open in the above instance is:
    RS00A1001009 18/02/2009 5:00AM

    I would therefore then like it to be opened in the following code fashion
        Workbooks.OpenText Filename:= _
            "C:\Documents and Settings\L487698\Desktop\THELATESTFILE.txt", Origin:=-535, _
            StartRow:=1, DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, 1), Array(19 _
            , 1), Array(29, 1)), TrailingMinusNumbers:=True
    Where the "THELATESTFILE" is the file i want to open as suggested above...

    hope someone can help, thanks
    --
    Regards
    PD

    ----- Don't Forget -----

    1. Use code tags. Place "[code]" before the first line of code and "[/code"]" after the last line of code. Exclude quotation marks

    2. Thank those who have helped you by Clicking the scales above each post.

    3. Please mark your post [SOLVED] if it has been answered satisfactorily.

  2. #2
    Forum Contributor
    Join Date
    03-25-2008
    MS-Off Ver
    Excel, Outlook, Word 2007/2003
    Posts
    245

    Re: Open Latest TextFile with latest TimeStamp

    Not tested but maybe something like this :
    Sub check_on_dir()
    'RS00A1012009 17/02/2009 5:50PM
    'RS00B1012009 17/02/2009 7:09PM
    'RS00C1012009 17/02/2009 7:15PM
    'RS00A1001009 18/02/2009 5:00AM
    'RS00B1001009 18/02/2009 6:00AM
    Const mydir As String = "C:\Data\"
    Dim myfile As String
    Dim mydate As Date
    Dim mystrdate
    Dim resultfile As String
    
    mydate = DateSerial(1990, 1, 1)
    myfile = Dir(mydir & "*.txt")
    Do While myfile <> vbNullString
        If Left(myfile, 5) = "A" Then
            mystrdate = Split(Split(myfile, " ")(1), "/")
            If DateSerial(mystrdate(2), mystrdate(1), mystrdate(0)) > mydate Then
                mydate = DateSerial(mystrdate(2), mystrdate(1), mystrdate(0))
                resultfile = myfile
            End If
        End If
        myfile = Dir
    Loop
    Workbooks.OpenText Filename:= _
        "C:\Documents and Settings\L487698\Desktop\" & resultfile, Origin:=-535, _
        StartRow:=1, DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, 1), Array(19 _
        , 1), Array(29, 1)), TrailingMinusNumbers:=True
    End Sub
    Charlize

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1