Sign in to follow this  
Followers 0
DarkBoost

DarkBoost : File Search

1 post in this topic

This is my first contribution script to the Forum, its a simple GUI File Search which hopefully someone will find handy or may learn something from.

As the title suggests it does a file search on a user input field for FileName and FilePath and provides the results in a Log Style window. I am sure

there are many thousands of ways to achieve and/or write this, this is my very simple way which was used in another project.

#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <StaticConstants.au3>
#include <File.au3>

Opt("MustDeclareVars", 1)                                                                                                                   ;1=Enabled

Global $title, $width, $height, $fname, $fsize, $temp, $line
Global $gui, $msg, $input_file, $input_path, $input_button, $search_button, $log_edit, $reset_button, $save_button

$title = "DarkBoost : File Search"
$width = 500
$height = 400
$fname = "Tahoma"
$fsize = 8
$temp = @TempDir & "\DarkBoost.txt"
$line = "================================================================"

$gui = GUICreate($title, $width, $height)
GUISetFont($fsize, "", "", $fname)
GUICtrlCreateLabel("Enter FileName...", 10, 10, 80, 13)
GUICtrlCreateLabel("Enter FilePath...", 140, 10, 80, 13)
$input_file = GUICtrlCreateInput("", 10, 25, 120, 18, BitOR($ES_LOWERCASE, $ES_AUTOHSCROLL))
$input_path = GUICtrlCreateInput("", 140, 25, $width-190, 18, BitOR($ES_LOWERCASE, $ES_AUTOHSCROLL))
$input_button = GUICtrlCreateButton("...", $width-40, 25, 30, 18)
$search_button = GUICtrlCreateButton("Search", $width-60, $height-30, 50, 18)
$log_edit = GUICtrlCreateEdit("", 10, 60, $width-20, 300)
$reset_button = GUICtrlCreateButton("Reset Fields", 10, $height-30, 70, 18)
$save_button = GUICtrlCreateButton("Save Log", 90, $height-30, 60, 18)
GUICtrlCreateLabel("Created by DarkBoost 2009", 160, $height-26, $width-230, 13, $SS_CENTER)                                                ;label name
GUICtrlSetColor(-1, 0x888888)                                                                                                               ;set label name color
GUICtrlSetTip($input_button, "Select File Path")                                                                                            ;tip for file path button
GUICtrlSetTip($reset_button, "Reset all Fields")                                                                                            ;tip for reset all button
GUICtrlSetTip($save_button, "Save data in log window to LOG file")                                                                          ;tip for save log button
GUICtrlSetTip($search_button, "Start searching for files")                                                                                  ;top for search button
GUICtrlSetData($log_edit, $title & @CRLF & "Supporting FileNames: [*.*] or [filename.*] or [*.txt] or [*.*,filename.*,*.txt]" & @CRLF, 1)   ;supporting filenames to log window
GUICtrlSetData($log_edit, $line & @CRLF, 1)
GUISetState(@SW_SHOW)

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $input_button
            input_button()

        Case $msg = $reset_button
            reset_button()

        Case $msg = $save_button
            save_button()

        Case $msg = $search_button
            search()

        Case $msg = $GUI_EVENT_CLOSE
            If FileExists($temp) Then FileDelete($temp)                                                                                     ;delete temp file if exists
            Exit
    EndSelect
WEnd

Func input_button()
    Local $folder
    $folder = FileSelectFolder("Select File Path", "")                                                                                      ;prompt user to select file path
    If $folder = "" Then Return                                                                                                             ;user clicks cancel or blank field
    If StringRight($folder, 1) <> "\" Then $folder &= "\"                                                                                   ;add '\' to end of file path if does not exist
    GUICtrlSetData($input_path, $folder)                                                                                                    ;set input path with results
EndFunc ;==>Button: Input Path

