View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.misc
Randi R Randi R is offline
external usenet poster
 
Posts: 1
Default How to find and open the latest modified file


Kam

I have one folder, which has 20 files &
I want macro to open last modified
file in that folder. Is this can be done??

File Path: C:\Users\Glenys\Desktop\Glen_Macro\



Not a macro, but a script. I have added comments so you can transfer the logic to a macro.




# Script OpenLast.txt
var str list, file, latestfile, latesttime
# Go to the desired folder.
cd "C:\Users\Glenys\Desktop\Glen_Macro"
# Collect a list of files
lf -r -n "*" ($ftype=="f") $list
# Go thru files one by one, checking mod time of each.
while ($list < "")
do
# Get the next file from the list.
lex "1" $list $file
# Get mod time.
af $file null
# Mod time is now in $fmtime. Is it later than $latesttimg ?
if ($fmtime $latesttime)
do
# Yes, save this as the latest file.
set $latestfile = $file
set $latesttime = $fmtime
done
endif
done
# We now have the latest modified file in $latestfile. Open it.
system start ("\""+$latestfile+"\"")





Above script is in biterscripting ( http://www.biterscripting.com ). To try it before you make it into a macro, save the script in file C:/Scripts/OpenLast.txt, enter the following command in biterscripting.



script "C:/Scripts/OpenLast.txt"








Kam wrote:

Open Last Modified File form Location
09-Dec-09

Hi,

I have one folder, which has 20 files & I want macro to open last modified
file in that folder. Is this can be done??

File Path: C:\Users\Glenys\Desktop\Glen_Macro\

Best Regards,
Kam.

Previous Posts In This Thread:

On Wednesday, December 09, 2009 5:58 AM
Kam wrote:

Open Last Modified File form Location
Hi,

I have one folder, which has 20 files & I want macro to open last modified
file in that folder. Is this can be done??

File Path: C:\Users\Glenys\Desktop\Glen_Macro\

Best Regards,
Kam.

On Wednesday, December 09, 2009 6:51 AM
Jacob Skaria wrote:

One way using Dir()..
One way using Dir()..

Sub LastModifiedFilewithinFolder()

Dim strFile As String, strFolder As String
Dim dtLast As Date, strLMFile As String

strFolder = "C:\"
'strFolder = "C:\Users\Glenys\Desktop\Glen_Macro\"
strFile = Dir("c:\*.*", vbNormal)
Do While strFile < ""
If FileDateTime(strFolder & strFile) dtLast Then
dtLast = FileDateTime(strFolder & strFile)
strLMFile = strFolder & strFile
End If
strFile = Dir
Loop

MsgBox "Last Modified file is : " & strLMFile

End Sub


--
Jacob


"Kam" wrote:

On Wednesday, December 09, 2009 9:45 AM
????? (????) ????? wrote:

In order to OPEN(!
In order to OPEN(!) the last modified file he will need the command listed below:
Workbooks.Open (strLMFile)
Micky


"Jacob Skaria" wrote:

On Wednesday, December 09, 2009 12:26 PM
????? (????) ????? wrote:

A slightly another approach:Sub OpenLastModifiedFilewithinFolder()On Error
A slightly another approach:
Sub OpenLastModifiedFilewithinFolder()
On Error Resume Next
With Application.FileSearch
..LookIn = "C:" : .Filename = "*.XLS*"
If .Execute(SortBy:=msoSortByFileName, SortOrder:=msoSortOrderAscending)
For FF = 1 To .FoundFiles.Count
If FileDateTime(.FoundFiles(FF)) LastModDate Then
LastModDate = FileDateTime(.FoundFiles(FF))
LMF = .FoundFiles(FF)
End If
Next
End If
End With
Workbooks.Open (LMF)
End Sub
============
Micky


"?????????? (????????) ??????????" wrote:


Submitted via EggHeadCafe - Software Developer Portal of Choice
Extending the DataAdapter with a Helper Class
http://www.eggheadcafe.com/tutorials...taadapter.aspx