#1 2019-09-18 20:36

Nbot
Member
Registered: 2019-09-18
Posts: 3

Renaming folders with the first and last names of contained files

I’m trying to batch rename a group of folders with the beginning and end file names of the contents within it.  For example, a set of files 0001-9999 live in folder(new folder). I want the (new folder) to be named _0001_9999_.  It would  ideal to do this for multiple folders at a time.

Offline

#2 2019-09-18 20:43

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

Re: Renaming folders with the first and last names of contained files

This can be achieved using the Pascal Script rule in ReNamer.

Can you provide a few example filenames, to help us establish a pattern for extracting the necessary numbers?

Offline

#3 2019-09-18 20:52

Nbot
Member
Registered: 2019-09-18
Posts: 3

Re: Renaming folders with the first and last names of contained files

(New Folder, New Folder 2, New Folder 3,...etc) Inside each folder will be a list of files for example, CF451161, CF451162, CF451163...etc. or files named like this _04A7657, _04A7658, _04A7659.. etc.  The last four digits of each file are usually sequential. I would like to batch name the parent folder for each list of files with the first and last file name.  So the New Folder's will be renamed like this (_CF451161_CR45163) and (_04A7657_04A7659)

Offline

#4 2019-09-18 21:29

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

Re: Renaming folders with the first and last names of contained files

Ok, lets consider the following paths:

C:\Temp\Nbot\A\CF451161.txt
C:\Temp\Nbot\A\CF451162.txt
C:\Temp\Nbot\A\CF451163.txt
C:\Temp\Nbot\B\_04A7657.txt
C:\Temp\Nbot\B\_04A7658.txt
C:\Temp\Nbot\B\_04A7659.txt

Add your folders for renaming as folders (not as files), by adjusting the filter settings.

Add a Pascal Script rule with a script included below. Basically, it finds all contained files, sorts them and uses the first and last filenames for renaming the parent folder.

The new names of folders should like the following:

C:\Temp\Nbot\A   ->   CF451161_CF451163
C:\Temp\Nbot\B   ->  _04A7657__04A7659

The script to achieve this:

{ Rename folders using MIN & MAX contained filenames }

var
  FirstFile, LastFile: WideString;
  Files: TWideStringArray;
  I: Integer;

begin
  if WideDirectoryExists(FilePath) then
  begin
    SetLength(Files, 0);
    WideScanDirForFiles(FilePath, Files, False, False, False, '*');
    if Length(Files) > 0 then
    begin
      FirstFile := Files[0];
      LastFile := FirstFile;
      for I := 1 to Length(Files) - 1 do
      begin
        if WideCompareText(FirstFile, Files[I]) > 0 then
          FirstFile := Files[I];
        if WideCompareText(LastFile, Files[I]) < 0 then
          LastFile := Files[I];
      end;
      FileName := WideExtractBaseName(FirstFile) + '_' + WideExtractBaseName(LastFile);
    end;
  end;
end.

Offline

#5 2019-09-18 21:47

Nbot
Member
Registered: 2019-09-18
Posts: 3

Re: Renaming folders with the first and last names of contained files

This is great! Thanks!

Offline

Board footer

Powered by FluxBB