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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Comparing files in the same folder

Status
Not open for further replies.

guru533

MIS
Aug 22, 2002
25
US
What is the best way to get all the files in a particular folder and compare the dates on those files? This is what I have so far and this will give me all the filenames and the date they were created:

Function ShowFolders(folderName)
Dim fs, f, f1, fc, s
s = ""
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(folderName)
Set fc = f.Files
For Each f1 in fc
s = s & f1.name & " " & f1.DateCreated
s = s & (Chr(13) & Chr(10))
Next
ShowFolders = s
End Function

What I want to do with this information is to compare the dates on them and return the newest file. Thanks for the help.
 
stick some files in C:\temp and try this

Dim fs, f, f1, fc, s
foldername = "C:\temp"
newest = ""
s = ""
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(folderName)
Set fc = f.Files
For Each f1 in fc
if newfile = "" Then
newfile = f1.name
newest = f1.DateCreated
Else
If Datediff("s",newest,f1.Datecreated) > "0" Then
msgbox f1.name & " " & f1.DateCreated
End If
End If

Next
msgbox newfile & " " & newest Regards
Steve Friday
 
sorry, forgot to take out the message box

Dim fs, f, f1, fc, s
foldername = "C:\temp"
newest = ""
s = ""
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(folderName)
Set fc = f.Files
For Each f1 in fc
if newfile = "" Then
newfile = f1.name
newest = f1.DateCreated
Else
If Datediff("s",newest,f1.Datecreated) > "0" Then
newfile = f1.name
newest = f1.DateCreated
End If
End If

Next
msgbox newfile & " " & newest Regards
Steve Friday
 
From this example, is there a way for me to just search through .exe files and look for the newest file? Thanks for the help so far.
 
Dim fs, f, f1, fc, s
foldername = "C:\temp"
newest = ""
s = ""
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(folderName)
Set fc = f.Files
For Each f1 in fc
If Ucase(Right(f1.name,4)) = ".EXE" Then
if newfile = "" Then
newfile = f1.name
newest = f1.DateCreated
Else
If Datediff("s",newest,f1.Datecreated) > "0" Then
newfile = f1.name
newest = f1.DateCreated
End If
End If
End If

Next
msgbox newfile & " " & newest Regards
Steve Friday
 
Do you know if this would work in an ASP script? Instead of an .EXE, let's say I want a hyperlink to point to a .htm file. Let's say .htm files gets created automatically with the syntax 1test.htm, 2test.htm, etc. into "C:\temp".

Using your script, I'm guessing I could have the script just read *test.htm and grab the newest file. I have never created an ASP script before, but this seems like a good test. Would the HTML page take too long to process because of these scripts? Are these scripts even possible?

Thank you very much for your help sFriday. The scripts have been very helpful.

 
Sorry I do not know enough about ASP to answer this - no doubt someone will pick this thread up and be able to answer your question.

Regards
Steve Friday
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top