Jump to content

File Searcher


Recommended Posts

Hello guys... im trying to find(i need source code)/make a program that searches for a specific file on the hard disk without specifying the path....

for example to search for a .doc file on all the hard disk.

can any1 point me in the right direction please? i`ve spent 2days searching but didnt find anything on the net.

-tnx

Edited by NoMercy
Link to comment
Share on other sites

Make the functions mentioned to create an filelist array from a folder, then go through it to isolate all the .doc files.

how can i do that? pls help im not that good with autoit... im still learning... if you have some time pls show me how.

Link to comment
Share on other sites

This one worked for me:

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

Global $sRet
Local $sPath = @WindowsDir
Local $sFindFile = "calc.exe"

$aRetArray =  _FindPathName($sPath, $sFindFile)
_ArrayDisplay($aRetArray)


; Searches all subfolders of $sPath for $sFindFile (* and ? wildcards accepted)
; Returns an array containing full path and name of all matches.
; Number of matches is in zero index of array
Func _FindPathName($sPath, $sFindFile)
    Local $sSubFolderPath, $iIndex, $aFolders
    $search = FileFindFirstFile($sPath & "\" & $sFindFile)
    $aFolders = _FileListToArray($sPath, "*", 2)
    While 1
        $file = FileFindNextFile($search)
        If @error Then
            ExitLoop
        Else
            $sRet &= $sPath & "\" & $file & "|"
        EndIf
    WEnd
    FileClose($search)
    For $iIndex = 1 To $aFolders[0]
        $sSubFolderPath = $sPath & "\" & $aFolders[$iIndex]
        $aFoldersSubs = _FileListToArray($sSubFolderPath, "*", 2)
        If IsArray($aFoldersSubs) Then _FindPathName($sSubFolderPath, $sFindFile)
    Next
    Return StringSplit(StringTrimRight($sRet,1), "|")
EndFunc  ;==>_FindPathName

What isn't working for you?

Link to comment
Share on other sites

Here's another, though a very weird, way to do it:

#include <constants.au3>
#include <Array.au3>

Dim $fileList[1]
$fileList[0] = ""

$searchString = InputBox("Enter search", "What files do you want to find?", "*.doc")
$cmd = 'dir \' & $searchString & ' /s | find /I "directory of"' 

$result = _CMDreturn($cmd)

If Not $result Or StringInStr($result, "File Not Found") Then
    MsgBox(0, "", "No Files Found!")
    Exit
EndIf

$directories = StringRegExp($result, '[a-zA-Z]:\\[^/:*?"<>|]*[\r\n]', 3)

For $i = 0 To UBound($directories) - 1
    $dir = StringTrimRight($directories[$i],2)
    
    ;ConsoleWrite("$dir = " & $dir & @CRLF)
    If Not FileChangeDir($dir) Then
        ConsoleWrite("Unable to change directory to: " & $dir & @CRLF)
        ExitLoop
    EndIf
    
    $search = FileFindFirstFile($searchString)
    ;ConsoleWrite("$search = " & $search & ", and @error = " & @error & @CRLF)
    
    If $search = -1 Then ExitLoop
    While 1
        $file = FileFindNextFile($search)
        If @error Then ExitLoop
        ;ConsoleWrite("$file = " & $file & @CRLF)
        _ArrayAdd($fileList, $dir & "\" & $file)
    WEnd
    FileClose($search)
Next

$fileList[0] = UBound($fileList) - 1
_ArrayDisplay($fileList)

Exit

Func _CMDreturn($sCommand) ; This function returns the output of a DOS command as a string
    Local $cmdreturn = ""
    Local $line, $stream
    $stream = Run(@ComSpec & " /c " & $sCommand, @SystemDir, @SW_HIDE, $STDERR_MERGED + $STDIN_CHILD)
    While 1 ; loop through the return from the command until there is no more
        $line = StdoutRead($stream)
        If @error Then ExitLoop
        $cmdreturn &= $line
    WEnd
    Return $cmdreturn
EndFunc   ;==>_CMDreturn

It's buggy but works for your purpose, probably won't work 100% of the time either...but since we're on the subject and nothing else is working for you I figure why not post it?

Link to comment
Share on other sites

ok im using this and its working :

but how can i copy all these .doc files to another destination or delete them (im using the FileMove() and the FileDelete() ) not sure what im doing wrong...

and tnx for the help ppl...

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

Global $sRet
Local $sPath = "e:\"
Local $sFindFile = "*.doc"

$aRetArray =  _FindPathName($sPath, $sFindFile)
_ArrayDisplay($aRetArray)


; Searches all subfolders of $sPath for $sFindFile (* and ? wildcards accepted)
; Returns an array containing full path and name of all matches.
; Number of matches is in zero index of array
Func _FindPathName($sPath, $sFindFile)
    Local $sSubFolderPath, $iIndex, $aFolders
    $search = FileFindFirstFile($sPath & "\" & $sFindFile)
    $aFolders = _FileListToArray($sPath, "*", 2)
    While 1
        $file = FileFindNextFile($search)
        If @error Then
            ExitLoop
        Else
            $sRet &= $sPath & "\" & $file & "|"
            


         FileMove($sRet,"c:\")                               ;move files    
         FileDelete($sRet)                          ; Delete files

        

                         EndIf
    WEnd
    FileClose($search)
    For $iIndex = 1 To $aFolders[0]
        $sSubFolderPath = $sPath & "\" & $aFolders[$iIndex]
        $aFoldersSubs = _FileListToArray($sSubFolderPath, "*", 2)
        If IsArray($aFoldersSubs) Then _FindPathName($sSubFolderPath, $sFindFile)
    Next
    Return StringSplit(StringTrimRight($sRet,1), "|")
EndFunc;==>_FindPathName
Edited by NoMercy
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...