Sign in to follow this  
Followers 0
copyleft

Check to see WHERE a file exists

9 posts in this topic

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

 

Share this post


Link to post
Share on other sites



Is that searching the root folder only?


Forum Rules         Procedure for posting code

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

#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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

And what about _FileListToArrayRec()  ?   :)

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

@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

Share this post


Link to post
Share on other sites

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.


When you're dead, you don't know you're dead - it's only difficult for those that know you. It's the same way when you're stupid...

My Scripts: SCCM UDFInclude Source with Compiled Script, Windows Firewall UDF

Share this post


Link to post
Share on other sites

@JLogan3o13. Ok adding the removable drive parameter with DriveGetDrive helps a lot. I'm still confused about where to enter my command,  I don't want to output the file name to DOS as kylomas has it. 

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