Jump to content

Find a specific file


Recommended Posts

Hi friends,

I wrote a script to give the path of given file name.Here is my script

$drives=DriveGetDrive("all")
    For $i = 1 to $drives[0]
        FileChangeDir($drives[$i])
        $search=FileFindFirstFile("firefox.exe")
        if $search =-1 Then
            ContinueLoop
        Else
            $path=FileFindNextFile($search) 
            ExitLoop
        EndIf
    Next
MsgBox(0,"File path is",$path)
.But instead of returning file path,it returns file name.Whats wrong with my code. Edited by Mecrazycoder

[size="4"][font="Arial Narrow"][font="Garamond"]Attitude is a little thing that makes a big difference[/font][/font][/size][indent][/indent]

Link to comment
Share on other sites

Hi friends,

I wrote a script to give the path of given file name.Here is my script

$drives=DriveGetDrive("all")
    For $i = 1 to $drives[0]
        FileChangeDir($drives[$i])
        $search=FileFindFirstFile("firefox.exe")
        if $search =-1 Then
            ContinueLoop
        Else
            $path=FileFindNextFile($search) 
            ExitLoop
        EndIf
    Next
MsgBox(0,"File path is",$path)
.But instead of returning file path,it returns file name.Whats wrong with my code.

What you write can be done differently. For example,

$sPath = ''
$aDrives = DriveGetDrive('ALL')
If IsArray($aDrives) Then
    For $i = 1 to $aDrives[0]
        If FileExists($aDrives[$i] & '\firefox.exe') Then
            $sPath = $aDrives[$i] & '\firefox.exe'
            ExitLoop
        EndIf
    Next
EndIf
MsgBox(0, 'File path is', $sPath)

Your script looks for the file "firefox.exe" only in the root of each detected device. I think that you would like something else, dude.

Edited by Yashied
Link to comment
Share on other sites

What you write can be done differently. For example,

$sPath = ''
$aDrives = DriveGetDrive('ALL')
If IsArray($aDrives) Then
    For $i = 1 to $aDrives[0]
        If FileExists($aDrives[$i] & '\firefox.exe') Then
            $sPath = $aDrives[$i] & '\firefox.exe'
            ExitLoop
        EndIf
    Next
EndIf
MsgBox(0, 'File path is', $sPath)

Your script looks for the file "firefox.exe" only in the root of each detected device. I think that you would like something else, dude.

Dude,I am afraid this not i want.I want to find the full path of file specified.Anyway tanx for your response.

[size="4"][font="Arial Narrow"][font="Garamond"]Attitude is a little thing that makes a big difference[/font][/font][/size][indent][/indent]

Link to comment
Share on other sites

I think the FileFindNextFile($search) will return only the filename.

In the Help file it said:

Success: Returns a filename according to a previous call to FileFindFirstFile.

[font="Palatino Linotype"][size="2"]*** The information contained in this post should be considered and certified WORKS ON MY MACHINE ***[/size][/font][font="Palatino Linotype"][size="2"] [/size][/font]
Link to comment
Share on other sites

I think the FileFindNextFile($search) will return only the filename.

In the Help file it said:

Success: Returns a filename according to a previous call to FileFindFirstFile.

Yes you are right. I need to find the full path of the given file

[size="4"][font="Arial Narrow"][font="Garamond"]Attitude is a little thing that makes a big difference[/font][/font][/size][indent][/indent]

Link to comment
Share on other sites

  • Moderators

Mecrazycoder,

I am afraid recursive file searching is a bit more complicated than you think! :D This script will give you the full path of a given file name:

$sFile = "firefox.exe"  ; or any other filename WITHOUT a path
$aDrives = DriveGetDrive('ALL')

If IsArray($aDrives) Then
    For $i = 1 to $aDrives[0]
        ConsoleWrite("Searching " & $aDrives[$i] & @CRLF)
        _RecFileFinder($aDrives[$i], $sFile)
    Next
EndIf

