Jump to content

FileFindFirstFile multiple extension


hot202
 Share

Recommended Posts

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)
Link to comment
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.
Link to comment
Share on other sites

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