Imports System.IO
Imports Microsoft.Win32
Public Class Form1
#Region "Procedure, funzioni"
Public Sub SearchFiles(ByVal Dir As String, ByVal SubDir As Boolean)
Dim Files() As String = Directory.
GetFiles(Dir)
Dim Dirs() As String = Directory.GetDirectories(Dir)
lblStatus.Text = "Ricerca nella directory " + Dir + " in corso..."
For Each S
As String In
Files
Results.Add(S)
Application.DoEvents()
Next
If SubDir Then
For Each S As String In Dirs
SearchFiles(S, True)
Application.DoEvents()
Next
End If
End Sub
Public Sub SearchDirectories(ByVal Dir As String)
Dim Dirs() As String = Directory.GetDirectories(Dir)
For Each S As String In Dirs
Directories.Add(S)
SearchDirectories(S)
Next
End Sub
Public Sub AddFileToList(ByVal FileName As String)
Dim S(1) As String
Dim L As ListViewItem
S(0) = Path.GetFileName(FileName)
S(1) = FileName
L = New ListViewItem(S)
L.Group = lstResult.Groups(0)
lstResult.Items.Add(L)
End Sub
Public Sub AddDirToList(ByVal DirName As String)
Dim S(1) As String
Dim L As ListViewItem
S(0) = GetDirName(DirName)
S(1) = DirName
L = New ListViewItem(S)
L.Group = lstResult.Groups(1)
lstResult.Items.Add(L)
End Sub
Public Sub Transfer(ByRef ArFrom As ArrayList, ByRef ArTo As ArrayList)
ArTo.Clear()
For Each S As String In ArFrom
ArTo.Add(S)
Next
ArFrom.Clear()
End Sub
Public Function GetDirName(ByVal Dir As String) As String
Return Dir.Remove(0, Dir.LastIndexOf("\") + 1)
End Function
Public Function GetDirLen(ByVal Dir As String) As Double
Dim Size As Double
Dim Files() As String = Directory.
GetFiles(Dir)
Dim Dirs() As String = Directory.GetDirectories(Dir)
For Each S
As String In
Files
Size += FileLen(S)
Next
For Each S As String In Dirs
Size += GetDirLen(S)
Next
Return Size
End Function
Public Function RoundSize(ByVal Size As Double) As String
If Size >= 0 And Size < 1000 Then
Return Size & "B"
End If
If Size >= 1000 And Size < 1000000 Then
Return CType(Size / 1000, UInt32) & "KB"
End If
If Size >= 1000000 And Size < 1000000000 Then
Return CType(Size / 1000000, UInt16) & "MB"
End If
If Size >= 1000000000 And Size < 1000000000000 Then
Return CType(Size / 1000000000, UInt16) & "GB"
End If
End Function
Public Function GetIcon(ByVal Ext As String) As Icon
Dim RegKey As RegistryKey
Dim KeyName As String
Dim Icn As Icon
RegKey = Registry.ClassesRoot.OpenSubKey(Ext)
KeyName = RegKey.GetValue("(Predefinito)")
RegKey = Registry.ClassesRoot.OpenSubKey(KeyName).OpenSubKey("DefaultIcon")
If RegKey Is Nothing Then
Return Nothing
End If
KeyName = RegKey.GetValue("(Predefinito)")
Icn = Drawing.Icon.ExtractAssociatedIcon(KeyName)
Return Icn
End Function
#End Region
Public Results As New ArrayList
Public Directories As New ArrayList
Private Sub chbWord_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chbWord.CheckedChanged
grpWord.Enabled = chbWord.Checked
End Sub
Private Sub chbExt_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chbExt.CheckedChanged
grpExt.Enabled = chbExt.Checked
End Sub
Private Sub chbDate_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chbDate.CheckedChanged
grpDate.Enabled = chbDate.Checked
End Sub
Private Sub chbAttribute_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chbAttribute.CheckedChanged
grpAttribute.Enabled = chbAttribute.Checked
End Sub
Private Sub chbSize_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chbSize.CheckedChanged
grpSize.Enabled = chbSize.Checked
End Sub
Private Sub cmdBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBrowse.Click
Dim F As New FolderBrowserDialog
F.Description = "Selezionare la cartella in cui cercare:"
If F.ShowDialog = Windows.Forms.DialogResult.OK Then
txtDir.Text = F.SelectedPath
End If
End Sub
Private Sub cmdSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSearch.Click
If txtDir.Text = Nothing Then
MsgBox("Nessuna directory inserita: impossibile iniziare la ricerca!", MsgBoxStyle.Exclamation)
Exit Sub
End If
If chbWord.Checked And txtWord.Text = Nothing Then
MsgBox("Specificare una parola da cercare nel nome del file!", MsgBoxStyle.Exclamation)
Exit Sub
End If
If chbExt.Checked And lstExt.Items.Count = 0 Then
MsgBox("Inserire almeno un'estensione per iniziare la ricerca!", MsgBoxStyle.Exclamation)
Exit Sub
End If
If chbSize.Checked And cmbRel.SelectedIndex < 0 Then
MsgBox("Specificare una relazione per selezionare i file di una determinata grandezza!", MsgBoxStyle.Exclamation)
Exit Sub
End If
If chbDate.Checked And (cmbField.SelectedIndex < 0 Or cmbWhen.SelectedIndex < 0) Then
MsgBox("Inserire tutti i campi necessari per condurre una ricerca basata sulla data!", MsgBoxStyle.Exclamation)
Exit Sub
End If
If chbAttribute.Checked And cmbAttribute.SelectedIndex < 0 Then
MsgBox("Specificare un attributo!", MsgBoxStyle.Exclamation)
Exit Sub
End If
Results.Clear()
lstResult.Items.Clear()
SearchFiles(txtDir.Text, chbSubDir.Checked)
If chbExt.Checked And lstExt.Items.Contains(".dir") Then
SearchDirectories(txtDir.Text)
End If
Dim Backup As New ArrayList
Dim W As StreamWriter = Nothing
If chbWord.Checked Then
lblStatus.Text = "Selezione dei file che contengano la parola '" + txtWord.Text + "' nel proprio nome in corso..."
Dim N As String
For Each S As String In Results
N = Path.GetFileName(S)
If chbCase.Checked Then
If N.ToLower.Contains(txtWord.Text.ToLower) Then
Backup.Add(S)
End If
Else
If N.Contains(txtWord.Text) Then
Backup.Add(S)
End If
End If
Application.DoEvents()
Next
Transfer(Backup, Results)
lblStatus.Text = "Selezione delle cartelle che contengano la parola '" + txtWord.Text + "' nel proprio nome in corso..."
For Each S As String In Directories
N = GetDirName(S)
If chbCase.Checked Then
If N.ToLower.Contains(txtWord.Text.ToLower) Then
Backup.Add(S)
End If
Else
If N.Contains(txtWord.Text) Then
Backup.Add(S)
End If
End If
Application.DoEvents()
Next
Transfer(Backup, Directories)
End If
If chbExt.Checked Then
lblStatus.Text = "Selezione dei file con estensione data in corso..."
For Each S As String In Results
For Each Ext As String In lstExt.Items
If S.EndsWith(Ext) Then
Backup.Add(S)
End If
Next
Next
Transfer(Backup, Results)
End If
If chbDate.Checked Then
lblStatus.Text = "Selezione dei file " + cmbField.SelectedItem + " " + cmbWhen.SelectedItem + " " + mclDate.SelectionRange.Start.ToShortDateString
Dim F As FileInfo
Dim P As DirectoryInfo
Dim D As Date
Dim Selected As Date = mclDate.SelectionRange.Start
For Each S As String In Results
F = New FileInfo(S)
Select Case cmbField.SelectedIndex
Case 0
D = F.LastWriteTime
Case 1
D = F.CreationTime
Case 2
D = F.LastAccessTime
End Select
Select Case cmbWhen.SelectedIndex
Case 0
If D.CompareTo(Selected) < 0 Then
Backup.Add(S)
End If
Case 1
If D.CompareTo(Selected) = 0 Then
Backup.Add(S)
End If
Case 2
If D.CompareTo(Selected) > 0 Then
Backup.Add(S)
End If
End Select
Next
Transfer(Backup, Results)
lblStatus.Text = "Selezione delle cartelle " + cmbField.SelectedItem + " " + cmbWhen.SelectedItem + " " + mclDate.SelectionRange.Start.ToShortDateString
For Each S As String In Directories
P = New DirectoryInfo(S)
Select Case cmbField.SelectedIndex
Case 0
D = P.LastWriteTime
Case 1
D = P.CreationTime
Case 2
D = P.LastAccessTime
End Select
Select Case cmbWhen.SelectedIndex
Case 0
If D.CompareTo(Selected) < 0 Then
Backup.Add(S)
End If
Case 1
If D.CompareTo(Selected) = 0 Then
Backup.Add(S)
End If
Case 2
If D.CompareTo(Selected) > 0 Then
Backup.Add(S)
End If
End Select
Next
Transfer(Backup, Directories)
End If
If chbSize.Checked Then
lblStatus.Text = "Selezione dei file di dimensioni " + cmbRel.SelectedItem + " " & nudSize.Value & "KB"
Dim Size As Double
For Each S As String In Results
Size = FileLen(S)
Select Case cmbRel.SelectedIndex
Case 0
If Size / 1000 <= nudSize.Value Then
Backup.Add(S)
End If
Case 1
If Size / 1000 >= nudSize.Value Then
Backup.Add(S)
End If
End Select
Next
Transfer(Backup, Results)
lblStatus.Text = "Selezione delle cartelle file di dimensioni " + cmbRel.SelectedItem + " " & nudSize.Value & "KB"
For Each S As String In Directories
Size = GetDirLen(S)
Select Case cmbRel.SelectedIndex
Case 0
If Size / 1000 <= nudSize.Value Then
Backup.Add(S)
End If
Case 1
If Size / 1000 >= nudSize.Value Then
Backup.Add(S)
End If
End Select
Next
Transfer(Backup, Directories)
End If
If chbAttribute.Checked Then
Dim F As FileInfo
Dim Attr As FileAttribute
Select Case cmbAttribute.SelectedIndex
Case 0
Attr = FileAttributes.Hidden
Case 1
Attr = FileAttributes.System
Case 2
Attr = FileAttributes.Normal
Case 3
Attr = FileAttributes.ReadOnly
Case 4
Attr = FileAttributes.Compressed
Case 5
Attr = FileAttributes.Encrypted
Case 6
Attr = FileAttributes.Temporary
End Select
For Each S As String In Results
F = New FileInfo(S)
If F.Attributes = Attr Then
Backup.Add(S)
End If
Next
End If
If chbReport.Checked Then
W = New StreamWriter(Application.StartupPath + "\Report di " + Date.Now.ToLongDateString + ", ore " + Date.Now.ToShortTimeString + ".txt")
End If
For Each S As String In Results
AddFileToList(S)
If chbReport.Checked Then
W.WriteLine(S)
End If
Next
For Each S As String In Directories
AddDirToList(S)
If chbReport.Checked Then
W.WriteLine(S)
End If
Next
If chbReport.Checked Then
W.Close()
End If
lblStatus.Text = "Operazione completata: trovati " & Results.Count & " files e " & Directories.Count & " cartelle"
End Sub
Private Sub cmbPredExt_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbPredExt.SelectedIndexChanged
Select Case cmbPredExt.SelectedIndex
Case 0
lstExt.Items.Add(".dir")
Case 1
With lstExt.Items
.Add(".mp3")
.Add(".mid")
.Add(".wav")
.Add(".wma")
.Add(".ogg")
End With
Case 2
With lstExt.Items
.Add(".mpg")
.Add(".wmv")
.Add(".avi")
.Add(".mov")
.Add(".gvp")
.Add(".gvi")
End With
Case 3
With lstExt.Items
.Add(".txt")
.Add(".doc")
End With
Case 4
With lstExt.Items
.Add(".dat")
.Add(".bin")
.Add(".dll")
.Add(".cpl")
.Add(".log")
.Add(".inf")
.Add(".ini")
End With
Case 5
With lstExt.Items
.Add(".zip")
.Add(".rar")
.Add(".cab")
.Add(".7z")
.Add(".z")
.Add(".ace")
.Add(".tar")
.Add(".gz")
End With
End Select
End Sub
Private Sub strAddExt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strAddExt.Click
Dim S As String = InputBox("Inserire un'estensione in forma .xxx:")
If S <> Nothing Then
lstExt.Items.Add(S)
End If
End Sub
Private Sub strRemoveExt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strRemoveExt.Click
If lstExt.SelectedIndex >= 0 Then
lstExt.Items.RemoveAt(lstExt.SelectedIndex)
End If
End Sub
Private Sub lstResult_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstResult.SelectedIndexChanged
Try
Dim Selected As ListViewItem = lstResult.SelectedItems(0)
If Selected.Group Is lstResult.Groups(0) Then
Dim F As New FileInfo(Selected.SubItems(1).Text)
txtInfo.Text = F.Name + vbCrLf + vbCrLf + _
"Creato il " + F.CreationTime.ToShortDateString + " alle ore " + F.CreationTime.ToShortTimeString + vbCrLf + _
"Modificato il " + F.LastWriteTime.ToShortDateString + " alle ore " + F.LastWriteTime.ToShortTimeString + vbCrLf + _
"Aperto l'ultima volta il " + F.LastAccessTime.ToShortDateString + " alle ore " + F.LastAccessTime.ToShortTimeString + vbCrLf + _
"Grande complessivamente " & F.Length & " bytes (" + RoundSize(F.Length) + ")" + vbCrLf + vbCrLf + _
"Percorso completo: " + vbCrLf + F.FullName
Else
Dim D As New DirectoryInfo(Selected.SubItems(1).Text)
Dim L As Double = GetDirLen(D.FullName)
txtInfo.Text = D.Name + vbCrLf + vbCrLf + _
"Creata il " + D.CreationTime.ToShortDateString + " alle ore " + D.CreationTime.ToShortTimeString + vbCrLf + _
"Modificata il " + D.LastWriteTime.ToShortDateString + " alle ore " + D.LastWriteTime.ToShortTimeString + vbCrLf + _
"Aperta l'ultima volta il " + D.LastAccessTime.ToShortDateString + " alle ore " + D.LastAccessTime.ToShortTimeString + vbCrLf + _
"Grande complessivamente " & L & " bytes (" + RoundSize(L) + ")" + vbCrLf + vbCrLf + _
"Percorso completo: " + vbCrLf + D.FullName
End If
Catch aOoRE As ArgumentOutOfRangeException
End Try
End Sub
End Class