Image File name extension

We have images that were automatically migrated to the CMS. However, for the filename we did included th extension (.jpg, .gif, etc) instead of just keeping the filename.

What is the best way to remove the extension from the filename as in the publish location scheme it is now adding the img ext also to the filename. This is causing the filename to have double extensions.

thanks
Manvinder

This works for publishing without actually changing the underlying values. It gets the substring of end the $filename from the last occurrence of a period, if any. Then it compares that substring to the value stored in $extension. If the two match, then $extension is set to an empty string which suppresses redundancy of the file extension in output. Allows for when the ext string does include a period as well as when it doesn’t. Love to hear feedback, improvements, or alternatives.

$ct = $sys.item.definition.internalName; 
$id = $sys.item.getProperty("sys_contentid").String;

$extension=$rx.location.getFirstDefined($sys.item,'rx:sys_ext,rx:item_file_attachment_ext,rx:img1_ext','.html'); 

$filename = $rx.location.getFirstDefined($sys.item, 'rx:filename,rx:item_file_attachment_filename,rx:img1_filename', 'item'+$id);

if( $user.psoRelationships.isLandingPageGuid($user.psoObjectFinder.getGuidById($id)) )
{$filename = "index";}
else 
{$filename=$filename.replaceAll(' ','-').replaceAll(':','_').replaceAll('#','').replaceAll("\'","").replaceAll(",","").replaceAll("--","").replaceAll('\)','').replaceAll('\(','').replaceAll('\"','').replaceAll('\?','');}

$n = $filename.lastIndexOf('.');
$n1 = $tools.math.add($n, 1);
$t = $filename.substring($n);
$t1 = $filename.substring($n1);

if($extension==$t || $extension==$t1){$extension="";}

$sys.pub_path+$filename+$extension;

Often we just have a separate location scheme for images and files and just don’t add the extension field for these types. I have used something like the following before to strip characters and cleanup
If you want to do this on a field checkout the updated PSOCopyParameter pre extension in the PSOToolkit, this now has many options for copying fields and stripping of the extension and/or cleaning out special characters on the way. https://github.com/percussion/PSOToolkit/blob/master/src/com/percussion/pso/transform/PSOCopyParameter.java



$cleanpath= $sys.variables.rxs_urlroot +$sys.pub_path + $sys.template.prefix + $sys.item.getProperty('rx:filename').String;
$cleanpath=$cleanpath.replaceAll("[&]", " and ");
$cleanpath=$cleanpath.replaceAll("[^0-9a-zA-Z-_/ \.\\]", "");
$extIndex = $cleanpath.lastIndexOf(".");
$cleanpath=$cleanpath.replaceAll("[-_ \.\\]+", "-");
$cleanpath = $cleanpath.toLowerCase();
if ($extIndex > 0) {
//$orig_ext = $cleanpath.substring($tools.math.add($extIndex,1),$cleanpath.length() );
//$ext = orig_ext
$ext = $sys.item.getProperty('img1_ext').String; 
$cleanpath=$cleanpath.toString().substring(0,$extIndex);
$cleanpath=$cleanpath+"."+$ext;
}
$cleanpath;