How to find and delete duplicate files on desktop and keep only one file

Mostafa Salaheldien 61 Reputation points
2021-06-09T17:37:38.037+00:00

hello everyone,
I would like to know how to find and delete duplicate files on the desktop and keep only one file
thanks in advanced

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,623 questions
{count} votes

Accepted answer
  1. Xingyu Zhao-MSFT 5,356 Reputation points
    2021-06-11T06:29:46.55+00:00

    Hi @Mostafa Salaheldien ,
    Take a look at the following example.

        Dim groups = (New DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory))).  
            GetFiles("*.rdp").GroupBy(Function(file) file.Name).Where(Function(group) group.Count() > 1)  
        For Each group In groups  
            Dim needDelete As Boolean = False  
            For Each file In group  
                ' Delete all deplicate files except the first one  
                If needDelete Then  
                    file.Delete()  
                End If  
                needDelete = True  
            Next  
        Next  
    

    The code will delete all the deplicate rdp files with the same name on the desktop.

    Best Regards,
    Xingyu Zhao
    *
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Karen Payne MVP 35,286 Reputation points
    2021-06-09T18:36:15.893+00:00

    More information is needed as per @Viorel but to get started with listing for file names use the following.

    Public Shared Sub GetDesktopFiles()  
    	Dim fileInfos = (New DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory))).  
    			GetFiles().Concat((New DirectoryInfo(  
    				Environment.GetFolderPath(Environment.SpecialFolder.CommonDesktopDirectory))).  
    								 GetFiles()).OrderBy(Function(item) item.Name)  
      
    	For Each file In fileInfos  
    		Debug.WriteLine(file.Name)  
    	Next  
    End Sub