Imports System.IO

Module Purge

    Public Sub Purge(PathToPurge As String, FilesToKeepNum As Integer, FileType As String, subdir As Boolean, copyonly As Boolean, TempTxtFile As String)
        Console.WriteLine(".........")
        Console.WriteLine("Folder: " + PathToPurge)
        My.Computer.FileSystem.WriteAllText(TempTxtFile, Environment.NewLine + ".........", True)

        My.Computer.FileSystem.WriteAllText(TempTxtFile, Environment.NewLine + "Folder: " + PathToPurge, True)


        Dim files() As String
        Dim pathss As String = PathToPurge
        '   Dim FileType As String = ".prt"


        files = Directory.GetFiles(pathss, "*" & FileType & "*", SearchOption.TopDirectoryOnly)
        Dim NumberOfFilesDeleted As String = 0
        Dim FirstCharacter As Integer
        Dim FirstCharacter_2 As Integer
        Dim SameFiles As New List(Of String)
        Dim SameFileExtentensions As New List(Of Integer)
        Dim FileExtention As New List(Of String)
        Dim FilesToKeep As New List(Of String)
        Dim FilesToRename As New List(Of String)
        Dim FilesToDelete As New List(Of String)

        'Fileok megszámolása
        Dim FileCount As Integer = files.Count



        For i = 0 To FileCount - 1


                '  Console.WriteLine(FileType)
                FirstCharacter = files(i).ToLower.IndexOf(FileType)
                '   FirstCharacter_2 = files(i - 1).ToLower.IndexOf(FileType)
                If i <> 0 Then
                    FirstCharacter_2 = files(i - 1).ToLower.IndexOf(FileType)
                    '     Console.WriteLine(files(i))
                    '     Console.WriteLine(FirstCharacter)
                    '     Console.WriteLine(Strings.Left(files(i), FirstCharacter))

                    '     Console.WriteLine(Strings.Left(files(i - 1), FirstCharacter))
                    '       Console.WriteLine(i)


                    If Strings.Left(files(i), FirstCharacter) <> Strings.Left(files(i - 1), FirstCharacter_2) Then
                        '   Console.WriteLine(files(i))
                        SameFiles.Add(files(i))

                    Else
                        'IDE AZT HA 


                    End If

                Else
                    SameFiles.Add(files(i))
                End If

            Next

        '     Console.WriteLine(SameFiles.Count)
        Dim EachSamefiles() As String
        For Each SameFile As String In SameFiles

            Dim samefilesname As String = Path.GetFileNameWithoutExtension(SameFile)
            Console.WriteLine(samefilesname)
            EachSamefiles = Directory.GetFiles(pathss, samefilesname & "*.*", SearchOption.TopDirectoryOnly)
            FirstCharacter = SameFile.LastIndexOf(FileType)
            ' If Left(SameFile, 108) = Left("Z:\Projekt-Rajzok\SHM-8001...SHM-8100\SHM-8020 - Umbau für G25 Interim\02 - Modellek\shm-9999-010-222-222-01", 108) Then
            Try
                For Each EachSameFile As String In EachSamefiles

                    SameFileExtentensions.Add(CInt(Mid(EachSameFile, FirstCharacter + 6)))

                Next
            Catch ex As Exception

            End Try


            SameFileExtentensions.Sort()
            SameFileExtentensions.Reverse()


            For i = 0 To SameFileExtentensions.Count - 1

                If i < FilesToKeepNum Then

                    FilesToRename.Add(Strings.Left(SameFile, FirstCharacter) & FileType & "." & CStr(SameFileExtentensions(i)))

                Else
                    FilesToDelete.Add(Strings.Left(SameFile, FirstCharacter) & FileType & "." & CStr(SameFileExtentensions(i)))

                End If

            Next


            FilesToRename.Reverse()


            For Each asd In FilesToDelete
                My.Computer.FileSystem.DeleteFile(asd)
                NumberOfFilesDeleted = NumberOfFilesDeleted + 1

                My.Computer.FileSystem.WriteAllText(TempTxtFile, Environment.NewLine + "File deleted:" + asd, True)
                Console.WriteLine("File deleted: " + asd)
                ' Console.WriteLine(asd)
            Next


            For Each asd In FilesToRename

                FirstCharacter = asd.ToLower.IndexOf(FileType)
                'Console.WriteLine(asd)
                My.Computer.FileSystem.WriteAllText(TempTxtFile, Environment.NewLine + "File renamed: " + asd + "to: " + Strings.Left(asd, FirstCharacter + 5) & FilesToRename.IndexOf(asd) + 1, True)
                Console.WriteLine("File renamed: " + asd + "to: " + Strings.Left(asd, FirstCharacter + 5) & FilesToRename.IndexOf(asd) + 1)
                'Console.WriteLine(Strings.Left(asd, FirstCharacter + 5) & FilesToRename.IndexOf(asd) + 1)
                If asd <> Strings.Left(asd, FirstCharacter + 5) & FilesToRename.IndexOf(asd) + 1 Then

                    My.Computer.FileSystem.RenameFile(asd, Path.GetFileName(Strings.Left(asd, FirstCharacter + 5) & FilesToRename.IndexOf(asd) + 1))

                End If


            Next

            FilesToRename.Clear()
            FilesToDelete.Clear()
            SameFileExtentensions.Clear()

        Next

        SameFiles.Clear()
        Console.WriteLine("...")
    End Sub

End Module
