The approach is as follows:
fileName with “11SP”’
filePath with the folder path which contains the files
reqFile variable will hold the path of required file.
Explanation of code : check if the
filePath (e.g., TestFolder) contains the file starting with
fileName (“11SP”). If No, then assign
reqFile as Nothing. If yes, then assign the latest modified file’s path to the
reqFile. So if there are two files with their name starting with the “11SP” then only the latest modified file’s will be assigned to the
reqFile variable. This is as per your requirement (Output 2 files available, now need to compare with file modification date then get latest file)
reqFile is Nothing, it means that there is no file matching the requirement (i.e Starting with
fileName “11SP”). Nothing here means that the output is empty as you stated in your problem above.
reqFile is Nothing then send the mail
Even this code should also work
Where(Function (f) Path.GetFileName(f.ToString).StartsWith(fileName)).Count.Equals(0),
Where(Function (f) Path.GetFileName(f.ToString).StartsWith(fileName)).
OrderBy(Function (x) File.GetLastWriteTime(x)).Last
Suppose in the Folder C:\TestFolder there are 5 files in total. Out of these 5 files, 3 files have their name start with “11SP”. See below image.
Let us run the process on this folder and see the output
C:\TestFolder\11SP_3.txt files is picked.
The reqFile contains this path now
For your reference
Compare two file then get latest file and email.xaml (7.5 KB)