Module Utils_FileFunctions


    Friend Function GetPath(ByVal str As String) As String
        Try
            Return IO.Path.GetDirectoryName(str) & "\"
        Catch
            Return str
        End Try
    End Function

    Friend Function GetLastFolder(ByVal str As String) As String
        Dim str2 As String = ""
        ' ------------------------------------------------- 
        Try
            str2 = IO.Path.GetDirectoryName(str)
        Catch
        End Try
        ' ------------------------------------------------- se e' di tipo C:\ allora lo lascio passare invariato
        If str2 Is Nothing Then str2 = str
        ' -------------------------------------------------
        Dim i As Int32 = InStrRev(str2, "\")
        If i > 1 Then
            str2 = Mid(str2, i + 1)
        End If
        ' ------------------------------------------------- se diventa una stringa nulla allora uso la stringa completa
        If str2 = "" Then str2 = str
        Return str2
    End Function

    Friend Function GetName(ByVal str As String) As String
        On Error Resume Next
        Return IO.Path.GetFileName(str)
    End Function

    Friend Function FileExists(ByVal fileName As String) As Boolean
        Return My.Computer.FileSystem.FileExists(fileName)
    End Function

    Friend Sub File_KillFile(ByRef FileName As String)
        On Error Resume Next
        If Not My.Computer.FileSystem.FileExists(FileName) Then Exit Sub
        SetAttr(FileName, FileAttribute.Normal)
        Kill(FileName)
    End Sub

    Friend Sub File_CreateEmptyFile(ByVal FilePathAndName As String)
        On Error Resume Next
        File_KillFile(FilePathAndName)
        Dim f As IO.Stream = IO.File.Create(FilePathAndName)
        f.Close()
    End Sub

    Friend Function FolderExists(ByVal FolderName As String) As Boolean
        If FolderName.Length < 2 Then Return False
        FolderName = LCase(FolderName)
        Select Case FolderName
            Case "a:\", "b:\", "c:\", "d:\", "e:\", "f:\", "g:\", "h:\", "i:\", "j:\", "k:\"
                Return True
        End Select
        Return My.Computer.FileSystem.DirectoryExists(FolderName)
    End Function

    Friend Sub TestAndCreateFolder(ByVal FolderName As String)
        On Error Resume Next                    '<-- per supporti read only 
        If Not FolderExists(FolderName) Then
            Dim nPos As Int32
            If Right$(FolderName, 1) <> "\" Then FolderName = FolderName & "\"
            nPos = InStr(1, FolderName, "\")
            Do While nPos > 0
                Dim PartialFolderName As String = Left$(FolderName, nPos)
                If FolderExists(PartialFolderName) Then
                    System.IO.File.SetAttributes(PartialFolderName, IO.FileAttributes.Archive)
                Else
                    My.Computer.FileSystem.CreateDirectory(PartialFolderName)
                End If
                nPos = InStr(nPos + 1, FolderName, "\")
            Loop
        End If
    End Sub

    'Friend Sub Copy_Folder(ByVal SourceFolder As String, _
    '                                   ByVal DestFolder As String, _
    '                                   Optional ByVal sExclude As String = Nothing, _
    '                                   Optional ByVal StartDate As Date = Nothing, _
    '                                   Optional ByVal SearchOpt As IO.SearchOption = IO.SearchOption.AllDirectories)
    '    '
    '    Dim DestSubFolder As String
    '    Dim SourceFile As String
    '    Dim DestFile As String
    '    Dim SourceDate As Date
    '    Dim DestDate As Date
    '    Dim fCopy As Boolean
    '    '
    '    For Each SourceFile In System.IO.Directory.GetFiles(SourceFolder, "*", SearchOpt)
    '        '
    '        ' ---------------------------------------------------------------- the destination subfolder
    '        DestSubFolder = GetPath(SourceFile)
    '        DestSubFolder = Replace(DestSubFolder, SourceFolder, DestFolder, , 1)
    '        'Debug.Print(DestSubFolder)
    '        ' ---------------------------------------------------------------- the destination file name
    '        DestFile = DestSubFolder & GetName(SourceFile)
    '        ' ---------------------------------------------------------------- the source file date
    '        SourceDate = IO.File.GetLastWriteTime(SourceFile)
    '        '
    '        fCopy = True
    '        ' ---------------------------------------------------------------- test EXCLUDE
    '        If sExclude <> Nothing Then
    '            If LCase(SourceFile).Contains(LCase(sExclude)) Then fCopy = False
    '        End If
    '        ' ---------------------------------------------------------------- test if NEWER
    '        If FileExists(DestFile) Then
    '            DestDate = IO.File.GetLastWriteTime(DestFile)
    '            If SourceDate <= DestDate Then fCopy = False
    '        End If
    '        ' ---------------------------------------------------------------- test StartDate
    '        If StartDate <> Nothing Then
    '            If SourceDate <= StartDate Then fCopy = False
    '        End If
    '        ' ---------------------------------------------------------------- if all is valid
    '        If fCopy Then
    '            ' ------------------------------------------------------------ eventually create the folder chain 
    '            TestAndCreateFolder(DestSubFolder)
    '            ' ------------------------------------------------------------ and copy the file
    '            My.Computer.FileSystem.CopyFile(SourceFile, DestFile, True)
    '            'Report(SourceFile, SourceFolder, DestSubFolder)
    '        End If
    '    Next SourceFile
    'End Sub




End Module
