Sign in to follow this  
Followers 0
hot202

FileFindFirstFile multiple extension

8 posts in this topic

is there anyway to use FileFindFirstFile with multiple extension so i can use it to find say .jpg and .bmp?

Share this post


Link to post
Share on other sites



I don't think you can. However if you just make it return any file it's easy to then check for multiple extentions with StringRegExp().

Share this post


Link to post
Share on other sites

so i would do a IF in the while loop? to see if its a .bmp or a .jpg?

Share this post


Link to post
Share on other sites

Yeah and then you can just Continueloop if the extention doesn't match.

Share this post


Link to post
Share on other sites

i cant seem to get it working

$search = FileFindFirstFile($nm_dir & '\' & $event_value & "\" & "*.*")
                        If $search = -1 Then
                            MsgBox(0, "Error", "No files/directories matched the search pattern")
                            Exit
                        EndIf
                        While 1
                            $file = FileFindNextFile($search)
                            If @error Then ExitLoop
                            $ConstR = StringRegExpReplace($file, "^.*\\", "")
                            $test = StringMid($ConstR, StringInStr($ConstR, ".") + StringLen("."))
                            Switch $test
                                Case "bmp"
                                    MsgBox(1,"test",$file & ".bmp")
                                Case "jpg"
                                    MsgBox(1,"test",$file & ".jpg")
                            EndSwitch
                        WEnd
                        FileClose($search)

Share this post


Link to post
Share on other sites

Try one of the following with the return from FileFindNextFile():

StringRegExp($file, ".*(bmp|jpg)\z") ;Returns 1 if *.bmp, or *.jpg. Returns 0 if not.
StringRight($file,4) ;Returns the last 4 characters of the filename for use with your switch. (add a period to your comparrison strings though)

You might want to double check to make sure you don't get a possitive on a folder it it has .jpg, or .bmp at the end. (daft I know)

Breakdown of the pattern:

".*(bmp|jpg)\z"
1: .            ;match any character
2: *            ;repeat 1 zero, or more times
3: (bmp|jpg)    ;match bmp Or JPG (I'm not sure how or knows what's part of the statements so I put brackets around it just to be sure. Seems to work without)
4: \z           ;match at end of file only.

Share this post


Link to post
Share on other sites

thank man

i wasnt sure about using StringRight($file,4) incase the extension is more then 3 but there aint any in windows are there.

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

There are plenty of longer extensions (docx, xlsx, java, etc), but as long as you don't want those extentions to return a match that doesn't matter.

edit:

If you want to return the full extention regardless of length you can use:

$pos = StringInStr($file, ".", 0, -1) ;search for last period
If $pos Then ;file has an extention
    $test = StringRight($file, StringLen($file) - $pos) ;get the extention
Else
    $test = "" ;create a blank string if the file has no extention
EndIf

Note that this doesn't return the leading "." as it's not needed anymore.

Edited by Tvern

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