[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 173
  • Last Modified:

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


0
ST3VO
Asked:
ST3VO
  • 2
1 Solution
 
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
 
TNameCommented:
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
 
ST3VOAuthor Commented:
Perfect ...Thanks :o)

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now