#1 2018-04-02 14:06

MOT
Member
Registered: 2018-04-02
Posts: 2

Rename according to a comma-separated mapping file

Hello,

I have a problem that is similar but not identical to Reading text from a file.

I have a text file which, on each line, contains the current name of a file and a text string that I would like to prepend to the filename. Example:

RB130123, CRB100999
LP120765, CLP120252

Resulting filenames should be:  CRB100999-RB130123, and CLP120252-LP120765

How would I accomplish this? I presume by using a PascalScript. Can anyone help me figure this out?

Thank you.

Edit: Originally posted in Reading text from a file, moved by moderator.

Offline

#2 2018-04-05 03:15

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

Re: Rename according to a comma-separated mapping file

The script below will accomplish what you need.

At each preview, it will ask for a full path to the file containing the comma-separated file name mappings (map file). It will the load the map file, and will find and insert an appropriate mapping into each filename. You could also hardcode the map file path inside the script, so that it wont be asked on every preview.

var
  MapFilePath: WideString;
  Initialized: Boolean;
  TargetName: String;
  SourceNames, TargetNames: TAnsiStringArray;

procedure LoadMapFile(const FileName: WideString);
var
  Lines: TAnsiStringArray;
  Line: String;
  I, DelimPos: Integer;
begin
  Lines := FileReadLines(FileName);
  SetLength(SourceNames, Length(Lines));
  SetLength(TargetNames, Length(Lines));
  for I := 0 to Length(Lines) - 1 do
  begin
    Line := Lines[I];
    DelimPos := Pos(',', Line);
    if DelimPos > 0 then
    begin
      SourceNames[I] := Trim(Copy(Line, 1, DelimPos - 1));
      TargetNames[I] := Trim(Copy(Line, DelimPos + 1, Length(Line)));
    end
    else
    begin
      SourceNames[I] := '';
      TargetNames[I] := '';
    end;
  end;
end;

function FindMapping(const SourceName: String): String;
var
  I: Integer;
begin
  Result := '';
  for I := 0 to Length(SourceNames) - 1 do
  begin
    if Length(SourceNames[I]) > 0 then
    if WideSameText(SourceName, SourceNames[I]) then
    begin
      Result := TargetNames[I];
      Exit;
    end;
  end;
end;

begin
  if not Initialized then
  begin
    Initialized := True;
    MapFilePath := WideInputBox('Map file path', 'Enter full path to the map file:', '');
    if Length(MapFilePath) > 0 then
      LoadMapFile(MapFilePath);
  end;
  if Length(MapFilePath) > 0 then
  begin
    TargetName := FindMapping(WideExtractBaseName(FileName));
    if Length(TargetName) > 0 then
      FileName := WideExtractBaseName(FileName) + '-' +
        TargetName + WideExtractFileExt(FileName);
  end;
end.

P.S. It is usually better to start a new thread instead of reviving a 10 year old thread, and then link to other relevant threads if needed.

Offline

#3 2018-04-05 03:37

MOT
Member
Registered: 2018-04-02
Posts: 2

Re: Rename according to a comma-separated mapping file

Thank you!

I suppose you're right about reviving an old thread.

I actually found a batch file example that does what I need, also. The following line, in a batch file, will read items from a text file (files.txt) with two columns separated by a tab, and rename the filename in the first column with the name in the second column:

for /F "tokens=1,2" %%a in (files.txt) do ren "%%a" "%%b"

Offline

Board footer

Powered by FluxBB