simple file extension manipulation functions

// getFileExtension() : returns the file extension of a given filename: function getFileExtension(fileName){ var extension=''; if ((fileName !== undefined) && (fileName.length !== 0)) { var dotPos = fileName.lastIndexOf('.'); if (dotPos != -1) { extension = fileName.substring(dotPos + 1, fileName.length); } } return extension.toLowerCase(); } // changeFileExtension() : replaces the file extension of fileName with newExt function changeFileExtension(fileName, newExt){ var newFn=''; if ((fileName !== undefined) && (fileName.length !== 0)) { var dotPos = fileName.lastIndexOf('.'); if (dotPos != -1) { newFn = fileName.substring(0, dotPos) + '.' + newExt; } else { newFn = fileName + '.' + newExt; } } return newFn; }
Here are some basic functions I use to

1) get the file extension of a filename
2) change the file extension of a filename

They are reasonably robust, and tolerate silly inputs well.
However, you could always put additional error handling in them if you desire ...

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.