Jump to content

Searching Files In Any Dir..


Recommended Posts

Hi, I saw posted code on how to search for any particular file within a specific folder, and more code on how to find within an inputted folder, but Iwas wondering how to find a file you know the name of but not the directory.

know whati mean?

Link to comment
Share on other sites

JdeB has a findfiles.au3 script at the bottom of http://www.autoitscript.com/fileman/users/jdeb/jdeb_autoit_stuff.htm

If you want to search multiple drives, you might need to wrap it in a loop like this (untested):

$fileToFind = "something.txt"

For $i = Asc("C") To Asc("Z")
    $drive = Chr($i) & ":"
    If DriveStatus($drive) = "READY" Then
        $FILES = _GetFileList($drive, $fileToFind)
        For $X = 0 To UBound($FILES)-1
           MsgBox(0,'found:' & $X, $FILES[$X])
        Next 
    EndIf
Next
Edited by CyberSlug
Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!
Link to comment
Share on other sites

JdeB has a findfiles.au3 script at the bottom of http://www.autoitscript.com/fileman/users/jdeb/jdeb_autoit_stuff.htm

If you want to search multiple drives, you might need to wrap it in a loop like this (untested):

$fileToFind = "something.txt"

For $i = Asc("C") To Asc("Z")
    $drive = Chr($i) & ":"
    If DriveStatus($drive) = "READY" Then
        $FILES = _GetFileList($drive, $fileToFind)
        For $X = 0 To UBound($FILES)-1
           MsgBox(0,'found:' & $X, $FILES[$X])
        Next 
    EndIf
Next

ty slug, when I run thescript, it just does nothing but a quick flash. I thoughtmaybe it was because it seemed to be searching in "c:\winutil\autoit3\programs\test" so I triedchanging that to "c:\" and then I get an error.

Is this script working but I have to change things or? sorry I'm very new

Link to comment
Share on other sites

Here, try this... a bit diffrent aproach .. maybe more savage then JdeB but that's all i could do ;p Program should write the file/files you're searching into log file. Just change those 3 Global variables to fit your needs

#Include <Array.au3>
#include <Constants.au3>
#Include <Date.au3>
#include <File.au3>
Global $log_file = @ScriptDir & "\cd.log"
Global $what_to_search = "something.au3"
Global $search_where = "F:\"

_CDTOTXT($search_where)

