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
File Operations - Class1.vb

Class1.vb

Caricato da: Totem
Scarica il programma completo

  1. Namespace FileOperations
  2.  
  3.     Public Class FileType
  4.         Private VPath As String
  5.         Private VNameOnly As String
  6.         Private VExtension As String
  7.         Private Shared Function GetPath(ByVal name As String) As String
  8.             Dim s As String = ""
  9.             Dim i, a As Int16
  10.  
  11.             For i = 0 To name.Length - 1
  12.                 If name.Chars(i) = "\" Then
  13.                     a = i
  14.                 End If
  15.             Next
  16.             For i = 0 To a
  17.                 s = s & name.Chars(i)
  18.             Next
  19.             GetPath = s
  20.         End Function
  21.         Private Shared Function GetName(ByVal name As String) As String
  22.             Dim s As String = ""
  23.             Dim i, a As Int16
  24.  
  25.             For i = 0 To name.Length - 1
  26.                 If name.Chars(i) = "\" Then
  27.                     a = i
  28.                 End If
  29.             Next
  30.             For i = a To name.Length - 1
  31.                 If name.Chars(i) <> "." And i < name.Length - 4 Then
  32.                     s = s & name.Chars(i)
  33.                 Else
  34.                     Exit For
  35.                 End If
  36.             Next
  37.             GetName = s
  38.         End Function
  39.         Private Shared Function GetExtension(ByVal name As String) As String
  40.             Dim s As String = ""
  41.             Dim i, a As Int16
  42.  
  43.             For i = 0 To name.Length - 1
  44.                 If name.Chars(i) = "." Then
  45.                     a = i
  46.                 End If
  47.             Next
  48.             For i = a To name.Length - 1
  49.                 s = s & name.Chars(i)
  50.             Next
  51.             GetExtension = s
  52.         End Function
  53.         Public Property FileName()
  54.             Get
  55.                 Return FileName
  56.             End Get
  57.             Set(ByVal value)
  58.                 VPath = GetPath(value)
  59.                 VNameOnly = GetName(value)
  60.                 VExtension = GetExtension(value)
  61.             End Set
  62.         End Property
  63.         Public ReadOnly Property Path()
  64.             Get
  65.                 Return VPath
  66.             End Get
  67.         End Property
  68.         Public ReadOnly Property NameOnly()
  69.             Get
  70.                 Return VNameOnly
  71.             End Get
  72.         End Property
  73.         Public ReadOnly Property Extension()
  74.             Get
  75.                 Return VExtension
  76.             End Get
  77.         End Property
  78.         Public Sub New(ByVal file As String)
  79.             FileName = file
  80.         End Sub
  81.     End Class
  82.  
  83.     Public Class Search
  84.         Public Const TrovaFiles As Int16 = 0
  85.         Public Const TrovaTutto As Int16 = 1
  86.         Public Structure Position
  87.             Dim LineNum As Double
  88.             Dim Line As String
  89.         End Structure
  90.         Public Structure MultiSearch
  91.             Dim details As Position
  92.             Dim file As FileType
  93.         End Structure
  94.         Private Shared Function Pos(ByVal toseek As String, ByVal inwhichfind As String) As Int64
  95.             Dim a, b As Int64
  96.  
  97.             Pos = -1
  98.             For a = 0 To inwhichfind.Length - 1 'Esamina ogni carattere della stringa inwhichfind
  99.                 If inwhichfind.Chars(a) = toseek.Chars(0) Then 'Se il carattere numero a è uguale al primo carattere di toseek...
  100.                     For b = 0 To toseek.Length - 1 '...verifica che tutti i caratteri che lo seguono siano uguali a quelli di toseek
  101.                         If toseek.Chars(b) = inwhichfind.Chars(a + b) Then
  102.                             If b = toseek.Length - 1 Then
  103.                                 Return a 'E, in questo caso, ritorna l'indice del primo carattere di toseek in inwhichfind
  104.                             End If
  105.                         Else
  106.                             Pos = -1
  107.                             Exit For 'Altrimenti annulla il ciclo e continua il controllo
  108.                         End If
  109.                     Next
  110.                 Else
  111.                     Pos = -1
  112.                 End If
  113.             Next
  114.         End Function
  115.         Public Shared Function FindIn(ByVal File As FileType, ByVal Occurrence As String) As Position()
  116.             Dim r As New System.IO.StreamReader(CType(File.FileName, String))
  117.             Dim Line As Double = 0.0
  118.             Dim Num As Int64 = 0
  119.             Dim S As String
  120.             Dim Ret() As Position
  121.  
  122.             While Not r.EndOfStream
  123.                 S = r.ReadLine
  124.                 Line += 1
  125.                 If Pos(Occurrence, S) > -1 Then
  126.                     Num += 1
  127.                     ReDim Ret(Num)
  128.                     Ret(Num).Line = S
  129.                     Ret(Num).LineNum = Line
  130.                 End If
  131.             End While
  132.             r.Close()
  133.             r = Nothing
  134.  
  135.             Return Ret
  136.         End Function
  137.         Public Shared Function MultiFindIn(ByVal file As FileType(), ByVal word As String) As MultiSearch()
  138.             Dim r As System.IO.StreamReader
  139.             Dim Line As Double = 0.0
  140.             Dim Num As Int64 = 0, i As Int64
  141.             Dim S As String
  142.             Dim Ret As MultiSearch()
  143.  
  144.             For i = 0 To file.Length - 1
  145.                 r = New System.IO.StreamReader(CType(file(i).FileName, String))
  146.                 Line = 0.0
  147.  
  148.                 While Not r.EndOfStream
  149.                     S = r.ReadLine
  150.                     Line += 1
  151.                     If Pos(word, S) > -1 Then
  152.                         Num += 1
  153.                         ReDim Ret(Num)
  154.                         Ret(Num).details.Line = S
  155.                         Ret(Num).details.LineNum = Line
  156.                         Ret(Num).file = file(i).FileName
  157.                     End If
  158.                 End While
  159.  
  160.                 r.Close()
  161.                 r = Nothing
  162.             Next
  163.  
  164.             Return Ret
  165.         End Function
  166.         Public Shared Function ElencaFiles(ByVal PercSorg As String, ByVal TipoFile As String, ByVal InSottoDir As Boolean) As String()
  167.             Dim ListaDir As New Collection
  168.             Dim PercAtt As String
  169.             Dim NomeFile As String
  170.             Dim FileAtt As String
  171.             Dim DirNum As Integer
  172.             Dim ListaFiles() As String
  173.             Dim N As Int64 = -1
  174.  
  175.             If PercSorg.Chars(PercSorg.Length - 1) <> "\" Then
  176.                 PercSorg = PercSorg & "\"
  177.             End If
  178.             If Dir(PercSorg, TrovaTutto) <> "" Then
  179.                 ListaDir.Add(PercSorg)
  180.             End If
  181.  
  182.             If InSottoDir Then
  183.                 DirNum = 1
  184.                 Do Until DirNum > ListaDir.Count
  185.                     PercAtt = ListaDir.Item(DirNum)
  186.                     NomeFile = Dir(PercAtt, TrovaTutto)
  187.                     Do Until NomeFile = ""
  188.                         If NomeFile.Chars(0) <> "." Then
  189.                             FileAtt = PercAtt & NomeFile
  190.                             If (GetAttr(FileAtt) And vbDirectory) Then
  191.                                 ListaDir.Add(FileAtt & "\")
  192.                             End If
  193.                         End If
  194.                         NomeFile = Dir()
  195.                     Loop
  196.                     DirNum = DirNum + 1
  197.                 Loop
  198.             End If
  199.  
  200.             For DirNum = 1 To ListaDir.Count
  201.                 PercAtt = ListaDir.Item(DirNum)
  202.                 NomeFile = Dir(PercAtt & TipoFile, TrovaFiles)
  203.                 Do Until NomeFile = ""
  204.                     N += 1
  205.                     ReDim ListaFiles(N)
  206.                     ListaFiles(N) = NomeFile
  207.                     NomeFile = Dir()
  208.                 Loop
  209.             Next
  210.  
  211.             Return ListaFiles
  212.         End Function
  213.         Public Shared Function FindFiles(ByVal PercSorg As String, ByVal Name As String, ByVal InSottoDir As Boolean) As String()
  214.             Dim ListaDir As New Collection
  215.             Dim PercAtt As String
  216.             Dim NomeFile As String
  217.             Dim FileAtt As String
  218.             Dim DirNum As Integer
  219.             Dim ListaFiles() As String
  220.             Dim N As Int64 = -1
  221.             Dim TipoFile As String = "*.*"
  222.  
  223.             If PercSorg.Chars(PercSorg.Length - 1) <> "\" Then
  224.                 PercSorg = PercSorg & "\"
  225.             End If
  226.             If Dir(PercSorg, TrovaTutto) <> "" Then
  227.                 ListaDir.Add(PercSorg)
  228.             End If
  229.  
  230.             If InSottoDir Then
  231.                 DirNum = 1
  232.                 Do Until DirNum > ListaDir.Count
  233.                     PercAtt = ListaDir.Item(DirNum)
  234.                     NomeFile = Dir(PercAtt, TrovaTutto)
  235.                     Do Until NomeFile = ""
  236.                         If NomeFile.Chars(0) <> "." Then
  237.                             FileAtt = PercAtt & NomeFile
  238.                             If (GetAttr(FileAtt) And vbDirectory) Then
  239.                                 ListaDir.Add(FileAtt & "\")
  240.                             End If
  241.                         End If
  242.                         NomeFile = Dir()
  243.                     Loop
  244.                     DirNum = DirNum + 1
  245.                 Loop
  246.             End If
  247.  
  248.             For DirNum = 1 To ListaDir.Count
  249.                 PercAtt = ListaDir.Item(DirNum)
  250.                 NomeFile = Dir(PercAtt & TipoFile, TrovaFiles)
  251.                 Do Until NomeFile = ""
  252.                     If Pos(Name, NomeFile) > -1 Then
  253.                         N += 1
  254.                         ReDim ListaFiles(N)
  255.                         ListaFiles(N) = NomeFile
  256.                         NomeFile = Dir()
  257.                     End If
  258.                 Loop
  259.             Next
  260.  
  261.             Return ListaFiles
  262.         End Function
  263.         Public Shared Function FindComplessFiles(ByVal PercSorg As String, ByVal Name As String, ByVal TipoFile As String, ByVal InSottoDir As Boolean) As String()
  264.             Dim ListaDir As New Collection
  265.             Dim PercAtt As String
  266.             Dim NomeFile As String
  267.             Dim FileAtt As String
  268.             Dim DirNum As Integer
  269.             Dim ListaFiles() As String
  270.             Dim N As Int64 = -1
  271.  
  272.             If PercSorg.Chars(PercSorg.Length - 1) <> "\" Then
  273.                 PercSorg = PercSorg & "\"
  274.             End If
  275.             If Dir(PercSorg, TrovaTutto) <> "" Then
  276.                 ListaDir.Add(PercSorg)
  277.             End If
  278.  
  279.             If InSottoDir Then
  280.                 DirNum = 1
  281.                 Do Until DirNum > ListaDir.Count
  282.                     PercAtt = ListaDir.Item(DirNum)
  283.                     NomeFile = Dir(PercAtt, TrovaTutto)
  284.                     Do Until NomeFile = ""
  285.                         If NomeFile.Chars(0) <> "." Then
  286.                             FileAtt = PercAtt & NomeFile
  287.                             If (GetAttr(FileAtt) And vbDirectory) Then
  288.                                 ListaDir.Add(FileAtt & "\")
  289.                             End If
  290.                         End If
  291.                         NomeFile = Dir()
  292.                     Loop
  293.                     DirNum = DirNum + 1
  294.                 Loop
  295.             End If
  296.  
  297.             For DirNum = 1 To ListaDir.Count
  298.                 PercAtt = ListaDir.Item(DirNum)
  299.                 NomeFile = Dir(PercAtt & TipoFile, TrovaFiles)
  300.                 Do Until NomeFile = ""
  301.                     If Pos(Name, NomeFile) > -1 Then
  302.                         N += 1
  303.                         ReDim ListaFiles(N)
  304.                         ListaFiles(N) = NomeFile
  305.                         NomeFile = Dir()
  306.                     End If
  307.                 Loop
  308.             Next
  309.  
  310.             Return ListaFiles
  311.         End Function
  312.  
  313.     End Class
  314.  
  315. End Namespace