Deleting the same file

Hello

Sometimes I have files that are doubled in my folder. Two files are exactly the same the only difference is name. Can I somehow delete duplicate files based on something else? what would be the best way? based on file size? how to do this?

I have two files with difference names but they are the same and I need to delete one of them

Hi @anon40731888

Follow the below workflow-

  1. Use the “Assign” activity to initialize an empty Dictionary variable, let’s call it “fileGroups.” The dictionary will be used to store file groups based on their size. The key of the dictionary will be the file size, and the value will be a list of file paths that have that size.

    fileGroups = New Dictionary(Of Long, List(Of String))
    
  2. Use the “Directory.GetFiles” activity to get the list of files in the target folder and assign it to the “fileList” variable.

  3. Use a “For Each” activity to iterate through each file path in “fileList.”

  4. Within the “For Each” loop, use the “File.Length” method to get the size of the current file and store it in a variable, let’s call it “fileSize.”

    fileSize = New FileInfo(filePath).Length
    
  5. Add the file path to the appropriate group in the “fileGroups” dictionary based on its size. If the group doesn’t exist, create a new one.

    If Not fileGroups.ContainsKey(fileSize) Then
        fileGroups(fileSize) = New List(Of String)()
    End If
    
    fileGroups(fileSize).Add(filePath)
    
  6. After the “For Each” loop, use another “For Each” activity to iterate through the “fileGroups” dictionary.

  7. Within this loop, check if the current group has more than one file. If it does, it means there are duplicates.

  8. If duplicates are found, you can choose which file to keep (e.g., the first one, the last one) and delete the rest. For example, to delete all except the first file:

    If group.Value.Count > 1 Then
        For Each filePathToDelete In group.Value.Skip(1)
            File.Delete(filePathToDelete)
    

Hope it helps!!

@anon40731888

  1. Get the list of files: Use the “Directory.GetFiles” activity to get a list of all files in the folder you want to check for duplicates. You can provide the folder path as input to this activity.
  2. Create a Dictionary: Create a Dictionary to group files based on their file size. The key of the Dictionary will be the file size, and the value will be a List of file paths that have that file size.
  3. Filter duplicates: Loop through the Dictionary and identify groups of files with the same file size (duplicates). You can use a “For Each” loop to iterate through the Dictionary and check if the List of file paths has more than one entry.
  4. Delete duplicates: Once you identify the duplicate files, keep only one copy of each unique file and delete the rest. To do this, you can use the “Delete File” activity to delete the duplicate files.

Hope it helps!!