Link to home
Start Free TrialLog in
Avatar of ST3VO
ST3VOFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Rename All Files in a Folder

Hi all,

I've got some files in a folder under the following name format:

AFileName.jpg.off

I need to take out all the ".off" from all the files that have the ".off" at the end. from a folder.

Can someone please help?

Thanks again

ST3VO


Avatar of TName
TName

Hi, have a look at this. It will scan subfolders also. Place a button on a form:




unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, StrUtils;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


procedure ScanDir(path: String;str:String);
var sr: TSearchRec;
NewName,s:String;
idx,len:Integer;
begin
  if path[Length(path)] <> '\' then
    path := path + '\';
  if FindFirst(path + '*.*', faAnyFile, sr) = 0 then begin
    repeat
      len:= Length(sr.Name);
      idx:=Pos(str, sr.Name);
      if (idx <> 0) and ((len-idx)=3) then begin
        NewName:=sr.Name;
        Delete(NewName, idx, len-idx+1);
        RenameFile(path+sr.Name, path+NewName);
      end;
    until FindNext(sr) <> 0;
    FindClose(sr);
  end;
  if FindFirst(path + '*.*', faAnyFile, sr) = 0 then begin
    repeat
      if (sr.Name <> '.') and (sr.name <> '..') then
        if sr.Attr = faDirectory then
          ScanDir(path + sr.Name + '\', str);
    until FindNext(sr) <> 0;
    FindClose(sr);
  end;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
  ScanDir('C:\Test','.off');
  ShowMessage('Done!');
end;

end.
ASKER CERTIFIED SOLUTION
Avatar of TName
TName

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ST3VO

ASKER

Perfect ...Thanks :o)