Remove Extension From File Name

Remove the extension from a file name using the ExtractFileName(), Left() and InStrRev() functions

This sample task will demonstrate how to remove the extension from a file name using the ExtractFileName(), Left() and InStrRev() functions.

- ExtractFileName() returns only file name and extension parts of a string.
- Left() returns a specified number of characters from the left side of a string.
- InStrRev() returns the position of the first occurrence of one string within another. The search begins from the end of the string.

The InStrRev() function is better than the InStr() function because the InStrRev() function returns the last occurrence whereas the InStr() function returns the first occurrence. For example:

input: this.is.a.test.txt
%left('this.is.a.test.txt', instrrev('this.is.a.test.txt', '.')-1)%
output: this.is.a.test

input: this.is.a.test.txt
%left('this.is.a.test.txt', instr('this.is.a.test.txt', '.')-1)%
output: this

As you can see from the examples above, InStrRev() is 100% accurate as far as locating the extension since it will start at the very last occurrence of the dot. If you have files with multiple dots in the file names this would be very beneficial.

Task Category:

Functions (Basic)