Compare the File names in the Directory

Hi All,

I struck with one issue.

for example let us consider I have the files more then 1000 in a folder and I need to compare the Files with their names.

Like the every file name should be compared with the input file name receiving and if the file name matches then that’s a duplicate file otherwise that’s a non-duplicate file.

Kindly help with the any LINQ query for this issue.

Thanks
Sreenivasa

Assign activity:
inputFileName = “The name of the file you are comparing against”

Assign activity:
allFiles = Directory.GetFiles(“YourFolderPath”)

Assign activity:
duplicateFiles = allFiles.Where(Function(f) Path.GetFileName(f).Equals(inputFileName)).ToList()

Assign activity:
nonDuplicateFiles = allFiles.Where(Function(f) Not Path.GetFileName(f).Equals(inputFileName)).ToList()

Note:
Read All File Names : Use Directory.GetFiles(FolderPath)
Compare File Names : Use a LINQ query to compare each file name against the input file name to identify duplicates.

hey @srinusoft37
duplicates:
Directory.GetFiles(folderPath).Where(Function(f) Path.GetFileName(f).Equals(inputFileName)).ToArray()

non-duplicates:
Directory.GetFiles(folderPath).Where(Function(f) Not Path.GetFileName(f).Equals(inputFileName)).ToArray()

@srinusoft37

inputFileName = "your_input_file_name_here.pdf" ' Change this to your input file name

duplicateFiles = Directory.GetFiles("your_folder_path_here")
                          .Where(Function(filePath) Path.GetFileName(filePath) = inputFileName)
                          .ToArray()

Hi @srinusoft37

Try the below syntax:

uniqueFileNames = Directory.GetFiles("C:\Your\Folder\Path").Select(Function(filePath) Path.GetFileName(filePath)).Distinct().ToArray()

uniqueFileNames is of DataType Array(System.String)

Hope it helps!!

Hi @srinusoft37

You can use the LinQ Expressions to get the file is already exist in the Directory.

Store the Input file name in a Variable called InputFile it is String Variable.

- Assign -> BoolFlag = System.IO.Directory.GetFiles("C:\Users\mkankatala\Downloads").AsEnumerable.any(Function(X) System.IO.Path.GetFileNameWithoutExtension(X.ToString).Equals(InputFile))

Note - BoolFlag is the Boolean Datatype Variable
Change the Directory path in the above expression.

The BoolFlag variable gives the True if file contains the directory, it gives false if file doesn’t contain in the directory.

Hope it helps!!

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.