View Full Version : جستجوی فایل و پوشه های سیستم
  
hossein-jam
پنج شنبه 08 شهریور 1386, 15:15 عصر
سلام
چگونه میتوان در یک موتور جستجو مانند موتور جستجوی ویندوز ساخت که بشه یه فایل مورد نظر رو در کل سیستم جستجو کرد و در صورت وجود اونرو پیدا کنه و آدرسش رو به ما بده
با تشکر
hossein-jam
جمعه 09 شهریور 1386, 11:00 صبح
کسی نیست جواب بده ؟
Bahram0110
جمعه 09 شهریور 1386, 11:55 صبح
سلام
اینو امتحان کن 100% جواب می ده
Public Sub getfiles(Path As String, SubFolder As Boolean, Optional Pattern As String = "*.*")
On Error Resume Next
    Dim WFD As WIN32_FIND_DATA
    Dim hFile As Long, fPath As String, fName As String
    
    fPath = AddBackslash(Path)
    
    Dim sPattern As String
    sPattern = Pattern
    fName = fPath & sPattern
    
  
    hFile = FindFirstFile(fName, WFD)
    If (hFile > 0) And ((WFD.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) <> FILE_ATTRIBUTE_DIRECTORY) Then
        lstFile.AddItem (fPath & StripNulls(WFD.cFileName))
    End If
    
    If hFile > 0 Then
    While FindNextFile(hFile, WFD)
        If ((WFD.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) <> FILE_ATTRIBUTE_DIRECTORY) Then
            lstFile.AddItem (fPath & StripNulls(WFD.cFileName))
        End If
    Wend
    End If
    
    If SubFolder Then
        
   
        hFile = FindFirstFile(fPath & "*.*", WFD)
        If (hFile > 0) And ((WFD.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY) And _
        StripNulls(WFD.cFileName) <> "." And StripNulls(WFD.cFileName) <> ".." Then
        
           getfiles fPath & StripNulls(WFD.cFileName), True, sPattern
        End If
        
        While FindNextFile(hFile, WFD)
            If ((WFD.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY) And _
            StripNulls(WFD.cFileName) <> "." And StripNulls(WFD.cFileName) <> ".." Then
                getfiles fPath & StripNulls(WFD.cFileName), True, sPattern
            End If
        Wend
        
    End If
    FindClose hFile
   
End Sub
Private Sub Search()
    lstfile.Clear
      Call getfiles("C:\windows", False, "*.exe")
End Sub
Private Function StripNulls(f As String) As String
    StripNulls = Left$(f, InStr(1, f, Chr$(0)) - 1)
End Function
Private Function AddBackslash(s As String) As String
    If Len(s) Then
       If Right$(s, 1) <> "\" Then
          AddBackslash = s & "\"
       Else
          AddBackslash = s
       End If
    Else
       AddBackslash = "\"
    End If
End Function
Bahram0110
جمعه 09 شهریور 1386, 11:57 صبح
هر 4 تا تابع رو به پروژت اضافه کن 
با استفاده از تابع search فایل یا فایل های مورد نظرت رو جستجو کن
توجه کن که فایل های پیدا شده در یک لیست باکس به نام lstfile لیست می شوند
Bahram0110
جمعه 09 شهریور 1386, 12:00 عصر
یادم رفت اینو بگم
تو کد زیر : 
Private Sub Search()
    lstfile.Clear
      Call getfiles("C:\windows", False, "*.exe")
End Sub
اگه می خوای زیر پوشه ها هم جستجو بشن باید فالس رو به ترو تغییر بدی
Evil 69
شنبه 10 شهریور 1386, 09:59 صبح
این یک ocx برای این کار
hossein-jam
شنبه 10 شهریور 1386, 17:09 عصر
سلام
با تشکر از تمامی دوستان که پاسخ سوال منو دادن
من توسط توابع آ پی آی تونستم یه موتور جستجو بسازم
حالا میخوام به فایل های یافته شده دستیابی پیدا کنم
یعنی بتونم اونا رو تنظیم و تغییر و ... 
و این تنظیمات رو میخوام توسط خود برنامه نویس انجام بشه 
یعنی قبل از نمایش در لیست ما تنظیمات خودمون رو انجام داده باشیم و سپس فایل ها رو در لیست نمایش بده.
با تشکر و این پایین هم سورس موتور جستجو
:
'Create a form with a command button (command1), a list box (list1)
'and four text boxes (text1, text2, text3 and text4).
'Type in the first textbox a startingpath like c:\
'and in the second textbox you put a pattern like *.* or *.txt
Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
Const MAX_PATH = 260
Const MAXDWORD = &HFFFF
Const INVALID_HANDLE_VALUE = -1
Const FILE_ATTRIBUTE_ARCHIVE = &H20
Const FILE_ATTRIBUTE_DIRECTORY = &H10
Const FILE_ATTRIBUTE_HIDDEN = &H2
Const FILE_ATTRIBUTE_NORMAL = &H80
Const FILE_ATTRIBUTE_READONLY = &H1
Const FILE_ATTRIBUTE_SYSTEM = &H4
Const FILE_ATTRIBUTE_TEMPORARY = &H100
Private Type FILETIME
    dwLowDateTime As Long
    dwHighDateTime As Long
End Type
Private Type WIN32_FIND_DATA
    dwFileAttributes As Long
    ftCreationTime As FILETIME
    ftLastAccessTime As FILETIME
    ftLastWriteTime As FILETIME
    nFileSizeHigh As Long
    nFileSizeLow As Long
    dwReserved0 As Long
    dwReserved1 As Long
    cFileName As String * MAX_PATH
    cAlternate As String * 14
End Type
Function StripNulls(OriginalStr As String) As String
    If (InStr(OriginalStr, Chr(0)) > 0) Then
        OriginalStr = Left(OriginalStr, InStr(OriginalStr, Chr(0)) - 1)
    End If
    StripNulls = OriginalStr
End Function
Function FindFilesAPI(path As String, SearchStr As String, FileCount As Integer, DirCount As Integer)
    Dim FileName As String ' Walking filename variable...
    Dim DirName As String ' SubDirectory Name
    Dim dirNames() As String ' Buffer for directory name entries
    Dim nDir As Integer ' Number of directories in this path
    Dim i As Integer ' For-loop counter...
    Dim hSearch As Long ' Search Handle
    Dim WFD As WIN32_FIND_DATA
    Dim Cont As Integer
    If Right(path, 1) <> "\" Then path = path & "\"
    ' Search for subdirectories.
    nDir = 0
    ReDim dirNames(nDir)
    Cont = True
    hSearch = FindFirstFile(path & "*", WFD)
    If hSearch <> INVALID_HANDLE_VALUE Then
        Do While Cont
        DirName = StripNulls(WFD.cFileName)
        ' Ignore the current and encompassing directories.
        If (DirName <> ".") And (DirName <> "..") Then
            ' Check for directory with bitwise comparison.
            If GetFileAttributes(path & DirName) And FILE_ATTRIBUTE_DIRECTORY Then
                dirNames(nDir) = DirName
                DirCount = DirCount + 1
                nDir = nDir + 1
                ReDim Preserve dirNames(nDir)
            End If
        End If
        Cont = FindNextFile(hSearch, WFD) 'Get next subdirectory.
        Loop
        Cont = FindClose(hSearch)
    End If
    ' Walk through this directory and sum file sizes.
    hSearch = FindFirstFile(path & SearchStr, WFD)
    Cont = True
    If hSearch <> INVALID_HANDLE_VALUE Then
        While Cont
            FileName = StripNulls(WFD.cFileName)
            If (FileName <> ".") And (FileName <> "..") Then
                FindFilesAPI = FindFilesAPI + (WFD.nFileSizeHigh * MAXDWORD) + WFD.nFileSizeLow
                FileCount = FileCount + 1
                List1.AddItem path & FileName
            End If
            Cont = FindNextFile(hSearch, WFD) ' Get next file
        Wend
        Cont = FindClose(hSearch)
    End If
    ' If there are sub-directories...
    If nDir > 0 Then
        ' Recursively walk into them...
        For i = 0 To nDir - 1
            FindFilesAPI = FindFilesAPI + FindFilesAPI(path & dirNames(i) & "\", SearchStr, FileCount, DirCount)
        Next i
    End If
End Function
Sub Command1_Click()
    Dim SearchPath As String, FindStr As String
    Dim FileSize As Long
    Dim NumFiles As Integer, NumDirs As Integer
    Screen.MousePointer = vbHourglass
    List1.Clear
    SearchPath = Text1.Text
    FindStr = Text2.Text
    FileSize = FindFilesAPI(SearchPath, FindStr, NumFiles, NumDirs)
    Text3.Text = NumFiles & " Files found in " & NumDirs + 1 & " Directories"
    Text4.Text = "Size of files found under " & SearchPath & " = " & Format(FileSize, "#,###,###,##0") & " Bytes"
    Screen.MousePointer = vbDefault
End Sub
اگه کسی جواب منو بده ممنون میشم
hossein-jam
یک شنبه 11 شهریور 1386, 14:59 عصر
اگه کسی جواب منو بده ممنون میشم
 
vBulletin® v4.2.5, Copyright ©2000-1404, Jelsoft Enterprises Ltd.