Using ColdFusion and RegEx on a special file naming convention

I had a project where we had a graphics library, and a bunch of zip files were placed in a directory. The zip files contained 1 or more images packaged up. It was mostly used for product shots to show views of front, back, left, or right. Each zip file could have an associated thumbnail preview image. The files followed a special naming convention, with file names in a format of: SUBJ_DESC_VIEW_FORMAT_TYPE.zip. I used a RegEx (regular expression), as well as the Find() function to parse the file name.

SUBJ is the subject, and should describe the file, and helps order the files alphabetically in the directory.

DESC is the description, such as: LOGO, Model, Version-3, etc. (hint: you can lengthen the description with up to 3 dashes -'s)

VIEW is optional, and can be either: Ft (front), Rt (right), Lt (left), or Bk (back)

FORMAT can be either: PPT, PRINT, or WEB

TYPE is optional, and can be either: EPS, JPG, TIF

A thumbnail preview can be used, and must be named: SUBJ_DESC_VIEW_thumb.gif (or .jpg). If VIEW was used for the zip file name, it must also be used in the thumbnail file name.

Example zip file: UTM_1100D_Ft_PRINT_EPS.zip

Example thumb file: UTM_1100D_Ft_thumb.gif

Here was the code I used to parse the file name and display a friendly version without dashes to the user.

<cfsavecontent variable="file_desc">
<cfset found = REFind("\_[[:alnum:]]+\-*[[:alnum:]]*\-*[[:alnum:]]*\-*[[:alnum:]]*\-*[[:alnum:]]*\-*[[:alnum:]]*\_",name,0,"TRUE")>

<!--- grab the description out of the file name, replacing dashes with spaces --->
<cfif found.pos[1]>#Replace(Mid(name,found.pos[1]+1,found.len[1]-2),"-"," ","ALL")#</cfif>

<!--- tell user which view --->
<cfif FindNoCase("_Ft",name,1)>
   (Front view)
<cfelseif FindNoCase("_Rt",name,1)>
   (Right view)
<cfelseif FindNoCase("_Lt",name,1)>
   (Left view)
<cfelseif FindNoCase("_Bk",name,1)>
   (Back view)
</cfif>

<!--- tell user what file type(s) --->
<cfif Find("_EPS",name,1)>
   (EPS High-Res)
<cfelseif Find("_JPG",name,1)>
   (JPG High-Res)
<cfelseif Find("_TIF",name,1)>
   (TIFF High-Res)
<cfelseif Find("_PPT",name,1)>
   (PowerPoint)
<cfelseif Find("_WEB",name,1)>
   (WEB Low-Res)
<cfelseif Find("_PRINT",name,1)>
   (PRINT High-Res)
</cfif>
</cfsavecontent>
<cfif Not Len(Trim(file_desc))>
   File: #name#
<cfelse>
   #file_desc#
</cfif>
Here is an example of what the parsing accomplished.

Example zip file: UTM_Model-1100D-Silver_Ft_PRINT_EPS.zip

The user would see: Model 1100D Silver (Front view) (EPS High-Res)

Comments
BlogCFC was created by Raymond Camden. This blog is running version 5.9.002. Contact Blog Owner