How to check Filename in two folders?

Hello I’m new hear,

How do I check a Filename in two folders if it’s the same or not?

Hi @fniedez

Use For each file in folder it has a filter condition is it and see if the file is returned or not

cheers

Hi @fniedez ,

Can you try this,

for example file name is str = “Test”

1.Get folder 1 files name as Array

folder1Files = Directory.GetFiles(“Your directory”)

folder1FilesName = (from x in folder1Files.AsEnumerable Select path.GetFileNameWithoutExtension(x)).ToArray

2.Get folder 2 files name as Array

folder2Files = Directory.GetFiles(“Your directory”)

folder2FilesName = (from x in folder2Files.AsEnumerable Select path.GetFileNameWithoutExtension(x)).ToArray

3.folder1FilesName.contains(str)
4.folder2FilesName.contains(str)

You will get Boolean True/False for both

Thanks!

Hello @Umadevi_Sanjeevi

Could you please elaborate your answer, I can’t even understand on what you are saying, I’m new in UiPath.

Thanks,
Ally

Hello @Anil_G

I don’t even understand on what you are saying. How can I even know the result?

This is the example scenario:

・A file with the same name exists in both zip files
->Prints the file name and the changes (line number and contents).

・A specific file exists in only one zip file
->Print the filename and which zip file it resides in.

And this is the example result that I need:

[Given zip file]
->BeforeExample.zip, AfterExample.zip

filter_message.cfg
before_change(line:1) : #test1
after_change :

filter_time.cfg
before_change(line:6) : #test2
after_change (line:6) : #test3

correlation_order.txt
This file only exists in AfterExample.zip

Note: if you need my files just message me

Thanks,
France

@fniedez

What you asked initially is to check two folders if they have same file name or not…but now I guess you have so many other things added…

Isn’t this the same thing that you are asking again?

And if you are unable to understand I would suggest to go through the academy videos first to get some basic understanding

cheers

Hi @fniedez ,

For example have to check file name str = “Test” is available for both folder1 and folder2?

Folder1 have some files like Test.xlsx,Test1.xlsx,Test2.doc

Folder2 have some files like File1.xlsx,File2.doc,Test.xlsx

so both folder has “Test”

  1. Get files on folder1

folder1Files = Directory.GetFiles(“Your directory”) data type is Array of String

2.Get file names of folder1

folder1FilesName = (from x in folder1Files.AsEnumerable Select path.GetFileNameWithoutExtension(x)).ToArray data type is Array of String

Same like folder2.

  1. Get files on folder2

folder2Files = Directory.GetFiles(“Your directory”) data type is Array of String

2.Get file names of folder2

folder2FilesName = (from x in folder2Files.AsEnumerable Select path.GetFileNameWithoutExtension(x)).ToArray data type is Array of String

Now you have file names for both folder1 and folder2 as array variables(folder1FilesName,folder2FilesName).

You can use if activity

folder1FilesName.Contains(str) AND folder2FilesName.Contains(str)

For our scenario “Test” is available for both file. So will get TRUE for above condition.

Thanks

@Anil_G

That’s the same thing but in your example, you didn’t check “if” all the Filenames inside on the zip files BeforeExample and AfterExample are equally same. Therefore, if have a Filenames are not equally then use throw activity.

@fniedez

Did you happen to notice this if condition at all?

cheers

@Anil_G

I need to print also the BeforeExample file. In your example I can’t see the folder of BeforeExample. All the output names are AfterExample.

@fniedez

then change the loop other way round…in the loop use second folder and in directory.getfiles use first

You need to change as per the requirement

Or do directory.Getfiles on both folders and use

arr1.Except(arr2)

This will give list of all non matched records

cheers

@fniedez

Yes …do this assign for both the folders and get the two arrays then use

aar1.Except(arr2) - this will give not present in arr2 and present in arr1
arr2.Except(arr1) - this will give not present in arr1 and present in arr2

cheers

2 Likes

@Anil_G

Thanks for your help but I didn’t catch your point I faced a lot of complication in my program.

Thanks,
France

@fniedez

That is because you have limited understanding on UI Path and the activities …once you are familiar you will understand better.

Here…Directory.Getfiles will list all the files in the given directory as a array

SO what you want is to check which files are present and which are not present…

So I asked you to get both the arrays and in arrays there is a except method which will compare two arrays and ill give the values that are not matching as an output array…so that is what you need as per the files

cheers