Sign in to follow this  
Followers 0
nhardel

problem with if..then..

5 posts in this topic

I am modifing a function based on weaponx recursivefilesearch(string based) my goal is to give the funtion two things file name and starting dir to looking in. I want an output of the full path to the file.

#cs ----------------------------------------------------------------------------
           
           AutoIt Version: 3.2.8.1
           Author:       WeaponX
           Modified:       Nhardel
           
           Script Function:
           Recursive file search (string based)

           RecursiveFileSearch($filename, $startdir)
                            $filename = file that you are looking for
                            $startdir = (optional) dir that you would like to start in as root
                                             possible make this search all drives if nothing is entered  (future)
           
           Notes:
           -Fastest thus far
           
#ce ----------------------------------------------------------------------------


#include <array.au3>
$timestamp = TimerInit()
$result = RecursiveFileSearch ("kleanz.exe",@ProgramFilesDir&"\special")
MsgBox(0, "","Path:" & $result & @CRLF & (TimerDiff($timestamp) / 1000) & " seconds");0.0902s / 2090 files

Func RecursiveFileSearch ($filename, $startDir = "C:", $depth = 0)
    If $depth = 0 Then Global $RFSstring = ""
    $search = FileFindFirstFile($startDir & "\*.*")
    If @error Then Return
;Search through all files and folders in directory
    While 1
        $next = FileFindNextFile($search)
        If @error Then ExitLoop
