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

Find most recent file in a directory - how to?

Status
Not open for further replies.

katbear

Programmer
Mar 14, 2007
270
US
Hi,

I am working with SQL Server Integration Services, which uses vb.net in script tasks. However, I am not a vb.net programmer, but I can make my way around when I have to.

What I'd like to do is find a sample script that loops through the files in a directory and finds the *most recent* file, as given by the file name. Then it just saves the file name to a variable.

My files names look like this:
20070531myFile.csv
20070530myFile.csv

Very simple, I would think. Can anyone please provide an example or link that can show me what to do? If I can see the basic logic, I can modify it for my purposes.

Thanks very much
 
Check out the FileSystemObject in scrrun.dll (I think). In VB.Net the System.IO.DirectoryInfo exposes a Files collection (which accepts wild cards) which you can iterate. Something along these lines and Good Luck!
Code:
Imports System.IO
Dim maxFile As String
For Each file As FileInfo In New DirectoryInfo("YourPath").Files
    If file.Name > maxFile Then maxFile = file.Name
Next file
 
Yesterday I found some code that was similar to yours and managed to figure it out.

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top