rename all files in FileListBox with a header string

How to rename all files in FileListBox with a header string?
fhtongAsked:
Who is Participating?
 
geobulCommented:
Or using RenameFile function:

with FileListBox1 do begin
  for i := 0 to Items.Count - 1 do begin
    if not RenameFile(Directory + '\' + Items[i], Directory + '\' + 'YourPrefix' + Items[i])  then begin
      // do something here when renaming is not possible
    end;
  end;
end;

Regards, Geo
0
 
kretzschmarCommented:
well, no delphi on hand, but i would guess

uses shellapi;

procedure TForm1.Button1Click(Sender: TObject);
var I : Integer;
    opInfo : _SHFILEOPSTRUCT;
begin
  for i := 0 to fileListBox1.Items.Count-1 do
  begin
     opInfo.pFrom := YourPath+fileListBox1.Items[i];  //Source
    opInfo.pTo   := YourPath+YourPrefix+fileListBox1.Items[i];  //Destination
    opInfo.wFunc := FO_RENAME;  //Rename it
    opInfo.fFlags := FOF_NOCONFIRMATION  or //no confirmation
                   FOF_NOCONFIRMMKDIR or   //no confirmation                  
                   FOF_RENAMEONCOLLISION;
    opInfo.hNameMappings := nil;  // no mappings
    opInfo.fAnyOperationsAborted := False; //obsolete
    SHFileOperation(opInfo);  //do it
  end;
end;

just from head and really not tested -> typos possible

meikl ;-)
0
 
fhtongAuthor Commented:
Thanks, geobul.

It works out fine.
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.