Jump to content

Check to see WHERE a file exists


copyleft
 Share

Recommended Posts

I'm looking to create a script that searches all drives for a particular file, say, "settings.ini" then sets a variable for that file location that I can use to run programs or commands, much like this batch file code:

FOR %%d IN (D E F G H I J K L M N O P Q R S T U V W X Y Z) DO IF EXIST %%d:\Settings.ini SET CFDrv=%%d:

del %CFDrv%\*.txt
yada, yada, yada...

 

Link to comment
Share on other sites

#include <Array.au3>

Global $aDrives = DriveGetDrive("ALL")

_ArrayDelete($aDrives, 0)

For $i = 0 To UBound($aDrives) - 1
    Local $sFoundPath = ""
    ConsoleWrite("Searching " & $aDrives[$i] & "\ drive for explorer.exe" & @CRLF)

    If (FileFindFile(StringUpper($aDrives[$i]), "explorer.exe", $sFoundPath)) Then Exit MsgBox("", "", "Found explorer.exe in " & $sFoundPath)
Next

Func FileFindFile($sStartDir, Const ByRef $vValue, ByRef $sOutString, $iDepth = 0)
    Local $hSearch = FileFindFirstFile($sStartDir & "\*.*")

    If (@error) Then Return

    While (True)
        Local $hNext = FileFindNextFile($hSearch)

        If (@error) Then ExitLoop

        ;If folder, recurse
        If (StringInStr(FileGetAttrib($sStartDir & "\" & $hNext), "D")) Then
            FileFindFile($sStartDir & "\" & $hNext, $vValue, $sOutString, $iDepth + 1)
        Else
            ; Set the path to the output
            If ($hNext = $vValue) Then
                $sOutString = $sStartDir & "\"
                ExitLoop
            EndIf
        EndIf
    WEnd

    FileClose($hSearch)

    Return ($iDepth ? False : True)
EndFunc   ;==>RecursiveFileSearch

Didn't do the same thing a while back but did do something where I was looking for something on a computer and needed to find it (It wasn't explorer.exe lol)

Edited by InunoTaishou
Link to comment
Share on other sites

copyleft,

Another alternative (probably much faster)...

#include <Array.au3>

local $aDrives = DriveGetDrive("ALL")
local $sFile = 'sdsf.* mv1.*'
local $out

For $i = 1 To UBound($aDrives) - 1
    ConsoleWrite("Searching " & $aDrives[$i] & ' for ' & $sFile & @CRLF)
    _srch($aDrives[$i], $sFile)
Next

$out = stringregexpreplace($out,'(?i)(\w:\\>)|(file not found)','')
$out = stringregexpreplace($out, '\R+', @CRLF)

ConsoleWrite($out & @CRLF)

func _srch($drv, $fle)

    if DriveStatus($drv) <> 'READY' then return

    $DOS = Run(@ComSpec & ' /k ' & $drv & ' & cd\ & dir /s /b ' & $sFile, '', '', $STDERR_MERGED)

    While 1
        $out &= StdoutRead($DOS)
        If @error then exitloop
    WEnd

endfunc

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Too late here to start testing speeds.  Given the breadth of the search I think DIR is faster than any "find first...find next" solution.  Besides, started out because I was bored, now just tired.  Good Night...

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

@kylomas. Very nice script. Many thanks. Is there anyway to constrain the number of drives to speed up the script. In my usage, I know the file is at least not going to be on drives B: or C:. Also, I assume at line 32, I can enter my command based on the drive found?

Edited by copyleft
Link to comment
Share on other sites

  • Moderators

Look at DriveGetDrive in the help file, you can modify the "ALL" parameter based on the type of disk, such as all "FIXED" drives. Alternatively, you could simply create your own array of drives to search and pass that parameter to the search function.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

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