Func _RecFileFinder($sDrive = "", $sFile = "")

    Local $asFolderList[3] = [1], $sCurrentPath, $hSearch, $sName, $fFolder

    ; Check parameters
    If $sDrive = "" Or $sFile = "" Then Return SetError(1, 0, 0)

    ; Ensure trailing \
    If StringRight($sDrive, 1) <> "\" Then $sDrive = $sDrive & "\"
    ; Add path to folder list
    $asFolderList[1] = $sDrive

    ; Search in listed folders
    While $asFolderList[0] > 0

        ; Set path to search
        $sCurrentPath = $asFolderList[$asFolderList[0]]
        ConsoleWrite("Searching: " & $sCurrentPath & @CRLF)
        ; Reduce folder array count
        $asFolderList[0] -= 1
        ; Get search handle
        $hSearch = FileFindFirstFile($sCurrentPath & "*")
        ; If folder empty move to next in list
        If $hSearch = -1 Then ContinueLoop

        ; Search folder
        While 1
            $sName = FileFindNextFile($hSearch)
            ; Check for end of folder
            If @error Then ExitLoop
            ; Check for subfolder
            $fFolder = @extended ; @extended set in 3.3.1.1 +  <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
            ;$fFolder = StringInStr(FileGetAttrib($sCurrentPath & $sName), "D") ; pre 3.3.1.1 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<

            ; Add subfolder to folder list
            If $fFolder Then
                ; Increase folder array count
                $asFolderList[0] += 1
                ; Double folder array size if too small (fewer ReDim needed)
                If UBound($asFolderList) <= $asFolderList[0] + 1 Then ReDim $asFolderList[UBound($asFolderList) * 2]
                ; Add subfolder to list
                $asFolderList[$asFolderList[0]] = $sCurrentPath & $sName & "\"
            EndIf

            ; Check filename against required value
            If Not $fFolder And $sName = $sFile Then

                ;This is where you can do what you want with the found files
                MsgBox(0, "Result", "Found " & $sCurrentPath & $sName)

            EndIf
        WEnd

        ; Close current search
        FileClose($hSearch)

    WEnd

    Return 1

EndFunc   ;==>_RecFileFinder

If you are running the Beta version of AutoIt, the script will work. If you are still using 3.3.0.0 or earlier, you need to alter it slightly - look for the <<<<<<<<<<<<<<< lines.

The script tells you how the search is going in the SciTE console and pops up a messagebox when it finds a matching file.

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

Mecrazycoder,

I am afraid recursive file searching is a bit more complicated than you think! :D This script will give you the full path of a given file name:

$sFile = "firefox.exe"  ; or any other filename WITHOUT a path
$aDrives = DriveGetDrive('ALL')

If IsArray($aDrives) Then
    For $i = 1 to $aDrives[0]
        ConsoleWrite("Searching " & $aDrives[$i] & @CRLF)
        _RecFileFinder($aDrives[$i], $sFile)
    Next
EndIf

Func _RecFileFinder($sDrive = "", $sFile = "")

    Local $asFolderList[3] = [1], $sCurrentPath, $hSearch, $sName, $fFolder

    ; Check parameters
    If $sDrive = "" Or $sFile = "" Then Return SetError(1, 0, 0)

    ; Ensure trailing \
    If StringRight($sDrive, 1) <> "\" Then $sDrive = $sDrive & "\"
    ; Add path to folder list
    $asFolderList[1] = $sDrive

    ; Search in listed folders
    While $asFolderList[0] > 0

        ; Set path to search
        $sCurrentPath = $asFolderList[$asFolderList[0]]
        ConsoleWrite("Searching: " & $sCurrentPath & @CRLF)
        ; Reduce folder array count
        $asFolderList[0] -= 1
        ; Get search handle
        $hSearch = FileFindFirstFile($sCurrentPath & "*")
        ; If folder empty move to next in list
        If $hSearch = -1 Then ContinueLoop

        ; Search folder
        While 1
            $sName = FileFindNextFile($hSearch)
            ; Check for end of folder
            If @error Then ExitLoop
            ; Check for subfolder
            $fFolder = @extended ; @extended set in 3.3.1.1 +  <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
            ;$fFolder = StringInStr(FileGetAttrib($sCurrentPath & $sName), "D") ; pre 3.3.1.1 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<

            ; Add subfolder to folder list
            If $fFolder Then
                ; Increase folder array count
                $asFolderList[0] += 1
                ; Double folder array size if too small (fewer ReDim needed)
                If UBound($asFolderList) <= $asFolderList[0] + 1 Then ReDim $asFolderList[UBound($asFolderList) * 2]
                ; Add subfolder to list
                $asFolderList[$asFolderList[0]] = $sCurrentPath & $sName & "\"
            EndIf

            ; Check filename against required value
            If Not $fFolder And $sName = $sFile Then

                ;This is where you can do what you want with the found files
                MsgBox(0, "Result", "Found " & $sCurrentPath & $sName)

            EndIf
        WEnd

        ; Close current search
        FileClose($hSearch)

    WEnd

    Return 1

