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


ST3VOAsked:
Who is Participating?
 
TNameConnect With a Mentor Commented:
Two small changes in the procedure ScanDir():  deleted a variable s that wasn't needed and replaced
  if (idx <> 0) and ((len-idx)=3) then begin
with
  if (idx <> 0) and ((len-idx)=Length(str)-1) then begin



procedure ScanDir(path: String;str:String);
var
sr: TSearchRec;
NewName: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)=Length(str)-1) 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;
0
 
TNameCommented:
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.
0
 
ST3VOAuthor Commented:
Perfect ...Thanks :o)

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.