Compare two file then get latest file and email

Hi,

My file name like that

1.11SP
2.11SP1 or SP2 ……
3.12SP
4.13SP

Now need to compare both file then copy the latest file.

1 assign(File name )= “11SP”
2 locations = file path

For each loop = directory.get file ( file path)

If condition

Item.to string.startswith(file name)

Then

Out put
11 SP
11 SP1

Now I need your help

  1. Output 2 files available,now need to compare with file modification date then get latest file

  2. If file not match output empty then need to send email

Guys any one please help me.

@krishna_annadurai

This can be done as follows

If(
	Directory.
	GetFiles(filePath).
	Where(Function (f) Path.GetFileName(f.ToString).StartsWith(fileName)).
	OrderBy(Function (x) File.GetLastWriteTime(x)).Count.Equals(0),
	_
	Nothing,
	_
	Directory.
	GetFiles(filePath).
	Where(Function (f) Path.GetFileName(f.ToString).StartsWith(fileName)).
	OrderBy(Function (x) File.GetLastWriteTime(x)).Last
	)

For Mail Part

image

Hi @krishna_annadurai

You mean how to get the newest file from folder?
To do that watch this video:

Best regards
Mahmoud

Sorry brother it’s not like that

Hi sorry brother I con’t understand …

@krishna_annadurai

The approach is as follows:

  1. Assign fileName with “11SP”’

  2. Assign filePath with the folder path which contains the files

  3. 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)

    • If 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.

  4. If reqFile is Nothing then send the mail

Even this code should also work

If(
	Directory.
	GetFiles(filePath).
	Where(Function (f) Path.GetFileName(f.ToString).StartsWith(fileName)).Count.Equals(0),
	_
	Nothing,
	_
	Directory.
	GetFiles(filePath).
	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

image

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)