The ChangeFileExt function changes the extension on the given file path to that specified.

  • The Filename parameter can be either a file name, or a complete file path.

  • The Extension parameter should be given with the full-stop.

Declaration: Function ChangeFileExt( const FileName, Extension : string) : string

A basic example.

procedure OnMapEvent(var Value:Variant);
begin
Value := ChangeFileExt(Source.FileCon.CurrentFilename, '.dat');
end;

Some additional examples follow.

procedure ScriptEvent (var Value : variant);
var
f: string;
begin
LogInfo('');
f := 'abc.exe'; LogInfo('ExtractFileExt(''' + f + ''') -> ''' + ChangeFileExt(f, '.txt') + '''');
f := 'C:\MyData\MyFiles\abc.exe'; LogInfo('ExtractFileExt(''' + f + ''') -> ''' + ChangeFileExt(f, '.jpg') + '''');
f := 'abc.ref1.ref2.txt'; LogInfo('ExtractFileExt(''' + f + ''') -> ''' + ChangeFileExt(f, '.EXE') + '''');
LogInfo('');
end;

The following portion of the log shows how the extension has been changed by the above code.

image-20240107-224232.png