Open Last Modified File form Location

K

Kam

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.
 
J

Jacob Skaria

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
 
×

מיכ×ל (מיקי) ×בידן

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

מיכ×ל (מיקי) ×בידן

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
 
R

Randi R

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 modifie
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:

Open Last Modified File form Location
Hi

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

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

Best Regards
Kam.

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

Sub LastModifiedFilewithinFolder(

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

strFolder = "C:\
'strFolder = "C:\Users\Glenys\Desktop\Glen_Macro\
strFile = Dir("c:\*.*", vbNormal
Do While strFile <> "
If FileDateTime(strFolder & strFile) > dtLast The
dtLast = FileDateTime(strFolder & strFile
strLMFile = strFolder & strFil
End I
strFile = Di
Loo

MsgBox "Last Modified file is : " & strLMFil

End Su

-
Jaco

:

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

:

A slightly another approach:Sub OpenLastModifiedFilewithinFolder()On Error
A slightly another approach
Sub OpenLastModifiedFilewithinFolder(
On Error Resume Nex
With Application.FileSearc
..LookIn = "C:" : .Filename = "*.XLS*
If .Execute(SortBy:=msoSortByFileName, SortOrder:=msoSortOrderAscending
For FF = 1 To .FoundFiles.Coun
If FileDateTime(.FoundFiles(FF)) > LastModDate The
LastModDate = FileDateTime(.FoundFiles(FF)
LMF = .FoundFiles(FF
End I
Nex
End I
End Wit
Workbooks.Open (LMF
End Su
===========
Mick

:


Submitted via EggHeadCafe - Software Developer Portal of Choice
Extending the DataAdapter with a Helper Class
http://www.eggheadcafe.com/tutorial...8-d5e53a43f8f1/extending-the-dataadapter.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top