Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Iper Search Plus - Form1.vb

Form1.vb

Caricato da: Totem
Scarica il programma completo

  1. Imports System.IO
  2. Imports Microsoft.Win32
  3. Public Class Form1
  4. #Region "Procedure, funzioni"
  5.     Public Sub SearchFiles(ByVal Dir As String, ByVal SubDir As Boolean)
  6.         Dim Files() As String = Directory.GetFiles(Dir)
  7.         Dim Dirs() As String = Directory.GetDirectories(Dir)
  8.  
  9.         lblStatus.Text = "Ricerca nella directory " + Dir + " in corso..."
  10.         For Each S As String In Files
  11.             Results.Add(S)
  12.             Application.DoEvents()
  13.         Next
  14.         If SubDir Then
  15.             For Each S As String In Dirs
  16.                 SearchFiles(S, True)
  17.                 Application.DoEvents()
  18.             Next
  19.         End If
  20.     End Sub
  21.     Public Sub SearchDirectories(ByVal Dir As String)
  22.         Dim Dirs() As String = Directory.GetDirectories(Dir)
  23.  
  24.         For Each S As String In Dirs
  25.             Directories.Add(S)
  26.             SearchDirectories(S)
  27.         Next
  28.     End Sub
  29.     Public Sub AddFileToList(ByVal FileName As String)
  30.         Dim S(1) As String
  31.         Dim L As ListViewItem
  32.  
  33.         S(0) = Path.GetFileName(FileName)
  34.         S(1) = FileName
  35.         L = New ListViewItem(S)
  36.         L.Group = lstResult.Groups(0)
  37.         lstResult.Items.Add(L)
  38.     End Sub
  39.     Public Sub AddDirToList(ByVal DirName As String)
  40.         Dim S(1) As String
  41.         Dim L As ListViewItem
  42.  
  43.         S(0) = GetDirName(DirName)
  44.         S(1) = DirName
  45.         L = New ListViewItem(S)
  46.         L.Group = lstResult.Groups(1)
  47.         lstResult.Items.Add(L)
  48.     End Sub
  49.     Public Sub Transfer(ByRef ArFrom As ArrayList, ByRef ArTo As ArrayList)
  50.         ArTo.Clear()
  51.         For Each S As String In ArFrom
  52.             ArTo.Add(S)
  53.         Next
  54.         ArFrom.Clear()
  55.     End Sub
  56.     Public Function GetDirName(ByVal Dir As String) As String
  57.         Return Dir.Remove(0, Dir.LastIndexOf("\") + 1)
  58.     End Function
  59.     Public Function GetDirLen(ByVal Dir As String) As Double
  60.         Dim Size As Double
  61.         Dim Files() As String = Directory.GetFiles(Dir)
  62.         Dim Dirs() As String = Directory.GetDirectories(Dir)
  63.  
  64.         For Each S As String In Files
  65.             Size += FileLen(S)
  66.         Next
  67.         For Each S As String In Dirs
  68.             Size += GetDirLen(S)
  69.         Next
  70.  
  71.         Return Size
  72.     End Function
  73.     Public Function RoundSize(ByVal Size As Double) As String
  74.         If Size >= 0 And Size < 1000 Then
  75.             Return Size & "B"
  76.         End If
  77.         If Size >= 1000 And Size < 1000000 Then
  78.             Return CType(Size / 1000, UInt32) & "KB"
  79.         End If
  80.         If Size >= 1000000 And Size < 1000000000 Then
  81.             Return CType(Size / 1000000, UInt16) & "MB"
  82.         End If
  83.         If Size >= 1000000000 And Size < 1000000000000 Then
  84.             Return CType(Size / 1000000000, UInt16) & "GB"
  85.         End If
  86.     End Function
  87.     Public Function GetIcon(ByVal Ext As String) As Icon
  88.         Dim RegKey As RegistryKey
  89.         Dim KeyName As String
  90.         Dim Icn As Icon
  91.  
  92.         RegKey = Registry.ClassesRoot.OpenSubKey(Ext)
  93.         KeyName = RegKey.GetValue("(Predefinito)")
  94.         RegKey = Registry.ClassesRoot.OpenSubKey(KeyName).OpenSubKey("DefaultIcon")
  95.         If RegKey Is Nothing Then
  96.             Return Nothing
  97.         End If
  98.         KeyName = RegKey.GetValue("(Predefinito)")
  99.         Icn = Drawing.Icon.ExtractAssociatedIcon(KeyName)
  100.         Return Icn
  101.     End Function
  102. #End Region
  103.     Public Results As New ArrayList
  104.     Public Directories As New ArrayList
  105.     Private Sub chbWord_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chbWord.CheckedChanged
  106.         grpWord.Enabled = chbWord.Checked
  107.     End Sub
  108.     Private Sub chbExt_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chbExt.CheckedChanged
  109.         grpExt.Enabled = chbExt.Checked
  110.     End Sub
  111.     Private Sub chbDate_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chbDate.CheckedChanged
  112.         grpDate.Enabled = chbDate.Checked
  113.     End Sub
  114.     Private Sub chbAttribute_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chbAttribute.CheckedChanged
  115.         grpAttribute.Enabled = chbAttribute.Checked
  116.     End Sub
  117.     Private Sub chbSize_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chbSize.CheckedChanged
  118.         grpSize.Enabled = chbSize.Checked
  119.     End Sub
  120.     Private Sub cmdBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBrowse.Click
  121.         Dim F As New FolderBrowserDialog
  122.         F.Description = "Selezionare la cartella in cui cercare:"
  123.         If F.ShowDialog = Windows.Forms.DialogResult.OK Then
  124.             txtDir.Text = F.SelectedPath
  125.         End If
  126.     End Sub
  127.     Private Sub cmdSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSearch.Click
  128.         If txtDir.Text = Nothing Then
  129.             MsgBox("Nessuna directory inserita: impossibile iniziare la ricerca!", MsgBoxStyle.Exclamation)
  130.             Exit Sub
  131.         End If
  132.         If chbWord.Checked And txtWord.Text = Nothing Then
  133.             MsgBox("Specificare una parola da cercare nel nome del file!", MsgBoxStyle.Exclamation)
  134.             Exit Sub
  135.         End If
  136.         If chbExt.Checked And lstExt.Items.Count = 0 Then
  137.             MsgBox("Inserire almeno un'estensione per iniziare la ricerca!", MsgBoxStyle.Exclamation)
  138.             Exit Sub
  139.         End If
  140.         If chbSize.Checked And cmbRel.SelectedIndex < 0 Then
  141.             MsgBox("Specificare una relazione per selezionare i file di una determinata grandezza!", MsgBoxStyle.Exclamation)
  142.             Exit Sub
  143.         End If
  144.         If chbDate.Checked And (cmbField.SelectedIndex < 0 Or cmbWhen.SelectedIndex < 0) Then
  145.             MsgBox("Inserire tutti i campi necessari per condurre una ricerca basata sulla data!", MsgBoxStyle.Exclamation)
  146.             Exit Sub
  147.         End If
  148.         If chbAttribute.Checked And cmbAttribute.SelectedIndex < 0 Then
  149.             MsgBox("Specificare un attributo!", MsgBoxStyle.Exclamation)
  150.             Exit Sub
  151.         End If
  152.  
  153.         Results.Clear()
  154.         lstResult.Items.Clear()
  155.         SearchFiles(txtDir.Text, chbSubDir.Checked)
  156.         If chbExt.Checked And lstExt.Items.Contains(".dir") Then
  157.             SearchDirectories(txtDir.Text)
  158.         End If
  159.  
  160.         Dim Backup As New ArrayList
  161.         Dim W As StreamWriter = Nothing
  162.  
  163.         If chbWord.Checked Then
  164.             lblStatus.Text = "Selezione dei file che contengano la parola '" + txtWord.Text + "' nel proprio nome in corso..."
  165.             Dim N As String
  166.             For Each S As String In Results
  167.                 N = Path.GetFileName(S)
  168.                 If chbCase.Checked Then
  169.                     If N.ToLower.Contains(txtWord.Text.ToLower) Then
  170.                         Backup.Add(S)
  171.                     End If
  172.                 Else
  173.                     If N.Contains(txtWord.Text) Then
  174.                         Backup.Add(S)
  175.                     End If
  176.                 End If
  177.                 Application.DoEvents()
  178.             Next
  179.             Transfer(Backup, Results)
  180.  
  181.             lblStatus.Text = "Selezione delle cartelle che contengano la parola '" + txtWord.Text + "' nel proprio nome in corso..."
  182.             For Each S As String In Directories
  183.                 N = GetDirName(S)
  184.                 If chbCase.Checked Then
  185.                     If N.ToLower.Contains(txtWord.Text.ToLower) Then
  186.                         Backup.Add(S)
  187.                     End If
  188.                 Else
  189.                     If N.Contains(txtWord.Text) Then
  190.                         Backup.Add(S)
  191.                     End If
  192.                 End If
  193.                 Application.DoEvents()
  194.             Next
  195.             Transfer(Backup, Directories)
  196.         End If
  197.  
  198.         If chbExt.Checked Then
  199.             lblStatus.Text = "Selezione dei file con estensione data in corso..."
  200.             For Each S As String In Results
  201.                 For Each Ext As String In lstExt.Items
  202.                     If S.EndsWith(Ext) Then
  203.                         Backup.Add(S)
  204.                     End If
  205.                 Next
  206.             Next
  207.             Transfer(Backup, Results)
  208.         End If
  209.  
  210.         If chbDate.Checked Then
  211.             lblStatus.Text = "Selezione dei file " + cmbField.SelectedItem + " " + cmbWhen.SelectedItem + " " + mclDate.SelectionRange.Start.ToShortDateString
  212.             Dim F As FileInfo
  213.             Dim P As DirectoryInfo
  214.             Dim D As Date
  215.             Dim Selected As Date = mclDate.SelectionRange.Start
  216.  
  217.             For Each S As String In Results
  218.                 F = New FileInfo(S)
  219.                 Select Case cmbField.SelectedIndex
  220.                     Case 0
  221.                         D = F.LastWriteTime
  222.                     Case 1
  223.                         D = F.CreationTime
  224.                     Case 2
  225.                         D = F.LastAccessTime
  226.                 End Select
  227.                 Select Case cmbWhen.SelectedIndex
  228.                     Case 0
  229.                         If D.CompareTo(Selected) < 0 Then
  230.                             Backup.Add(S)
  231.                         End If
  232.                     Case 1
  233.                         If D.CompareTo(Selected) = 0 Then
  234.                             Backup.Add(S)
  235.                         End If
  236.                     Case 2
  237.                         If D.CompareTo(Selected) > 0 Then
  238.                             Backup.Add(S)
  239.                         End If
  240.                 End Select
  241.             Next
  242.             Transfer(Backup, Results)
  243.  
  244.             lblStatus.Text = "Selezione delle cartelle " + cmbField.SelectedItem + " " + cmbWhen.SelectedItem + " " + mclDate.SelectionRange.Start.ToShortDateString
  245.             For Each S As String In Directories
  246.                 P = New DirectoryInfo(S)
  247.                 Select Case cmbField.SelectedIndex
  248.                     Case 0
  249.                         D = P.LastWriteTime
  250.                     Case 1
  251.                         D = P.CreationTime
  252.                     Case 2
  253.                         D = P.LastAccessTime
  254.                 End Select
  255.                 Select Case cmbWhen.SelectedIndex
  256.                     Case 0
  257.                         If D.CompareTo(Selected) < 0 Then
  258.                             Backup.Add(S)
  259.                         End If
  260.                     Case 1
  261.                         If D.CompareTo(Selected) = 0 Then
  262.                             Backup.Add(S)
  263.                         End If
  264.                     Case 2
  265.                         If D.CompareTo(Selected) > 0 Then
  266.                             Backup.Add(S)
  267.                         End If
  268.                 End Select
  269.             Next
  270.             Transfer(Backup, Directories)
  271.         End If
  272.  
  273.         If chbSize.Checked Then
  274.             lblStatus.Text = "Selezione dei file di dimensioni " + cmbRel.SelectedItem + " " & nudSize.Value & "KB"
  275.             Dim Size As Double
  276.             For Each S As String In Results
  277.                 Size = FileLen(S)
  278.                 Select Case cmbRel.SelectedIndex
  279.                     Case 0
  280.                         If Size / 1000 <= nudSize.Value Then
  281.                             Backup.Add(S)
  282.                         End If
  283.                     Case 1
  284.                         If Size / 1000 >= nudSize.Value Then
  285.                             Backup.Add(S)
  286.                         End If
  287.                 End Select
  288.             Next
  289.             Transfer(Backup, Results)
  290.  
  291.             lblStatus.Text = "Selezione delle cartelle file di dimensioni " + cmbRel.SelectedItem + " " & nudSize.Value & "KB"
  292.             For Each S As String In Directories
  293.                 Size = GetDirLen(S)
  294.                 Select Case cmbRel.SelectedIndex
  295.                     Case 0
  296.                         If Size / 1000 <= nudSize.Value Then
  297.                             Backup.Add(S)
  298.                         End If
  299.                     Case 1
  300.                         If Size / 1000 >= nudSize.Value Then
  301.                             Backup.Add(S)
  302.                         End If
  303.                 End Select
  304.             Next
  305.             Transfer(Backup, Directories)
  306.         End If
  307.  
  308.         If chbAttribute.Checked Then
  309.             Dim F As FileInfo
  310.             Dim Attr As FileAttribute
  311.             Select Case cmbAttribute.SelectedIndex
  312.                 Case 0
  313.                     Attr = FileAttributes.Hidden
  314.                 Case 1
  315.                     Attr = FileAttributes.System
  316.                 Case 2
  317.                     Attr = FileAttributes.Normal
  318.                 Case 3
  319.                     Attr = FileAttributes.ReadOnly
  320.                 Case 4
  321.                     Attr = FileAttributes.Compressed
  322.                 Case 5
  323.                     Attr = FileAttributes.Encrypted
  324.                 Case 6
  325.                     Attr = FileAttributes.Temporary
  326.             End Select
  327.  
  328.             For Each S As String In Results
  329.                 F = New FileInfo(S)
  330.                 If F.Attributes = Attr Then
  331.                     Backup.Add(S)
  332.                 End If
  333.             Next
  334.         End If
  335.  
  336.         If chbReport.Checked Then
  337.             W = New StreamWriter(Application.StartupPath + "\Report di " + Date.Now.ToLongDateString + ", ore " + Date.Now.ToShortTimeString + ".txt")
  338.         End If
  339.  
  340.         For Each S As String In Results
  341.             AddFileToList(S)
  342.             If chbReport.Checked Then
  343.                 W.WriteLine(S)
  344.             End If
  345.         Next
  346.         For Each S As String In Directories
  347.             AddDirToList(S)
  348.             If chbReport.Checked Then
  349.                 W.WriteLine(S)
  350.             End If
  351.         Next
  352.  
  353.         If chbReport.Checked Then
  354.             W.Close()
  355.         End If
  356.  
  357.         lblStatus.Text = "Operazione completata: trovati " & Results.Count & " files e " & Directories.Count & " cartelle"
  358.     End Sub
  359.     Private Sub cmbPredExt_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbPredExt.SelectedIndexChanged
  360.         Select Case cmbPredExt.SelectedIndex
  361.             Case 0
  362.                 lstExt.Items.Add(".dir")
  363.             Case 1
  364.                 With lstExt.Items
  365.                     .Add(".mp3")
  366.                     .Add(".mid")
  367.                     .Add(".wav")
  368.                     .Add(".wma")
  369.                     .Add(".ogg")
  370.                 End With
  371.             Case 2
  372.                 With lstExt.Items
  373.                     .Add(".mpg")
  374.                     .Add(".wmv")
  375.                     .Add(".avi")
  376.                     .Add(".mov")
  377.                     .Add(".gvp")
  378.                     .Add(".gvi")
  379.                 End With
  380.             Case 3
  381.                 With lstExt.Items
  382.                     .Add(".txt")
  383.                     .Add(".doc")
  384.                 End With
  385.             Case 4
  386.                 With lstExt.Items
  387.                     .Add(".dat")
  388.                     .Add(".bin")
  389.                     .Add(".dll")
  390.                     .Add(".cpl")
  391.                     .Add(".log")
  392.                     .Add(".inf")
  393.                     .Add(".ini")
  394.                 End With
  395.             Case 5
  396.                 With lstExt.Items
  397.                     .Add(".zip")
  398.                     .Add(".rar")
  399.                     .Add(".cab")
  400.                     .Add(".7z")
  401.                     .Add(".z")
  402.                     .Add(".ace")
  403.                     .Add(".tar")
  404.                     .Add(".gz")
  405.                 End With
  406.         End Select
  407.     End Sub
  408.     Private Sub strAddExt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strAddExt.Click
  409.         Dim S As String = InputBox("Inserire un'estensione in forma .xxx:")
  410.         If S <> Nothing Then
  411.             lstExt.Items.Add(S)
  412.         End If
  413.     End Sub
  414.     Private Sub strRemoveExt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strRemoveExt.Click
  415.         If lstExt.SelectedIndex >= 0 Then
  416.             lstExt.Items.RemoveAt(lstExt.SelectedIndex)
  417.         End If
  418.     End Sub
  419.     Private Sub lstResult_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstResult.SelectedIndexChanged
  420.         Try
  421.             Dim Selected As ListViewItem = lstResult.SelectedItems(0)
  422.  
  423.             If Selected.Group Is lstResult.Groups(0) Then
  424.                 Dim F As New FileInfo(Selected.SubItems(1).Text)
  425.                 txtInfo.Text = F.Name + vbCrLf + vbCrLf + _
  426.                 "Creato il " + F.CreationTime.ToShortDateString + " alle ore " + F.CreationTime.ToShortTimeString + vbCrLf + _
  427.                 "Modificato il " + F.LastWriteTime.ToShortDateString + " alle ore " + F.LastWriteTime.ToShortTimeString + vbCrLf + _
  428.                 "Aperto l'ultima volta il " + F.LastAccessTime.ToShortDateString + " alle ore " + F.LastAccessTime.ToShortTimeString + vbCrLf + _
  429.                 "Grande complessivamente " & F.Length & " bytes (" + RoundSize(F.Length) + ")" + vbCrLf + vbCrLf + _
  430.                 "Percorso completo: " + vbCrLf + F.FullName
  431.             Else
  432.                 Dim D As New DirectoryInfo(Selected.SubItems(1).Text)
  433.                 Dim L As Double = GetDirLen(D.FullName)
  434.                 txtInfo.Text = D.Name + vbCrLf + vbCrLf + _
  435.                 "Creata il " + D.CreationTime.ToShortDateString + " alle ore " + D.CreationTime.ToShortTimeString + vbCrLf + _
  436.                 "Modificata il " + D.LastWriteTime.ToShortDateString + " alle ore " + D.LastWriteTime.ToShortTimeString + vbCrLf + _
  437.                 "Aperta l'ultima volta il " + D.LastAccessTime.ToShortDateString + " alle ore " + D.LastAccessTime.ToShortTimeString + vbCrLf + _
  438.                 "Grande complessivamente " & L & " bytes (" + RoundSize(L) + ")" + vbCrLf + vbCrLf + _
  439.                 "Percorso completo: " + vbCrLf + D.FullName
  440.             End If
  441.         Catch aOoRE As ArgumentOutOfRangeException
  442.  
  443.         End Try
  444.     End Sub
  445. End Class