segunda-feira, 7 de novembro de 2011

on


Imports System.IO

Public Class Form1

    ' File information
    Public Const TOOL_DIR_NAMEONLY As Integer = 0
    Public Const TOOL_DIR_SIZE As Integer = 1
    Public Const TOOL_DIR_MODIFIED As Integer = 2
    Public Const TOOL_DIR_FULLPATH As Integer = 4
    Public Const TOOL_DIR_DIRECTORIES_ONLY As Integer = 8
    Public Const TOOL_DIR_ALL As Integer = 15

    ''' <summary>
    ''' Searches for files that match ány of the file masks in the FileMask array
    ''' </summary>
    ''' <param name="RootPath">Path to start search from</param>
    ''' <param name="FileMask">An array of filemasks</param>
    ''' <param name="FileNames">Returns matching files</param>
    ''' <param name="RecurseDirs">Recurse subdirectories too</param>
    ''' <param name="ExcludeRootPath">Strip start path away from the file names</param>
    ''' <param name="MinDate">Search files newer than MinDate</param>
    ''' <param name="MaxDate">Search files older than MaxDate</param>
    ''' <param name="MinSize">Search files larger than MinSize</param>
    ''' <param name="MaxSize">Search files smaller than MaxSize</param>
    ''' <param name="FileInfo">Optional parameter which tells if some extra info should be returned with the filenames. Use constant values above.</param>
    ''' <remarks></remarks>
    Public Sub ToolDir(ByVal RootPath As String, ByVal FileMask() As String, _
      ByRef FileNames() As String, _
      ByVal RecurseDirs As Boolean, _
      ByVal ExcludeRootPath As Boolean, _
      ByVal MinDate As Date, ByVal MaxDate As Date, _
      ByVal MinSize As Integer, ByVal MaxSize As Integer, _
      Optional ByVal FileInfo As Integer = TOOL_DIR_NAMEONLY)
        '
        ' Return files
        '
        Dim DirInfo As DirectoryInfo
        Dim Files() As FileInfo
        Dim OneFile As FileInfo
        Dim TempDrive As String
        Dim TempPath As String
        Dim TempFile As String
        Dim TempExt As String
        Dim CheckMinDate As Boolean
        Dim CheckMaxDate As Boolean
        Dim CheckMinSize As Boolean
        Dim CheckMaxSize As Boolean
        Dim AcceptFile As Boolean
        Dim RootPathLength As Integer
        Dim i As Integer
        Dim j As Integer

        Try
            If MinDate <> CDate("1.1.1970") Then
                CheckMinDate = True
            Else
                CheckMinDate = False
            End If
            If MaxDate <> CDate("1.1.1970") Then
                CheckMaxDate = True
            Else
                CheckMaxDate = False
            End If
            If MinSize > 0 Then
                CheckMinSize = True
            Else
                CheckMinSize = False
            End If
            If MaxSize > 0 Then
                CheckMaxSize = True
            Else
                CheckMaxSize = False
            End If
            If FileMask.GetUpperBound(0) < 0 Then
                ReDim FileMask(0)
                FileMask(0) = "*.*"
            End If
            i = 0
            TempDrive = ""
            TempPath = ""
            TempFile = ""
            TempExt = ""
            RootPathLength = RootPath.Length + 1
            For j = 0 To FileMask.GetUpperBound(0)
                ReDim Files(0)
                If RecurseDirs Then
                    DirInfo = New DirectoryInfo(RootPath)
                    Files = DirInfo.GetFiles(FileMask(j), IO.SearchOption.AllDirectories)
                Else
                    DirInfo = New DirectoryInfo(RootPath)
                    Files = DirInfo.GetFiles(FileMask(j), IO.SearchOption.TopDirectoryOnly)
                End If

                For Each OneFile In Files
                    AcceptFile = True
                    If CheckMinDate Then
                        If OneFile.LastAccessTime.Date.Subtract(MinDate).Days < 0 Then
                            AcceptFile = False
                        End If
                    End If
                    If CheckMaxDate Then
                        If OneFile.LastAccessTime.Date.Subtract(MaxDate).Days > 0 Then
                            AcceptFile = False
                        End If
                    End If
                    If CheckMinSize Then
                        If OneFile.Length < MinSize * 1024 Then
                            AcceptFile = False
                        End If
                    End If
                    If CheckMaxSize Then
                        If OneFile.Length > MaxSize * 1024 Then
                            AcceptFile = False
                        End If
                    End If
                    If AcceptFile Then
                        ReDim Preserve FileNames(i)
                        If (FileInfo And TOOL_DIR_FULLPATH) = TOOL_DIR_FULLPATH Then
                            If ExcludeRootPath Then
                                FileNames(i) = OneFile.FullName.Substring( _
                                  RootPathLength, OneFile.FullName.Length - RootPathLength)
                            Else
                                FileNames(i) = OneFile.FullName
                            End If
                        Else
                            FileNames(i) = OneFile.Name
                        End If
                        If (FileInfo And TOOL_DIR_SIZE) = TOOL_DIR_SIZE Then
                            FileNames(i) = FileNames(i) & Convert.ToChar(9) & OneFile.Length
                        End If
                        If (FileInfo And TOOL_DIR_MODIFIED) = TOOL_DIR_MODIFIED Then
                            FileNames(i) = FileNames(i) & Convert.ToChar(9) & OneFile.LastAccessTime
                        End If
                        i += 1
                    End If
                Next
            Next j
        Catch ex As Exception
            ' Some error
        End Try

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim FileNames(0) As String

        ' procura todos ospng-arquivos na C:\Temp pasta
        ToolDir("C:\Temp\", New String() {"*.png"}, FileNames, True, False, CDate("1.1.1970"), CDate("1.1.1970"), 0, 0)

        For Each fn As String In FileNames
            MessageBox.Show(fn)
        Next
    End Sub
End Class


0 comentários:

Postar um comentário