Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Recursive call and Exit Function

420 views
Skip to first unread message

w2kjl

unread,
Aug 19, 2001, 6:14:09 PM8/19/01
to
Hello,

I can't find the trick here.
I wrote this little recursive function (see code below) in order to search
for a file in a specified folder (with subfolders).

The message (6th line) always shows the correct result BUT the function
always returns FALSE.
Digging a bit, I realized the Exit Function statement was ignored and I
believe it is related to the recursive call below.
So, How can I really exit the function, including the recursive call for
browsing folders at the end of the function (otherwise, it will always give
a 'false' result) ?

THX

Function FindInFolder(afile, destfolder)
Dim fldr, tmpfile
Set fldr = fso.GetFolder(destfolder)

For Each tmpfile In fldr.Files
If (tmpfile.name = afile) Then
Wscript.Echo "file : " & tmpfile.name & " found"
FindInFolder = True
Exit Function 'Should exit here, right ? No ! Whyyyyyyyyyy ????
End If
Next

' If not found, calls recursively the search
For Each sfldr In fldr.SubFolders
Call FindInFolder(afile, sfldr)
Next

' Below : Other values or places doesn't mend anything...
FindInFolder = False
End Function

Johnny Nielsen

unread,
Aug 19, 2001, 7:23:56 PM8/19/01
to
"w2kjl" <w2k....@wanadoo.fr> wrote in message
news:OPpNCxPKBHA.1336@tkmsftngp04...

I wonder why so many people are afraid to use their real names. Maybe you
dare to tell me?

> I wrote this little recursive function (see code below) in order to
search
> for a file in a specified folder (with subfolders).
>
> The message (6th line) always shows the correct result BUT the function
> always returns FALSE.
> Digging a bit, I realized the Exit Function statement was ignored and I
> believe it is related to the recursive call below.
> So, How can I really exit the function, including the recursive call for
> browsing folders at the end of the function (otherwise, it will always
give
> a 'false' result) ?

There are a few bugs:

> Function FindInFolder(afile, destfolder)
> Dim fldr, tmpfile
> Set fldr = fso.GetFolder(destfolder)
>
> For Each tmpfile In fldr.Files
> If (tmpfile.name = afile) Then

This will eventually fail since you won't catch filenames that differs in
letter cases. Allways convert both sides to upper or lower case before
comparing for equality. Either 'if UCase(tmpfile.name) = UCase(afile) then'
or better yet, convert afile to upper case before entering the loop and
compare it with UCase(tmpfile.name) - that would be faster. Another thing -
you don't need the parentheses - it isn't C/Java/.. ;-)

> Wscript.Echo "file : " & tmpfile.name & " found"
> FindInFolder = True

Move the line above to the start of the function. I will tell you why
below..

> Exit Function 'Should exit here, right ? No ! Whyyyyyyyyyy ????
> End If
> Next
>
> ' If not found, calls recursively the search
> For Each sfldr In fldr.SubFolders
> Call FindInFolder(afile, sfldr)

Above is the primary problem - you don't allow the function to quit if a
match is found AND you pass the subfolder object instead of the path to the
function. Do it like this instead:

if FindInFolder(afile, sfldr.path) then exit function

If you move the 'FindInFolder = True' line to the start of the function the
line above will return the correct value (True).

> Next
>
> ' Below : Other values or places doesn't mend anything...
> FindInFolder = False
> End Function


Best regards
Johnny Nielsen

w2kjl

unread,
Aug 19, 2001, 9:54:36 PM8/19/01
to
Hi, Johnny
First, thanks for your answer.

> I wonder why so many people are afraid to use their real names. Maybe you
> dare to tell me?

I think it isn't a daring or not daring matter. There are lots of people in
real life that I wouldn't meet or even let them know my name. Why should it
be different here ? Anyway, my first name is Jean (John) if you wish to
know.

> you don't need the parentheses - it isn't C/Java/.. ;-)

You're right : coming from C and Delphi, VBS seems sometimes real smooth and
sometimes quite confusing...

> > ' If not found, calls recursively the search
> > For Each sfldr In fldr.SubFolders
> > Call FindInFolder(afile, sfldr)
>
> Above is the primary problem - you don't allow the function to quit if a
> match is found AND you pass the subfolder object instead of the path to
the
> function. Do it like this instead:
>
> if FindInFolder(afile, sfldr.path) then exit function
>
> If you move the 'FindInFolder = True' line to the start of the function
the
> line above will return the correct value (True).

I tried it and it actually works (of course).
The 'if FindInFolder(afile, sfldr.path) then exit function' is clear to me,
but I must say the move to the first line of 'FindInFolder = true' was
tricky. I wouldn't have found it ... and I didn't, btw. Clever tip.

Great help - Thanks a lot !
Cheers.

W2kjl (aka Jean) :-)

PS : I'm leaving tomorrow for five days, but I'll be back, as someone else
said...


Steve Fulton

unread,
Aug 20, 2001, 9:02:07 AM8/20/01
to
The Exit Function isn't ignored (put a WScript.Echo after it to verify
this). However, control returns to the function that called it (itself in
this case), where you then set its return value to False!

Capture the recursive call's return value and test it to see if the file was
found in a subfolder:

' If not found, calls recursively the search
For Each sfldr In fldr.SubFolders

FindInFolder = FindInFolder(afile, sfldr)
If FindInFolder Then Exit Function
Next

--
All great truths begin as blasphemies. -George Bernard Shaw

=-=-=
Steve
-=-=-

"w2kjl" <w2k....@wanadoo.fr> wrote in message
news:OPpNCxPKBHA.1336@tkmsftngp04...

w2kjl

unread,
Aug 23, 2001, 4:20:29 PM8/23/01
to
Well,
sorry, Johnny.
I posted an answer 5 days ago and just realize today it doesn't appear
here...
Don't know why.
Anyway, your tip is quite good, and works perfectly.
Many thanks.

"Johnny Nielsen" <DONT.WRIT...@megabit.dk> a écrit dans le message
news: vPXf7.17523$Ay1.2...@news000.worldonline.dk...

0 new messages