;If folder, recurse
        If StringInStr(FileGetAttrib($startDir & "\" & $next), "D") Then
            RecursiveFileSearch ($filename, $startDir & "\" & $next, $depth + 1)
        Else
;Check if filename matches file we're looking for
            $RFSstring = $startDir & "\" & $next
            $STRresult = StringInStr($RFSstring,$filename,2)
            ConsoleWrite($STRresult &"  "& $RFSstring & @CRLF)   ;debug
            if $STRresult <> 0 then return $RFSstring
        EndIf
    WEnd
EndFunc;==>RecursiveFileSearch

I can see from the console that stringinstr is doing its job correctly but I cant seem to get it to return the pathname based on the if statement

Any ideas :mellow:

Share this post


Link to post
Share on other sites



I am modifing a function based on weaponx recursivefilesearch(string based) my goal is to give the funtion two things file name and starting dir to looking in. I want an output of the full path to the file.

#cs ----------------------------------------------------------------------------
           
           AutoIt Version: 3.2.8.1
           Author:       WeaponX
           Modified:       Nhardel
           
           Script Function:
           Recursive file search (string based)

           RecursiveFileSearch($filename, $startdir)
                            $filename = file that you are looking for
                            $startdir = (optional) dir that you would like to start in as root
                                             possible make this search all drives if nothing is entered  (future)
           
           Notes:
           -Fastest thus far
           
#ce ----------------------------------------------------------------------------


#include <array.au3>
$timestamp = TimerInit()
$result = RecursiveFileSearch ("kleanz.exe",@ProgramFilesDir&"\special")
MsgBox(0, "","Path:" & $result & @CRLF & (TimerDiff($timestamp) / 1000) & " seconds");0.0902s / 2090 files

Func RecursiveFileSearch ($filename, $startDir = "C:", $depth = 0)
    If $depth = 0 Then Global $RFSstring = ""
    $search = FileFindFirstFile($startDir & "\*.*")
    If @error Then Return
;Search through all files and folders in directory
    While 1
        $next = FileFindNextFile($search)
        If @error Then ExitLoop
;If folder, recurse
        If StringInStr(FileGetAttrib($startDir & "\" & $next), "D") Then
            RecursiveFileSearch ($filename, $startDir & "\" & $next, $depth + 1)
        Else
;Check if filename matches file we're looking for
            $RFSstring = $startDir & "\" & $next
            $STRresult = StringInStr($RFSstring,$filename,2)
            ConsoleWrite($STRresult &"  "& $RFSstring & @CRLF)  ;debug
            if $STRresult <> 0 then return $RFSstring
        EndIf
    WEnd
EndFunc;==>RecursiveFileSearch

I can see from the console that stringinstr is doing its job correctly but I cant seem to get it to return the pathname based on the if statement

Any ideas :mellow:

Maybe this would do it. (Not tested)

#cs ----------------------------------------------------------------------------
          
           AutoIt Version: 3.2.8.1
           Author:       WeaponX
           Modified:       Nhardel
          
           Script Function:
           Recursive file search (string based)

           RecursiveFileSearch($filename, $startdir)
                            $filename = file that you are looking for
                            $startdir = (optional) dir that you would like to start in as root
                                             possible make this search all drives if nothing is entered  (future)
          
           Notes:
           -Fastest thus far
          
#ce ----------------------------------------------------------------------------


#include <array.au3>
$timestamp = TimerInit()
$result = RecursiveFileSearch ("kleanz.exe",@ProgramFilesDir&"\special")
MsgBox(0, "","Path:" & $result & @CRLF & (TimerDiff($timestamp) / 1000) & " seconds");0.0902s / 2090 files

Func RecursiveFileSearch ($filename, $startDir = "C:", $depth = 0)
    Local $res
    If $depth = 0 Then Global $RFSstring = ""
    $search = FileFindFirstFile($startDir & "\*.*")
    If @error Then Return
;Search through all files and folders in directory
    While 1
        $next = FileFindNextFile($search)
        If @error Then ExitLoop
;If folder, recurse
        If StringInStr(FileGetAttrib($startDir & "\" & $next), "D") Then
            $res = RecursiveFileSearch ($filename, $startDir & "\" & $next, $depth + 1)
            if $res <> "" then return $res
        Else
;Check if filename matches file we're looking for
            $RFSstring = $startDir & "\" & $next
            $STRresult = StringInStr($RFSstring,$filename,2)
            ConsoleWrite($STRresult &"  "& $RFSstring & @CRLF)  ;debug
            if $STRresult <> 0 then return $RFSstring
        EndIf
    WEnd
    Return ""
EndFunc;==>RecursiveFileSearch

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

I am modifing a function based on weaponx recursivefilesearch(string based) my goal is to give the funtion two things file name and starting dir to looking in. I want an output of the full path to the file.

CODE
#cs ----------------------------------------------------------------------------

AutoIt Version: 3.2.8.1

Author: WeaponX

Modified: Nhardel

Script Function:

Recursive file search (string based)

RecursiveFileSearch($filename, $startdir)

$filename = file that you are looking for

$startdir = (optional) dir that you would like to start in as root

possible make this search all drives if nothing is entered (future)

Notes:

-Fastest thus far

#ce ----------------------------------------------------------------------------

#include <array.au3>

$timestamp = TimerInit()

$result = RecursiveFileSearch ("kleanz.exe",@ProgramFilesDir&"\special")

MsgBox(0, "","Path:" & $result & @CRLF & (TimerDiff($timestamp) / 1000) & " seconds");0.0902s / 2090 files

Func RecursiveFileSearch ($filename, $startDir = "C:", $depth = 0)

If $depth = 0 Then Global $RFSstring = ""

$search = FileFindFirstFile($startDir & "\*.*")

If @error Then Return

;Search through all files and folders in directory

While 1

$next = FileFindNextFile($search)

If @error Then ExitLoop

;If folder, recurse

If StringInStr(FileGetAttrib($startDir & "\" & $next), "D") Then

RecursiveFileSearch ($filename, $startDir & "\" & $next, $depth + 1)

Else

;Check if filename matches file we're looking for

$RFSstring = $startDir & "\" & $next

$STRresult = StringInStr($RFSstring,$filename,2)

ConsoleWrite($STRresult &" "& $RFSstring & @CRLF) ;debug

if $STRresult <> 0 then return $RFSstring

EndIf

WEnd

EndFunc;==>RecursiveFileSearch

I can see from the console that stringinstr is doing its job correctly but I cant seem to get it to return the pathname based on the if statement

Any ideas :(

This must be old weaponx, 'cause new weaponx knows better than to declare Global variables in a function! :mellow:

Your match is being found during a recursion, and gets lost when passed back to the parent iteration because you were not saving the return from the recursive call. Also, the test for a match is not performed if a recursive call is made. This is fixed:

;----------------------------------------------------------------------------
           AutoIt Version: 3.2.8.1
           Author:       WeaponX
           Modified:       Nhardel
           Remodified:  PsaltyDS
           
           Script Function:
           Recursive file search (string based)

           RecursiveFileSearch($filename, $startdir)
                            $filename = file that you are looking for
                            $startdir = (optional) dir that you would like to start in as root
                                             possible make this search all drives if nothing is entered  (future)
           
           Notes:
           -Fastest thus far
;----------------------------------------------------------------------------
Func RecursiveFileSearch($filename, $startDir = "C:", $depth = 0)
    Local $RFSstring = "", $search, $STRresult
    
    Local $search = FileFindFirstFile($startDir & "\*.*")
    If @error Then Return SetError(1, 0, $RFSstring)
    
;Search through all files and folders in directory
    While 1
        $next = FileFindNextFile($search)
        If @error Then ExitLoop
        
        If StringInStr(FileGetAttrib($startDir & "\" & $next), "D") Then
        ;If folder, recurse
            $RFSstring = RecursiveFileSearch($filename, $startDir & "\" & $next, $depth + 1)
        Else
        ; Else test this filename
            $RFSstring = $startDir & "\" & $next
        EndIf
        
    ;Check if filename matches file we're looking for
        $STRresult = StringInStr($RFSstring, $filename, 2)
        If $STRresult <> 0 Then Return $RFSstring
    WEnd
EndFunc  ;==>RecursiveFileSearch

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Thanks PsaltyDS that did the trick. Although I'm not sure that I understand the logic in yours versus mine. I thought the best was was not to worry about the RFString when it was looking at folders and only do the comparison when it found a file. This is what lead me to search the string in the else statement. If i get your reasoning your saying I cant do this because the string wasnt declared yet. Sorry if I sound more confused than when I posted original but I'm pretty sure I am more confused now. :mellow:

Share this post


Link to post
Share on other sites

Thanks PsaltyDS that did the trick. Although I'm not sure that I understand the logic in yours versus mine. I thought the best was was not to worry about the RFString when it was looking at folders and only do the comparison when it found a file. This is what lead me to search the string in the else statement. If i get your reasoning your saying I cant do this because the string wasnt declared yet. Sorry if I sound more confused than when I posted original but I'm pretty sure I am more confused now. :mellow:

As a general rule it bad practice and inviting trouble to declare Global variables inside a function. Save yourself some hard-to-debug problems and just say 'No' to global declarations in functions.

That was not what broke the script you had, however.

Follow the logic in your version:

What happens when you find a subdirectory? It calls a recursive iteration.

When that recursive iteration is done and returns to the parent that called it, what happens to the result?

Note: RETURN DOES NOT EXIT THE ENTIRE RECURSIVE TREE -- IT ONLY EXITS THAT INSTANCE AND RETURNS TO THE INSTANCE THAT CALLED IT.

That's what broke your script

:(


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

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