function file_name_new = file_name_ext_swap ( file_name, ext ) %*****************************************************************************80 % %% FILE_NAME_EXT_SWAP replaces the current "extension" of a file name. % % Discussion: % % The "extension" of a filename is the string of characters % that appears after the LAST period in the name. A file % with no period, or with a period as the last character % in the name, has a "null" extension. % % Example: % % Input Output % ================ ============= % FILE_NAME EXT FILE_NAME_NEW % % bob.for obj bob.obj % bob.bob.bob txt bob.bob.txt % bob yak bob.yak % % Licensing: % % This code is distributed under the GNU LGPL license. % % Modified: % % 15 August 2005 % % Author: % % John Burkardt % % Parameters: % % Input, character FILE_NAME(*), a file name. % On output, the extension of the file has been changed. % % Input, character EXT(*), the extension to be used on the output % copy of FILE_NAME, replacing the current extension if any. % % Output, character FILE_NAME_NEW(*), a copy of the input file name, % with the new extension. % file_name_len = length ( file_name ); ext_len = length ( ext ); period = file_name_len + 1; for i = file_name_len : -1 : 1 if ( file_name(i:i) == '.' ) period = i; break end end file_name_new(1:period-1) = file_name(1:period-1); file_name_new(period) = '.'; file_name_new(period+1:period+ext_len) = ext(1:ext_len); return end