Func _CDTOTXT($path_to_cdrom)
        $DirOutput = Run(@ComSpec & " /c DIR /A:D /S " & $path_to_cdrom, '', @SW_HIDE, 2)
        Local $DirOutputOnce
        While 1
            $DirData = StdoutRead($DirOutput)
            If @error Then ExitLoop
            If $DirData Then
                $DirOutputOnce &= $DirData
            Else
                Sleep(10)
            EndIf
        WEnd
    ; Remove spaces from output
        $DirOutputOnce = StringStripWS($DirOutputOnce, 3)
    ; Split output into array
        $DirSplit = StringSplit($DirOutputOnce, @CRLF,1)
        For $i = 1 To $DirSplit[0]
            If StringInStr($DirSplit[$i], $path_to_cdrom) Then
                $DirsLine = StringSplit($DirSplit[$i], ": ",1)
                If $DirsLine[0] = 2 Then; Testing amount of elements in array, if more then 2 Exits
                    If StringInStr($DirsLine[2], $path_to_cdrom) Then; Making sure that path exists in string
                        $cd_directories = $DirsLine[2]
                        
                        $search_all = FileFindFirstFile($cd_directories & "\" & $what_to_search); Searching for all files in directory
                        If $search_all = -1 Then
                        Else
                            
                            While 1
                                $search_file = FileFindNextFile($search_all)
                                If @error Then ExitLoop                             
                                $full_path_to_all =  $cd_directories & "\" & $search_file
                                _AddLogToFile($full_path_to_all)
                            WEnd
                        EndIF
                        FileClose($search_all)
                    EndIf
                EndIf
            EndIf
        Next
EndFunc
    
Func _AddLogToFile($Text)
        $file_log = FileOpen($log_file,1)
        FileWriteLine($file_log, "["& _NowTime(5) & "] - " & $Text & @CRLF)
        FileClose ( $file_log )
EndFunc

My little company: Evotec (PL version: Evotec)

Link to comment
Share on other sites

Here, try this... a bit diffrent aproach .. maybe more savage then JdeB but that's all i could do ;p Program should write the file/files you're searching into log file. Just change those 3 Global variables to fit your needs

I changed to C:\ and then ran it, Error on line 15 stdoutput ?

Link to comment
Share on other sites

I changed to C:\ and then ran it, Error on line 15 stdoutput ?

Files\AutoIt3\beta" /UserParams

>Running AU3Check (1.54.1.1) params: from:C:\Program Files\AutoIt3\beta

+>AU3Check ended.rc:0

>Running:(3.1.1.122):C:\Program Files\AutoIt3\beta\autoit3.exe "C:\dupa.au3"

+>AutoIT3.exe ended.rc:0

>Exit code: 0 Time: 8.617

You sure you're using beta.. it works fine here. Output in log file:

[04:28:08] - F:\Projects\Project.AU3\Weird.Tests\something.au3

[04:28:08] - F:\Projects\Project.AU3\Weird.Tests2\something.au3

My little company: Evotec (PL version: Evotec)

Link to comment
Share on other sites

Hi, I saw posted code on how to search for any particular file within a specific folder, and more code on how to find within an inputted folder, but Iwas wondering how to find a file you know the name of but not the directory.

know whati mean?

If you know the file, can I assume you know the extension? If so, this searches C:\ and all subdirectories for that extension, and then finds the file from the output.

#include <file.au3>
Global $ReadArray[2], $found = 0
; example file
$File = "welcome"
$Ext = ".html"

RunWait (@ComSpec & " /c dir c:\*" & $Ext & " /s /b > c:\outputfile.txt", "", @SW_HIDE)

_FileReadToArray ("c:\outputfile.txt", $ReadArray)

For $i = 1 To $ReadArray[0]
    If StringInStr($ReadArray[$i], $File & $Ext) Then
        MsgBox (0, "Found File", $ReadArray[$i])
        $found = 1
    EndIf
Next
If $found = 0 Then MsgBox (0, "Finished", "No Files Found")
FileDelete ("c:\outputfile.txt")

Edit - D'oh! Forgot #include.

Edited by greenmachine
Link to comment
Share on other sites

OK, now I'm sad...

When I try slugs with non-beta, it gives me error.

When I try slugs with beta, it just flashes. When I do a search for cd.log, there's nothing.

When I try greens with either, I get error....

What error? I used 3.1.1.118 and got no error.

Link to comment
Share on other sites

FileDelete(@DesktopDir & '\Search.txt')

FileWrite(@DesktopDir & '\Search.txt', FileSearch('C:'))

Func FileSearch($search_dir, $s_ext = '*.*', $sep_char = @CRLF, $last_line = False)

$allfiles = ''

$search_dir = StringReplace($search_dir & '\', '\\', '\')

$search = FileFindFirstFile($search_dir & '*.*')

While 1

$file = FileFindNextFile($search)

If $file = '' Then ExitLoop

$full_file = $search_dir & $file

$check_file = StringInStr(FileGetAttrib($full_file), 'D')

If $check_file <> 0 Then $allfiles &= FileSearch($full_file, $s_ext, $sep_char, True)

If $check_file = 0 Then

If $s_ext = '*.*' Then $allfiles &= $full_file & $sep_char

If $s_ext <> '*.*' And StringTrimLeft($s_ext, 1) = StringRight($full_file, StringLen(StringTrimLeft($s_ext, 1))) Then $allfiles &= $full_file & $sep_char

EndIf

WEnd

FileClose($search)

If Not $last_line Then $allfiles = StringTrimRight($allfiles, StringLen($sep_char))

Return $allfiles

EndFunc ;==>FileSearch

Edited by psandu.ro
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...