Delete duplicate files in folders

Good day to all

I have a process that generates some files that are saved in folder 1 I have to validate if the files generated are any that exist in folder 2, if this is true I have to delete them and only pass the files that are not repeated in any of the two folders to generate folder 3

I appreciate if anyone can help me with this issue.

  1. Extract Folder1 file names into a List say LstFolder1

  2. Extract Folder2 file names into a List say ListFolder2

  3. For Each item in LstFolder1
       If LstFolder2.Contains(item) Then
            Delete file - path of folder 2 + item
        End If
      End For
    

Regards,
Karthik Byggari

3 Likes

Files_2 corresponde a la ruta del folder 2

1 Like

Hi @Sebasthian_Arias,
Try using as item.ToString

Thanks!

The path goes as I indicated? —> Files_2 + item

@Sebasthian_Arias Please try Files_2+item.ToString

Hi
You were almost done
The sequence be like this
—use two assign activity like this
Files1 = array of files in folder 1
Files2 = array of files in folder 2
—use another two assign activity like this
list_var = Files1.ToList()
list_other = Files2.ToList()
—now use a FOR EACH activity and pass the above variable list_other as input and change the type argument as string in the property panel of for each loop
—inside the loop use a IF condition like this
list_var.Contains(item.ToString)
If true it will go to THEN part where use a DELETE activity and mention like this
item.ToString
THUS MATCHING file and its file path will get deleted in variable list_other

Now next to this for each loop use a CREATE DIRECTORY ACTIVITY and create a folder with a folder path
—now use a FOR EACH LOOP and pass the variable list_other as input and change the type argument as string in the property panel of for each loop
—inside the loop use MOVE FILE activity where in the path mention as item.ToString and in the destination property mention the folder path of new folder created with CREATE DIRECTORY activity

Cheers @Sebasthian_Arias

I think it would be faster and easier if you just move all files from folder 1 to 3 and after all files from folder 2 to 3 using OVERWRITE as True…

It is not viable since folder 1 has the history of the files that are in an FTP

As the file names are already Stored in list, the calculations can even realized with the Set functions (e.g union, except etc). So WE can quickly calculates such delete or move Liste as well and very directly

Perform all the steps you indicated @Palaniyappan .

It didn’t work the files still appear

You dont need foreach to find the files, only to delete them, see if this examples help you get the idea:

Dim A() As String = {"Hello", "How", "Are", "You?"}
Dim B() As String = {"You", "How", "Something Else", "You"}

Console.WriteLine("A elements not in B: " + String.Join(", ", A.Except(B)))
Console.WriteLine("B elements not in A: " + String.Join(", ", B.Except(A)))
Console.WriteLine("Elements in both A & B: " + String.Join(", ", A.Intersect(B)))

*** EDIT
Also i need to remind you that you are using full paths to compare the files, so they will never be the same :slight_smile:

It is not deleting the files is because it does not perform the if, someone who can help me with the subject please

I debuted and found that the item that uses the for each has only “/” instead list_other has “//” I don’t know if this affects when making the comparison

image
image

Item of

image

list_other

image

Hi @Sebasthian_Arias

Wasn’t sure if you intend to delete from folders 1 and 2 but try something like this.

folder1 = directory.GetFiles("folder1").toList
folder2 = directory.GetFiles("folder2").toList
folder2path = path.GetDirectoryName("folder2")

For Each file In folder1
	
	checkFolder2File = folder2path & "\" & path.GetFileName(file)
	
	If folder2.contains(checkFolder2File) Then
		delete file
		delete checkFolder2File
		Else
		copy file To folder 3
	End If
	
Next

When you use the For Each activity, make sure you change the type from Object to String in the properties.

May not be exactly what you’re looking for but you’ll get the idea.

Cheers!

Troy

I generated two cycles one to read the files in folder 1 and another one to read folder 2. I split the routes and obtained the name of the file in a String. Now to make the comparison I use an Equals to validate if applicable or not with the file in the other folder and I delete the file PAth: item.ToString. Note: I remove the break to continue the cycle until it goes through all the files and eliminates the duplicates.

Thanks to everyone they helped me a lot with the subject.

image

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