EndFunc   ;==>_RecFileFinder

If you are running the Beta version of AutoIt, the script will work. If you are still using 3.3.0.0 or earlier, you need to alter it slightly - look for the <<<<<<<<<<<<<<< lines.

The script tells you how the search is going in the SciTE console and pops up a messagebox when it finds a matching file.

M23

Tanx for replying man.But the script you provided is not finding the location of the given file. Edited by Mecrazycoder

[size="4"][font="Arial Narrow"][font="Garamond"]Attitude is a little thing that makes a big difference[/font][/font][/size][indent][/indent]

Link to comment
Share on other sites

@Mecrazycoder

Melba23 is rights, you need to use a recursive calls, here's a simple example (but it may works slowly):

#Include <File.au3>

$sRoot = 'C:'
$sFile = 'shell32.dll'

MsgBox(0, '', 'Path for ' & $sFile & ' is "' & _FindFile($sRoot, $sFile) & '"')

Func _FindFile($sRoot, $sFile)

    If FileExists($sRoot & '\' & $sFile) Then
        Return $sRoot
    EndIf

    Local $Result = '', $FileList = _FileListToArray($sRoot, '*', 2)

    If Not @error Then
        For $i = 1 To $FileList[0]
            $Result = _FindFile($sRoot & '\' & $FileList[$i], $sFile)
            If Not @error Then
                ExitLoop
            EndIf
        Next
    EndIf
    Return SetError(($Result = ''), 0, $Result)
EndFunc   ;==>_FindFile
Edited by Yashied
Link to comment
Share on other sites

  • Moderators

Mecrazycoder,

The script does work, promise! I tested it several times and it is based on code I use every day.

Did you alter the commented/live lines marked with <<<<<<<<< so only the one valid for your Autoit version was live?

Is the file actually on your system?

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

Mecrazycoder,

The script does work, promise! I tested it several times and it is based on code I use every day.

Did you alter the commented/live lines marked with <<<<<<<<< so only the one valid for your Autoit version was live?

Is the file actually on your system?

M23

Ok dude let me check it.

[size="4"][font="Arial Narrow"][font="Garamond"]Attitude is a little thing that makes a big difference[/font][/font][/size][indent][/indent]

Link to comment
Share on other sites

@Mecrazycoder

Melba23 is rights, you need to use a recursive calls, here's a simple example (but it may works slowly):

#Include <File.au3>

$sRoot = 'C:'
$sFile = 'shell32.dll'

MsgBox(0, '', 'Path for ' & $sFile & ' is "' & _FindFile($sRoot, $sFile) & '"')

Func _FindFile($sRoot, $sFile)

    If FileExists($sRoot & '\' & $sFile) Then
        Return $sRoot
    EndIf

    Local $Result = '', $FileList = _FileListToArray($sRoot, '*', 2)

    If Not @error Then
        For $i = 1 To $FileList[0]
            $Result = _FindFile($sRoot & '\' & $FileList[$i], $sFile)
            If Not @error Then
                ExitLoop
            EndIf
        Next
    EndIf
    Return SetError(($Result = ''), 0, $Result)
EndFunc   ;==>_FindFile

Awesome dude its working.... :D

[size="4"][font="Arial Narrow"][font="Garamond"]Attitude is a little thing that makes a big difference[/font][/font][/size][indent][/indent]

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