home *** CD-ROM | disk | FTP | other *** search
/ Programming Tool Box / SIMS_2.iso / vb_code1 / findfile / findfile.frm < prev    next >
Text File  |  1994-05-20  |  6KB  |  252 lines

  1. VERSION 2.00
  2. Begin Form FindFile 
  3.    BackColor       =   &H00C0C0C0&
  4.    Caption         =   "Find File"
  5.    ClientHeight    =   2520
  6.    ClientLeft      =   1095
  7.    ClientTop       =   1590
  8.    ClientWidth     =   7365
  9.    Height          =   2925
  10.    Icon            =   FINDFILE.FRX:0000
  11.    Left            =   1035
  12.    LinkTopic       =   "Form1"
  13.    ScaleHeight     =   2520
  14.    ScaleWidth      =   7365
  15.    Top             =   1245
  16.    Width           =   7485
  17.    Begin CommandButton CancelBtn 
  18.       Caption         =   "Cancel"
  19.       Height          =   510
  20.       Left            =   5850
  21.       TabIndex        =   5
  22.       Top             =   1710
  23.       Visible         =   0   'False
  24.       Width           =   1230
  25.    End
  26.    Begin CommandButton OKBtn 
  27.       Caption         =   "OK"
  28.       Height          =   510
  29.       Left            =   5850
  30.       TabIndex        =   4
  31.       Top             =   1125
  32.       Width           =   1230
  33.    End
  34.    Begin TextBox Containing 
  35.       Height          =   285
  36.       Left            =   4095
  37.       TabIndex        =   1
  38.       Top             =   675
  39.       Width           =   2985
  40.    End
  41.    Begin SSCheck IncludeSub 
  42.       Alignment       =   1  'Right Justify
  43.       Caption         =   "Include Subdirectories"
  44.       Height          =   285
  45.       Left            =   2925
  46.       TabIndex        =   2
  47.       Top             =   1260
  48.       Value           =   -1  'True
  49.       Width           =   2220
  50.    End
  51.    Begin TextBox FileSpec 
  52.       Height          =   285
  53.       Left            =   4635
  54.       MaxLength       =   12
  55.       TabIndex        =   0
  56.       Text            =   "*.*"
  57.       Top             =   180
  58.       Width           =   2445
  59.    End
  60.    Begin DirListBox Dir1 
  61.       Height          =   2055
  62.       Left            =   315
  63.       TabIndex        =   6
  64.       Top             =   135
  65.       Width           =   2310
  66.    End
  67.    Begin DriveListBox Drive1 
  68.       Height          =   315
  69.       Left            =   3015
  70.       TabIndex        =   3
  71.       Top             =   1845
  72.       Width           =   2355
  73.    End
  74.    Begin Label Label1 
  75.       BackStyle       =   0  'Transparent
  76.       Caption         =   "Containing:"
  77.       Height          =   240
  78.       Index           =   1
  79.       Left            =   2970
  80.       TabIndex        =   8
  81.       Top             =   720
  82.       Width           =   1095
  83.    End
  84.    Begin Label Label1 
  85.       BackStyle       =   0  'Transparent
  86.       Caption         =   "File Specification:"
  87.       Height          =   240
  88.       Index           =   0
  89.       Left            =   2970
  90.       TabIndex        =   7
  91.       Top             =   225
  92.       Width           =   1770
  93.    End
  94. End
  95. Option Explicit
  96. Option Compare Text
  97.  
  98. Dim F1 As Found
  99. Dim CancelFlag As Integer
  100.  
  101. Sub CancelBtn_Click ()
  102.  
  103. CancelFlag = True
  104.  
  105. End Sub
  106.  
  107. Sub Drive1_Change ()
  108.  
  109. Dir1.Path = Left$(Drive1.Drive, 2)
  110.  
  111. End Sub
  112.  
  113. Function FileContains (FileName As String, SearchText As String) As Integer
  114. Dim FileNumber As Integer
  115. Dim FileLength As Long
  116. Dim Chunk As String
  117. Dim ChunkStart As Long
  118. Const MaxChunk = 20000
  119.  
  120. On Error GoTo FileContainsError
  121.  
  122. FileNumber = FreeFile
  123.  
  124. Open FileName For Binary Access Read Shared As FileNumber
  125. FileLength = LOF(FileNumber)
  126. ChunkStart = 0
  127.  
  128. Do Until ChunkStart = FileLength
  129.     If FileLength - ChunkStart > MaxChunk Then
  130.         Chunk = Input$(MaxChunk, FileNumber)
  131.         ChunkStart = ChunkStart + MaxChunk - Len(SearchText)
  132.     Else
  133.         Chunk = Input$(FileLength - ChunkStart, FileNumber)
  134.         ChunkStart = FileLength
  135.     End If
  136.     If InStr(Chunk, SearchText) > 0 Then
  137.         FileContains = True
  138.         Exit Do
  139.     End If
  140. Loop
  141.  
  142. Close FileNumber
  143.  
  144. Exit Function
  145.  
  146. FileContainsError:
  147.     Select Case Err
  148.         Case Else
  149.             MsgBox Error$ & " on file " & FileName
  150.     End Select
  151.     Exit Function
  152.  
  153. End Function
  154.  
  155. Sub Find (SearchPath As String)
  156. ReDim DirName(0 To 15) As String
  157. Dim DirCount As Integer
  158. Dim FileName As String, Attributes As Integer
  159. Dim x As Integer
  160.  
  161. If Right$(SearchPath, 1) <> "\" Then SearchPath = SearchPath & "\"
  162. DirCount = 0
  163. FileName = Dir$(SearchPath & FileSpec, Attr_Normal + Attr_System + Attr_Hidden)
  164. Do Until FileName = ""
  165.     If Containing = "" Then
  166.         F1.FoundFiles.AddItem SearchPath & FileName
  167.     Else
  168.         If FileContains(SearchPath & FileName, (Containing.Text)) Then
  169.             F1.FoundFiles.AddItem SearchPath & FileName
  170.         End If
  171.     End If
  172.     FileName = Dir$
  173.     DoEvents
  174.     If CancelFlag Then Exit Sub
  175. Loop
  176.  
  177. If IncludeSub Then
  178.     FileName = Dir$(SearchPath & "*.*", Attr_Normal + Attr_System + Attr_Hidden + Attr_Directory)
  179.     Do Until FileName = ""
  180.         If FileName <> "." And FileName <> ".." Then
  181.             Attributes = GetAttr(SearchPath & FileName)
  182.             If (Attributes And Attr_Directory) Then
  183.                 If DirCount > UBound(DirName) Then
  184.                     ReDim Preserve DirName(0 To DirCount + 15)
  185.                 End If
  186.                 DirName(DirCount) = SearchPath & FileName
  187.                 DirCount = DirCount + 1
  188.             End If
  189.         End If
  190.         FileName = Dir$
  191.         DoEvents
  192.         If CancelFlag Then Exit Sub
  193.     Loop
  194.     For x = 0 To DirCount - 1
  195.         Find DirName(x)
  196.     Next x
  197. End If
  198.  
  199. End Sub
  200.  
  201. Sub Form_Unload (Cancel As Integer)
  202.  
  203. If Forms.Count > 1 Then
  204.     Select Case MsgBox("Close search windows also?", MB_YesNoCancel)
  205.         Case IDYes
  206.             End
  207.         Case IDCancel
  208.             Cancel = True
  209.     End Select
  210. End If
  211.             
  212. End Sub
  213.  
  214. Sub OKBtn_Click ()
  215.  
  216. 'MousePointer = Hourglass
  217. OKBtn.Enabled = False
  218.  
  219. Caption = "Find File - Searching"
  220. CancelBtn.Visible = True
  221.  
  222. Set F1 = New Found
  223. CancelFlag = False
  224.  
  225. If FileSpec = "" Then FileSpec = "*.*"
  226. Find (Dir1.Path)
  227.  
  228. Caption = "Find File"
  229. CancelBtn.Visible = False
  230.  
  231. If CancelFlag Then
  232.     Unload F1
  233. Else
  234.     Select Case F1.FoundFiles.ListCount
  235.         Case 0
  236.             MsgBox "No files matching the search criteria were found."
  237.             Unload F1
  238.         Case 1
  239.             F1.Caption = F1.FoundFiles.ListCount & " File Found"
  240.             F1.Show
  241.         Case Else
  242.             F1.Caption = F1.FoundFiles.ListCount & " Files Found"
  243.             F1.Show
  244.     End Select
  245. End If
  246.  
  247. OKBtn.Enabled = True
  248. 'MousePointer = Default
  249.  
  250. End Sub
  251.  
  252.