PROCURAR ARQUIVOS CASE SENSITIVE
Tenho uma função que procurar arquivos no computador e tem uma rotina que eu [Ô]pulo[Ô] alguns diretórios, porém
me deparei que [Ô]aparentemente[Ô] ele é [Ô]Case Sensitive[Ô] em relação ao nome do diretório.
De acordo com a Microsoft não é, alguém tem alguma ideia de como eu poderia [Ô]generalizar[Ô] isso?
Rotina resumida:
Dim val() As String = {strProgramFiles.ToString, strProgramFilesX86.ToString }
Dim lst As New List(Of String)
lst.AddRange(val)
If lst.Contains(BaseDirectory.FullName.ToString) = False Then
[ô]MEU CODIGO
End if
ps.; so um chute
Vou testar primeiro.
Em vez de
If lst.Contains(BaseDirectory.FullName.ToString) = False Then
[ô]MEU CODIGO
End if
Use
if (From item In lst Where String.Compare(item, BaseDirectory.FullName) >= 0 Select Item).Any() Then
[ô]MEU CODIGO
End if
Detalhe que se o BaseDirectory for um DirectoryInfo ou FileInfo não precisa do ToString pois o FullName já é uma string
E só por curiosidade, usar essa expressão Linq seria quase o mesmo que fazer isso
Dim encontrado As Boolean
For Each item As String In lst
If String.Compare(item, BaseDirectory.FullName) Then
encontrado = True
Exit For
End If
Next
If encontrado Then
[ô]MEU CODIGO
End If
Vou postar aqui minha classe.
Citação:Option Strict On
Option Explicit On
Imports System.IO
Imports System.Collections.Specialized
Imports System.Configuration
Public Class clsProcuraArquivos
Private Const DefaultFileMask As String = [Ô]*.*[Ô]
Private Const DefaultDirectoryMask As String = [Ô]*[Ô]
#Region [Ô] Member Variables [Ô]
Private _InitialDirectory As DirectoryInfo
Private _DirectoryMasks As StringCollection
Private _FileMasks As StringCollection
[ô]
Private _Directories As New ArrayList
Private _Files As New ArrayList
#End Region
#Region [Ô] Properites [Ô]
Public Property InitialDirectory() As DirectoryInfo
Get
Return _InitialDirectory
End Get
Set(ByVal Value As DirectoryInfo)
_InitialDirectory = Value
End Set
End Property
Public Property DirectoryMask() As StringCollection
Get
Return _DirectoryMasks
End Get
Set(ByVal Value As StringCollection)
_DirectoryMasks = Value
End Set
End Property
Public Property FileMask() As StringCollection
Get
Return _FileMasks
End Get
Set(ByVal Value As StringCollection)
_FileMasks = Value
End Set
End Property
Public ReadOnly Property Directories() As ArrayList
Get
Return _Directories
End Get
End Property
Public ReadOnly Property Files() As ArrayList
Get
Return _Files
End Get
End Property
#End Region
#Region [Ô] Constructors [Ô]
Public Sub New()
End Sub
Public Sub New(ByVal BaseDirectory As String)
Me.New(New DirectoryInfo(BaseDirectory))
End Sub
Public Sub New(ByVal InitialDirectory As DirectoryInfo)
_InitialDirectory = InitialDirectory
End Sub
#End Region
Public Overloads Sub Search(ByVal InitalDirectory As String, Optional ByVal FileMask As String = Nothing, Optional ByVal DirectoryMask As String = Nothing)
Search(New DirectoryInfo(InitalDirectory), FileMask, DirectoryMask)
End Sub
Public Overloads Sub Search(Optional ByVal InitalDirectory As DirectoryInfo = Nothing, Optional ByVal FileMask As String = Nothing, Optional ByVal DirectoryMask As String = Nothing)
_Files = New ArrayList
_Directories = New ArrayList
If Not IsNothing(InitalDirectory) Then
_InitialDirectory = InitalDirectory
End If
If IsNothing(_InitialDirectory) Then
Throw New ArgumentException([Ô]A Directory Must be specified![Ô], [Ô]Directory[Ô])
End If
If IsNothing(FileMask) OrElse FileMask.Length = 0 Then
_FileMasks = New StringCollection
_FileMasks.Add(DefaultFileMask)
Else
_FileMasks = ParseMask(FileMask)
End If
If IsNothing(DirectoryMask) OrElse DirectoryMask.Length > 0 Then
_DirectoryMasks = New StringCollection
_DirectoryMasks.Add(DefaultDirectoryMask)
Else
_DirectoryMasks = ParseMask(DirectoryMask)
End If
DoSearch(_InitialDirectory)
End Sub
Private Sub DoSearch(ByVal BaseDirectory As DirectoryInfo)
[ô]NÃO PROCURA NO TEMPORARY INTERNET FILES, NO ARRAY E NOS DIRETÓRIOS/ARQUIVOS TEMPORÃRIOS
If CBool(Microsoft.VisualBasic.InStr(BaseDirectory.ToString, [Ô]Temporary Internet Files[Ô])) = False Then
If CBool(Microsoft.VisualBasic.InStr(BaseDirectory.Extension.ToString, [Ô].TMP[Ô])) = False Or CBool(Microsoft.VisualBasic.InStr(BaseDirectory.Extension.ToString, [Ô].tmp[Ô])) = False Then
Dim val() As String = {strProgramFiles.ToString, strProgramFilesX86.ToString, strProgramFilesX64.ToString, strProgramData.ToString, strWindowsDirectory.ToString, strMusicAll.ToString, strMusicRingtones.ToString, strMusicRingtonesWindows.ToString, strMusicasXPAll.ToString, strTemporaryInternetFile.ToString, strVideosPublic.ToString, strAllUser.ToString, strTodosUsuarios.ToString, strTodosUsuariosXP.ToString, strDrivesFolder.ToString, strTempDir.ToString, strTempDirEng.ToString, strWindowsWinsxs.ToString, strTmpDir.ToString, strTemporaryDir.ToString, strMSOCacheFolder.ToString, strDellFolder.ToString, strHPFolder.ToString, strLocalAppData.ToString, [Ô]backup[Ô]}
Dim lst As New List(Of String)
lst.AddRange(val)
[ô]COMPARA DIRETÓRIOS PARA NÃO PROCURAR NOS DIRETÓRIOS EXCESSÕES.
If lst.ToString.Contains(BaseDirectory.FullName.ToString) = False Then
Try
For Each fm As String In _FileMasks
If ConfigurationManager.AppSettings([Ô]ProcuraAllUsers[Ô]).ToString() = [Ô]SIM[Ô] Then
Files.AddRange(BaseDirectory.GetFiles(fm))
Else
If (CBool(Microsoft.VisualBasic.InStr(BaseDirectory.FullName.ToString, [Ô]C:\Users[Ô])) Or CBool(Microsoft.VisualBasic.InStr(BaseDirectory.FullName.ToString, [Ô]C:\Documents and Settings[Ô]))) Then
If CBool(Microsoft.VisualBasic.InStr(BaseDirectory.FullName.ToString, strHomePathUser)) Or CBool(Microsoft.VisualBasic.InStr(BaseDirectory.FullName.ToString, strHomePathUserEng)) Then
Files.AddRange(BaseDirectory.GetFiles(fm))
End If
Else
Files.AddRange(BaseDirectory.GetFiles(fm))
End If
End If
Next
_
Catch u As UnauthorizedAccessException
[ô]Siliently Ignore this error, there isnt any simple
[ô]way to avoid this error.
End Try
Try
Dim Directories As New ArrayList
For Each dm As String In _DirectoryMasks
Directories.AddRange(BaseDirectory.GetDirectories(dm))
_Directories.AddRange(Directories)
Next
For Each di As DirectoryInfo In Directories
DoSearch(di)
Next
Catch u As UnauthorizedAccessException
[ô]Siliently Ignore this error, there isnt any simple
[ô]way to avoid this error.
End Try
End If
End If
End If
End Sub
[ô]Masks are formated like *.jpeg;*.jpg
Private Shared Function ParseMask(ByVal Mask As String) As StringCollection
If IsNothing(Mask) Then
Return Nothing
End If
Mask = Mask.Trim([Ô];[Ô]c)
If Mask.Length = 0 Then
Return Nothing
End If
Dim Masks As New StringCollection
Masks.AddRange(Mask.Split([Ô];[Ô]c))
Return Masks
End Function
Protected Overrides Sub Finalize()
_Files = Nothing
_Directories = Nothing
MyBase.Finalize()
End Sub
[ô]UTILIZAÇÃO
x.Search(dirs(i), word)
End Class
Percorrer o array de diretórios exceções e se nenhum for encontrado ai sim eu pego o conteúdo do diretório e demais diretórios e assim por diante...mas estou me perdendo no array.
If val.Contains(BaseDirectory.FullName.ToString, StringComparer.OrdinalIgnoreCase) = False Then
[ô]MEU CODIGO
Endif