Sign in to follow this  
Followers 0
Charly-G

How To Find The Subdir, Where A Specific File Exists?

2 posts in this topic

Hello, all,

i was checking the forum for a routine to find a subfolder where the first occurance of a file-pattern is found.

There were a lot of very complex and complicated solutions for that, but i never found a "smart way".

So i wrote my own routine to do that and i did it in a smart recursive function.

Here is my code:

;A recursive SearchFile-Subroutine

;returns the first path under root $A, where the occurence of file-pattern $B was found

$a = "c:"

$b = "*.xml"

$c=FilePath($a,$:think:

if $c <> "0" then

MsgBox(0,"found in path",$c)

Else

MsgBox(0,"not found","Sorry!")

EndIf

Exit

func FilePath($path,$file)

local $s, $pf

if fileexists($path) then

$attrib=FileGetAttrib($path)

if not @error then

if StringInStr($attrib, "D") then

$s=FileFindFirstFile($path & "\" & $file)

FileClose($s)

if $s<>-1 then return $path

$s=FileFindFirstFile($path & "\*.*")

While 1

$f = FileFindNextFile($s)

If @error Then ExitLoop

if not ($f="." or $f="..") then

$pf=$path & "\" & $f

$fp=FilePath($pf,$file)

if $fp<>"0" then return $fp

EndIf

WEnd

FileClose($s)

EndIf

endif

EndIf

EndFunc

Share this post


Link to post
Share on other sites



Hello, again,

found a small bug in my function, when you try to read a folder without permission for that.

So here is the correction (change 1 line):

wrong line:

"while 1"

correct line:

"while $s<>-1"

then everything works fine!

Greetings:

Charly-G

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