I thought that I had completed my program until I tried to use the RenameFile function and it failed. I thought that I might have made the jpg files read-only, but they are not. I thought that maybe the file I was trying to rename was still open, but as far as I can tell, using LoadfromFie is a single operation.
My form has a File List Box and a List Box. I load the file names from my chosen directory into the former and then take each file name in turn, find its original date from the Exif data and create a new name. finally I rename the original file. The List Box contains the original file name and new file name for each file.
My code is below:-
procedure TMainForm.BtnLoadClick(Sender: TObject);
var
i, N: Integer;
lTag: TTag;
begin
ListBox.Clear;
For N := 0 to FileCount - 1 Do
begin
OldName := FileListBox.Items[N];
if FImgInfo = nil then FImgInfo := TImgInfo.Create;
try
FImgInfo.LoadFromFile(ExpandFileName(OldName));
if FImgInfo.ExifData <> nil then
begin
if FImgInfo.HasExif then
begin
for i:=0 to FImgInfo.ExifData.TagCount-1 do
begin
lTag := FImgInfo.ExifData.TagByIndex;
if lTag.Name = 'DateTimeOriginal' then
begin
NewName := lTag.AsString + EdtSuffx.Text + '.jpg';
ListBox.AddItem(OldName + ': ' + NewName, Nil);
Continue;
end;
end;
end;
end;
except
EdtDatTim.Text := 'ERROR';
end;
if not RenameFile(OldName, NewName) then
MessageDlgPos('Unable to rename file'+OLDNAME,mtWarning,[mbOK],0,100,200);
end;
FileListBox.UpdateFileList;
end;
Is there a mistake?
Thanks.