#1 Yesterday 15:41

blanc
Member
Registered: 2026-04-17
Posts: 1

Rename a file after another file

Hi,

In a folder there are one src and one txt files. How can I rename the txt file so that it copies src file name?

Before renaming: today.src and tomorrow.txt
After renaming: today.src and today.txt

Offline

#2 Yesterday 16:24

den4b
Administrator
From: den4b.com
Registered: 2006-04-06
Posts: 3,533

Re: Rename a file after another file

You'll need to use the Pascal Script rule for this.

This script will find the first file matching the specified mask (MASTER_FILE_MASK) located in the same directory, and will use its base name as the new name for the file in the renaming list.

const
  MASTER_FILE_MASK = '*.src';

function FindFirstFile(const Folder, FileMask: WideString): WideString;
var
  Files: TWideStringArray;
begin
  Result := '';
  SetLength(Files, 0);
  WideScanDirForFiles(Folder, Files, False, False, False, FileMask);
  if Length(Files) > 0 then
    Result := Files[0];
end;

var
  MasterFile: WideString;

begin
  MasterFile := FindFirstFile(WideExtractFileDir(FilePath), MASTER_FILE_MASK);
  if Length(MasterFile) > 0 then
    FileName := WideExtractBaseName(MasterFile) + WideExtractFileExt(FilePath);
end.

P.S. This script now has a permanent place in the Pascal Script Library.

Offline

Board footer

Powered by FluxBB