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

Determine the oldest file

Status
Not open for further replies.

bertieuk

IS-IT--Management
Jun 1, 2004
175
Hi

I am writing a file sync vbs script which is almost complete now. The only two issues I seem to have are:

1) I want to count the number of files in a folder
2) I want to find the oldest file in a folder

I currently do the first as follows:

i = 0
For Each file in strTargetPath.Files
i = i + 1
Next

Is there a better way of doing it?

Thanks
Si
 
Hello bertieuk,

[1] Use count property, like this.
Code:
    oTargetPath.files.count
I put oTargetPath here which is most probably your strTargetPath. You know, strTargetPath is really an folder object and not a string.

[2] If no other uses of determining the oldest file in terms of lastmodifieddate other than the single indicators of the "oldest", then the simplest is to use brutforce as it is inevitable you need to loop through the files collection.
Code:
set f_oldest=nothing : dt_oldest=null
for each f in oTargetPath.files
    dtfdlm=f.datelastmodified
    if isnull(dt_oldest) then
        dt_oldest=dtfdlm
        set f_oldest=f
    else
        if dtfdlm<dt_oldest then
            dt_oldest=dtfdlm
            set f_oldest=f
        end if
    end
next
'at this stage you have dt_oldest the oldest datelastmodifed
'and object f_older being the oldest file object for further manipulation
If you need other statistics like last-but-one oldest etc etc, then it would be worth considering to build a disconnected recordset and sort it with datelastmodified.

regards - tsuji
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top