Jump to content

Searching A File


 Share

Recommended Posts

Not only is it possible to search for a file on one drive, but you could also search every drive listed on the computer. You would just need to make your own file search function(s)...

I would normally post an example, but I don't have the time right now to make the script.

(However, I'll try to make it later if nobody else has one to offer.)

To help you out, I'd recommend looking into the following built-in functions:

DriveGetDrive()

DriveStatus()

FileExists()

FileFindFirstFile()

FileFindNextFile()

You wouldn't need to use all of these fucntions, just the last 2 or 3.

The first two were thrown in, just in case you wanted to search the entire system. :whistle:

Link to comment
Share on other sites

It's been a busy week, but I found some time to make the example that I promised. It's not that pretty right now, but you should be able to easily customize it to your liking: (Watch out out for line wraps from this forum!)

; Define Global Variables
Global $OrigDir = @WorkingDir
Global $SplashCount = 1
Global $BeenHere = 0

; Save source script name as array for later use
$ScriptName = StringSplit(@ScriptName, ".")

; Display input box, populate with wild-carded script root file name
$SearchName = InputBox("Search for Files and Folders", "Search for files or folders named:", $ScriptName[1] & ".*", "", 300, 60)
; If cancel button was clicked, then exit
If @error == 1 Then Exit

; Search the system for the specified file
SearchSystem($SearchName)

; Change current working directory back to original before exiting
ChangePath($OrigDir)

; Okay, we're done! (Finally!!!)
MsgBox(4096, "The End", "End of script reached...")

Exit

; Obtains list of valid drives for the system
Func SearchSystem($SearchName)
  $drives = DriveGetDrive("all")
  If NOT @error Then
   ; Performs check on each drive listed within system
    For $DriveNum = 1 to (Ubound($drives) - 1)
      $stat = DriveStatus($drives[$DriveNum])
     ; Only proceed if drive is readable
      If $stat == "READY" Then
        Global $DrivePath = $drives[$DriveNum] & "\"
       ; Try to change working directory to specified drive
        If ChangePath($DrivePath) <> -1 Then
         ; Search drive for specified file
          FileSearchTree($DrivePath, $SearchName)
        EndIf
      EndIf
    Next
  EndIf
EndFunc


; Searches specified directory for files
Func FileSearchTree($FilePath, $FileName)
 ; Search current directory for specified file
  Local $FileList = GetDirList($FilePath, $FileName)
 ; Obtain current directory contents
  Local $DirList = GetDirList($FilePath, "*.*")

 ; Are we at the root directory of the drive?
  If @WorkingDir == $DrivePath Then
   ; Have we been here before?
    If $BeenHere <> 1 Then
     ; No?  Well, we've been here now
      $BeenHere = 1
    Else
     ; Okay, let's exit this routine
      $BeenHere = 0
      Return 1
    EndIf
  EndIf

 ; Did we find any matches for the specified file?
  If $FileList <> -1 Then
   ; At least one match was found, let's do something
    DoFileFoundNotify($FileList)
  EndIf

 ; No matches for the specified file found?
  If $FileList == -1 Then
   ; Are there contents in the current directory?
    If $DirList <> -1 Then
     ; Reset variable for possible directories
      $NewDir = ""
     ; Search for possible subdirectories
      For $DirCount = 1 to (Ubound($DirList) - 1)
       ; Is the current object a directory?
        If StringInStr(FileGetAttrib($DirList[$DirCount]), "D") Then
         ; Check if we're at the root directory level
          If StringRight($FilePath, 1) <> "\" Then
           ; Append subdirectory name to current level
            $NewDir = $FilePath & "\" & $DirList[$DirCount]
          Else
           ; Append subdirectory name to current level
            $NewDir = $FilePath & $DirList[$DirCount]
          EndIf
         ; Are we able to go to the next directory?
          If ChangePath($NewDir) <> -1 Then
           ; Call this routine again from the new location
            FileSearchTree($NewDir, $FileName)
          EndIf
        EndIf
      Next
     ; A valid subdirectory wasn't found, let's go back up a level
      If $NewDir == "" Then
        ChangePath("..")
      EndIf
    EndIf
  EndIf
 ; If we're back at the root directory, exit function
  If @WorkingDir == $DrivePath Then
    Return 1
  EndIf
 ; We've parsed all directories within current tree
 ; Let's go back up a level to previous area
  If $FilePath <> $LastPath Then
    ChangePath($FilePath & "\..")
  EndIf
EndFunc


; Obtain a listing of directory information
Func GetDirList($FileSearchPath, $FileSearchName)
  Local $DirList = ""
  Local $search
  Local $file

 ; Check if specified file exists in current directory
  $search = FileFindFirstFile($FileSearchName)
 ; Okay, let's process the list of matches
  If $search <> -1 Then
    While 1
     ; Any more matches?
      $file = FileFindNextFile($search)
      If @error Then ExitLoop

     ; Filter special directories, just in case...
      If $file <> "." AND $file <> ".." Then
       ; Add file/directory information to list
        $DirList = $DirList & $file & @LF
      EndIf
    WEnd
 ; Close search stream
  FileClose($search)
  EndIf

 ; Display splash screen to let user know we're doing something...
  SearchSplash()

 ; Let's return any matches found
  If $DirList <> "" Then Return StringSplit($DirList, @LF)
 ; No matches found, return special value
  Return -1
EndFunc

; Change to specified directory
Func ChangePath($NewPath)
 ; Update visited directory history information
  Global $LastPath = @WorkingDir
  FileChangeDir($NewPath)
  Global $CurrPath = @WorkingDir

 ; Hmm, we didn't change directories - return notice
  If $CurrPath == $LastPath Then Return -1
  Return 1
EndFunc

; This function is called if a match is found
; (It's not pretty, but it gets the job done...)
Func DoFileFoundNotify($Listing)
    $FileNames = ""
   ; Parse list of matching files/directories
    For $FileCount = 1 to (Ubound($Listing) - 1)
     ; Make a readable list of matching files/directories
      $FileNames = $FileNames & $Listing[$FileCount] & @CR
    Next
   ; Turn off splash screen
    SplashOff()
   ; Display results
    MsgBox(4096, "Matching Files", "Directory: " & @WorkingDir & @CR & "Files: " & @CR & $FileNames)
EndFunc

; Displays a splash screen while searching for file
; (It's not pretty, but it gets the job done...)
Func SearchSplash()
; Make a spinning icon array
 Local $SplashIcon = StringSplit("////----\\\\||||", "")

; Update splashscreen current information
 SplashTextOn("Searching " & $SplashIcon[$SplashCount], @WorkingDir, 500, 40, 1, 1, 20, -1, 8, -1)
; Update spinning icon counter
 $SplashCount = $SplashCount + 1
; Reset counter if needed
 If $SplashCount > $SplashIcon[0] Then $SplashCount = 1
; Release some clock cycles to computer (Be nice!)
 Sleep(60)
EndFunc

Hope this helps!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...