Func save_button()
    Local $save_file, $read_log, $file_open
    $save_file = FileSaveDialog($title, "", "Log File (*.log)", 16, "DarkBoost.log")                                                        ;prompt user to select save path 16=prompt to overwrite existing file
    If $save_file = "" Then Return                                                                                                          ;user clicks cancel or blank field
    If StringRight($save_file, 4) <> ".log" Then $save_file &= ".log"                                                                       ;add .log if missing from file name
    $read_log = GUICtrlRead($log_edit)                                                                                                      ;read data in log window
    $file_open = FileOpen($save_file, 2 + 16)                                                                                               ;open save file 2=erase previous data, 8=create file structure
    FileWrite($save_file, $read_log)                                                                                                        ;write log window data to save file
    GUICtrlSetData($log_edit, "Log File Saved: " & $save_file & @CRLF, 1)                                                                   ;write file saved to log window
    GUICtrlSetData($log_edit, $line & @CRLF, 1)
EndFunc ;==>Button: Save Log

Func reset_button()
    GUICtrlSetData($input_file, "")                                                                                                         ;reset file name input
    GUICtrlSetData($input_path, "")                                                                                                         ;reset file path input
    GUICtrlSetData($log_edit, "")                                                                                                           ;reset log window
    If FileExists($temp) Then FileDelete($temp)                                                                                             ;delete temp file if exists
    GUICtrlSetData($log_edit, $title & @CRLF & "Supporting FileNames: [*.*] or [filename.*] or [*.txt] or [*.*,filename.*,*.txt]" & @CRLF, 1)   ;supporting filenames to log window
    GUICtrlSetData($log_edit, $line & @CRLF, 1)
EndFunc ;==>Button: Reset Fields

Func search()
    Local $read_file, $read_path, $file_split, $file_open, $read_results, $count
    $read_file = GUICtrlRead($input_file)                                                                                                   ;read input file name
    $read_path = GUICtrlRead($input_path)                                                                                                   ;read input file path
    If StringRight($read_path, 1) <> "\" Then $read_file &= "\"                                                                             ;add '\' to end of file path if does not exist
    If $read_file = "" Or $read_path = "" Then                                                                                              ;check if there is data in FileName/FilePath
        MsgBox(48, $title, "Please enter both FileName & FilePath to continue...")
        Return
    EndIf
    GUICtrlSetData($log_edit, "Searching for file(s)... " & $read_file & @CRLF, 1)                                                          ;write file name to log window
    GUICtrlSetData($log_edit, "Searching in file path... " & $read_path & @CRLF, 1)                                                         ;write file path to log window
    GUICtrlSetData($log_edit, $line & @CRLF, 1)
    If FileExists($temp) Then FileDelete($temp)                                                                                             ;delete temp file if exists
    $file_split = StringSplit($read_file, ",")                                                                                              ;split file name for multiple file types
    $count = 0
    For $x = 1 To $file_split[0]
        RunWait(@ComSpec & " /c dir " & $read_path & $file_split[$x] & " /s /b > " & $temp, "", @SW_HIDE)                                   ;begin file search and save results to temp file
        $file_open = FileOpen($temp, 0)                                                                                                     ;open temp file 0=read only
        If $file_open = -1 Then                                                                                                             ;error check opening temp file
            MsgBox(16, $title, "Failed to open temp file: " & @CRLF & $temp)
            Return
        EndIf

        While 1
            $read_results = FileReadLine($file_open)                                                                                        ;read temp file line by line
            If @error = -1 Then ExitLoop                                                                                                    ;exit loop when reach end of file
            GUICtrlSetData($log_edit, $read_results & @CRLF, 1)                                                                             ;write results to log window
            $count = $count + 1                                                                                                             ;count loops
        WEnd
    Next
    GUICtrlSetData($log_edit, $line & @CRLF, 1)
    GUICtrlSetData($log_edit, "Total Files Found: " & $count & @CRLF, 1)                                                                    ;display results
    GUICtrlSetData($log_edit, $line & @CRLF, 1)
    If FileExists($temp) Then FileDelete($temp)                                                                                             ;delete temp file if exists
EndFunc ;==> Button: Begin Search

Share this post


Link to post
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
Sign in to follow this  
Followers 0