Jump to content

[SOLVED] Search specific file or files on Hard disk


Recommended Posts

Hi

 

I have this piece of code i found in help file and it gives me partitions + other drives ( cd rom, usb, etc... )

So how can i make this now to loop through drives and search for file "test.txt", i tried with _FileListToArray() but it gives me error 1 ( Path was invalid ).

#include <Array.au3>
#include <File.au3>
#include <AutoItConstants.au3>

Global $DT_ALL = "ALL", $aArray = DriveGetDrive($DT_ALL), $file = "test.txt"

If @error Then
    ; An error occurred when retrieving the drives.
    MsgBox(0, "Error retrieving the drives", "Error : " & @error & ", Extended : " & @extended)
Else
    For $i = 0 To $aArray[0]

        ; Show all the drives found and convert the drive letter to uppercase.
        ConsoleWrite("Drives : " & StringUpper($aArray[$i]) & @CRLF) ; debug

;~      $aFileList = _FileListToArray(StringUpper($aArray[$i]) & "\")
;~      If @error = 1 Then
;~          MsgBox(0, "", "Path was invalid.")
;~          Exit
;~      EndIf
;~      If @error = 2 Then
;~          MsgBox(0, "", "Invalid $sFilter.")
;~          Exit
;~      EndIf
;~      If @error = 3 Then
;~          MsgBox(0, "", "Invalid $iFlag.")
;~          Exit
;~      EndIf
;~      If @error = 4 Then
;~          MsgBox(0, "", "No file(s) were found.")
;~          Exit
;~      EndIf

    Next
    _ArrayDelete($aArray, 0)
    _ArrayDisplay($aArray, "test") ; debug

;~     _ArrayDisplay($aFileList, "$aFileList")

EndIf

 

Edited by akira2891
Link to comment
Share on other sites

I tried this and i hear that it search on hard disk but it stops, no error, no result nothing. test.txt is on my D: drive so D:\text.txt but i dont get any errors or results

#include <Array.au3>
#include <File.au3>
#include <AutoItConstants.au3>

Global $DT_ALL = "ALL", $aArray = DriveGetDrive($DT_ALL), $file = "test.txt"

If @error Then
    ; An error occurred when retrieving the drives.
    MsgBox(0, "Error retrieving the drives", "Error : " & @error & ", Extended : " & @extended)
Else
    For $i = 0 To $aArray[0]

        ; Show all the drives found and convert the drive letter to uppercase.
;~      ConsoleWrite("Drives : " & StringUpper($aArray[$i]) & @CRLF) ; debug

        $aFileList = _FileListToArrayRec($aArray[$i] & "\", "test.txt", $FLTAR_FILESFOLDERS, $FLTAR_RECUR, $FLTAR_NOSORT, $FLTAR_FULLPATH)

        If @error Then
            ConsoleWriteError($aFileList)
        EndIf

    Next
    _ArrayDelete($aArray, 0)
;~  _ArrayDisplay($aArray, "test") ; debug

    _ArrayDisplay($aFileList, "$aFileList")

EndIf

 

Link to comment
Share on other sites

try this

 

msgbox(0,"",_Func_find_file_in_computer("jjjjjjjjjjjjjjjjjj"))

Func _Func_find_file_in_computer($File_name, $type = "file")
    If $type = "" Then Return SetError(1, 1)
    $get_all_driv = DriveGetDrive("All")
    If @error Then Return SetError(@error, 2)
    For $ii = 1 To $get_all_driv[0]
        $findfile = _Func_find_file_in_spath($get_all_driv[$ii], $File_name, $type)
        If Not @error Then Return $findfile
    Next
EndFunc   ;==>_Func_find_file_in_computer
Func _Func_find_file_in_spath($sch_Path, $File_name, $type)
    Local $hSearch = FileFindFirstFile($sch_Path & "\*.*")
    If @error Then Return SetError(1)
    Local $sFileName
    tooltip("find " & $File_name  & " in " & $sch_Path)
    While 1
        $sFileName = FileFindNextFile($hSearch)
        If @error Then ExitLoop

        If $type = "file" Then
            If Not @extended Then
                If $sFileName = $File_name Then Return $sch_Path & "\" & $sFileName
            Else
                $findfile = _Func_find_file_in_spath($sch_Path & "\" & $sFileName, $File_name, $type)
                If Not @error Then Return $findfile
                ContinueLoop
            EndIf
        Else
            If Not @extended Then
                
                ContinueLoop
            EndIf
        EndIf

    WEnd
    Return SetError(2)
EndFunc   ;==>_Func_find_file_in_spath

 

Link to comment
Share on other sites

  • Moderators

akira2891,

Of course it will not show anything - you keep overwriting the returned array as you test each drive, so there probably is nothing to display when you finally try to do so. Try this version:

#include <Array.au3>
#include <File.au3>
#include <AutoItConstants.au3>

Global $aArray = DriveGetDrive($DT_ALL), $file = "test.txt"

If @error Then
    ; An error occurred when retrieving the drives.
    MsgBox(0, "Error retrieving the drives", "Error : " & @error & ", Extended : " & @extended)
Else
    For $i = 1 To $aArray[0]

        $aFileList = _FileListToArrayRec($aArray[$i] & "\", $file, $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_NOSORT, $FLTAR_FULLPATH)

        If Not @error Then
            _ArrayDisplay($aFileList, "FileList for " & $aArray[$i])
        EndIf



    Next

EndIf

That finds the files for me.

Celtic88,

Why reinvent the wheel? We have a perfectly good recursive search function so no need to write another.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

This works and i see where it searching, thanks.

But i just wonder why my example dont works ? I get list of all drives, and convert drive letters to uppercase like 

$aArray[1] ; is C:
$aArray[2] ; is D:
$aArray[3] ; is E: ( dvd rom )
$aArray[4] ; is F: ( dvd rom )

now it just need to loop through them and search for a file but dont works.

Link to comment
Share on other sites

  • Moderators

akira2891,

 i just wonder why my example dont works ?

Did you see my post (and amended example) above?

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

akira2891,

Glad I could help.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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...