mirror of
https://github.com/doublecmd/doublecmd.git
synced 2026-06-21 09:58:13 +00:00
UPD: Execute multi-rename operation as one file source operation
This commit is contained in:
parent
12afc0bb73
commit
5471fc005f
6 changed files with 83 additions and 45 deletions
|
|
@ -1398,6 +1398,7 @@ end;"/>
|
|||
<Unit188>
|
||||
<Filename Value="filesources\wfxplugin\uwfxpluginsetfilepropertyoperation.pas"/>
|
||||
<IsPartOfProject Value="True"/>
|
||||
<UnitName Value="uWfxPluginSetFilePropertyOperation"/>
|
||||
</Unit188>
|
||||
<Unit189>
|
||||
<Filename Value="filesources\wfxplugin\uwfxpluginutil.pas"/>
|
||||
|
|
|
|||
|
|
@ -135,7 +135,7 @@ begin
|
|||
else
|
||||
aTemplateFile := nil;
|
||||
|
||||
SetProperties(aFile, aTemplateFile);
|
||||
SetProperties(CurrentFileIndex, aFile, aTemplateFile);
|
||||
|
||||
with FStatistics do
|
||||
begin
|
||||
|
|
|
|||
|
|
@ -115,7 +115,7 @@ begin
|
|||
else
|
||||
aTemplateFile := nil;
|
||||
|
||||
SetProperties(aFile, aTemplateFile);
|
||||
SetProperties(CurrentFileIndex, aFile, aTemplateFile);
|
||||
|
||||
with FStatistics do
|
||||
begin
|
||||
|
|
|
|||
|
|
@ -17,6 +17,9 @@ type
|
|||
|
||||
TSetFilePropertyResult = (sfprSuccess, sfprError, sfprSkipped);
|
||||
|
||||
TSetFilePropertyResultFunction = procedure(Index: Integer; aFile: TFile;
|
||||
aTemplate: TFileProperty; Result: TSetFilePropertyResult) of object;
|
||||
|
||||
TFileSourceSetFilePropertyOperationStatistics = record
|
||||
CurrentFile: String;
|
||||
TotalFiles: Int64;
|
||||
|
|
@ -77,6 +80,7 @@ type
|
|||
|
||||
protected
|
||||
FSupportedProperties: TFilePropertiesTypes;
|
||||
FSetFilePropertyResultFunction: TSetFilePropertyResultFunction;
|
||||
|
||||
function GetID: TFileSourceOperationType; override;
|
||||
procedure DoReloadFileSources; override;
|
||||
|
|
@ -84,7 +88,7 @@ type
|
|||
procedure UpdateStatistics(var NewStatistics: TFileSourceSetFilePropertyOperationStatistics);
|
||||
procedure UpdateStatisticsAtStartTime; override;
|
||||
|
||||
procedure SetProperties(aFile: TFile; aTemplateFile: TFile);
|
||||
procedure SetProperties(Index: Integer; aFile: TFile; aTemplateFile: TFile);
|
||||
function SetNewProperty(aFile: TFile; aTemplateProperty: TFileProperty): TSetFilePropertyResult; virtual abstract;
|
||||
|
||||
function GetErrorString(aFile: TFile; aProperty: TFileProperty): String;
|
||||
|
|
@ -122,6 +126,7 @@ type
|
|||
property Recursive: Boolean read FRecursive write FRecursive;
|
||||
property SupportedProperties: TFilePropertiesTypes read FSupportedProperties;
|
||||
property SkipErrors: Boolean read FSkipErrors write FSkipErrors;
|
||||
property OnSetFilePropertyResult: TSetFilePropertyResultFunction write FSetFilePropertyResultFunction;
|
||||
end;
|
||||
|
||||
implementation
|
||||
|
|
@ -227,6 +232,7 @@ begin
|
|||
|
||||
FStatistics := NewStatistics;
|
||||
|
||||
AppProcessMessages();
|
||||
finally
|
||||
FStatisticsLock.Release;
|
||||
end;
|
||||
|
|
@ -263,8 +269,8 @@ begin
|
|||
theTemplateFiles := nil;
|
||||
end;
|
||||
|
||||
procedure TFileSourceSetFilePropertyOperation.SetProperties(aFile: TFile;
|
||||
aTemplateFile: TFile);
|
||||
procedure TFileSourceSetFilePropertyOperation.SetProperties(Index: Integer;
|
||||
aFile: TFile; aTemplateFile: TFile);
|
||||
var
|
||||
FileAttrs: TFileAttrs;
|
||||
prop: TFilePropertyType;
|
||||
|
|
@ -306,6 +312,10 @@ begin
|
|||
end;
|
||||
|
||||
SetResult := SetNewProperty(aFile, templateProperty);
|
||||
|
||||
if Assigned(FSetFilePropertyResultFunction) then begin
|
||||
FSetFilePropertyResultFunction(Index, aFile, templateProperty, SetResult);
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
|
||||
|
|
|
|||
|
|
@ -117,7 +117,7 @@ begin
|
|||
else
|
||||
aTemplateFile := nil;
|
||||
|
||||
SetProperties(aFile, aTemplateFile);
|
||||
SetProperties(CurrentFileIndex, aFile, aTemplateFile);
|
||||
|
||||
with FStatistics do
|
||||
begin
|
||||
|
|
@ -125,8 +125,6 @@ begin
|
|||
UpdateStatistics(FStatistics);
|
||||
end;
|
||||
|
||||
AppProcessMessages;
|
||||
|
||||
CheckOperationState;
|
||||
end;
|
||||
end;
|
||||
|
|
|
|||
|
|
@ -20,7 +20,8 @@ interface
|
|||
uses
|
||||
LazUtf8, SysUtils, Classes, Graphics, Forms, StdCtrls, Menus, SynRegExpr,
|
||||
Controls, LCLType, DCClassesUtf8, uClassesEx, uFile, uFileSource,
|
||||
StringHashList, Grids, ExtCtrls, Buttons, DCXmlConfig, uOSForms;
|
||||
StringHashList, Grids, ExtCtrls, Buttons, DCXmlConfig, uOSForms,
|
||||
uFileProperty, uFileSourceSetFilePropertyOperation;
|
||||
|
||||
type
|
||||
|
||||
|
|
@ -177,6 +178,7 @@ type
|
|||
FSourceRow: Integer;
|
||||
FMoveRow : Boolean;
|
||||
FNames: TStringList;
|
||||
FLog: TStringListEx;
|
||||
|
||||
{Handles a single formatting string}
|
||||
function sHandleFormatString(const sFormatStr: string; ItemNr: Integer): string;
|
||||
|
|
@ -219,6 +221,7 @@ type
|
|||
procedure ClearPresetsList;
|
||||
{Load names from file}
|
||||
procedure LoadNamesFromFile(const AFileName: String);
|
||||
procedure SetFilePropertyResult(Index: Integer; aFile: TFile; aTemplate: TFileProperty; Result: TSetFilePropertyResult);
|
||||
public
|
||||
{ Public declarations }
|
||||
constructor Create(TheOwner: TComponent; aFileSource: IFileSource; var aFiles: TFiles); reintroduce;
|
||||
|
|
@ -239,8 +242,9 @@ implementation
|
|||
|
||||
uses
|
||||
uDebug, uLng, uGlobs, uFileProcs, DCOSUtils, DCStrUtils,
|
||||
fSelectTextRange, uShowMsg, uFileSourceUtil, uFileProperty, uFileFunctions,
|
||||
dmCommonData, fMultiRenameWait, uOSUtils;
|
||||
fSelectTextRange, uShowMsg, uFileSourceUtil, uFileFunctions,
|
||||
dmCommonData, fMultiRenameWait, uOSUtils, uFileSourceOperation,
|
||||
uOperationsManager;
|
||||
|
||||
const
|
||||
sPresetsSection = 'MultiRenamePresets';
|
||||
|
|
@ -956,50 +960,75 @@ begin
|
|||
Close;
|
||||
end;
|
||||
|
||||
procedure TfrmMultiRename.SetFilePropertyResult(Index: Integer; aFile: TFile;
|
||||
aTemplate: TFileProperty; Result: TSetFilePropertyResult);
|
||||
var
|
||||
S: String;
|
||||
begin
|
||||
with TFileNameProperty(aTemplate) do
|
||||
begin
|
||||
case Result of
|
||||
sfprSuccess:
|
||||
begin
|
||||
S:= 'OK ' + aFile.Name + ' -> ' + Value;
|
||||
FFiles[Index].Name := Value; // Write new name to the file object
|
||||
end;
|
||||
sfprError: S:= 'FAILED ' + aFile.Name + ' -> ' + Value;
|
||||
sfprSkipped: S:= 'SKIPPED ' + aFile.Name + ' -> ' + Value;
|
||||
end;
|
||||
end;
|
||||
if cbLog.Checked then FLog.Add(S);
|
||||
end;
|
||||
|
||||
procedure TfrmMultiRename.RenameFiles;
|
||||
var
|
||||
hFile: THandle;
|
||||
c: Integer;
|
||||
sNewName,
|
||||
sResult: String;
|
||||
I: Integer;
|
||||
AFile: TFile;
|
||||
OldFiles, NewFiles: TFiles;
|
||||
Operation: TFileSourceOperation;
|
||||
theNewProperties: TFileProperties;
|
||||
begin
|
||||
if cbLog.Checked then
|
||||
begin
|
||||
if edFile.Text = EmptyStr then
|
||||
edFile.Text:= FFiles[0].Path + 'default.log';
|
||||
mbForceDirectory(ExtractFileDir(edFile.Text));
|
||||
FLog:= TStringListEx.Create;
|
||||
end;
|
||||
|
||||
OldFiles:= FFiles.Clone;
|
||||
NewFiles:= TFiles.Create(EmptyStr);
|
||||
|
||||
try
|
||||
if cbLog.Checked then
|
||||
for I:= 0 to FFiles.Count - 1 do
|
||||
begin
|
||||
if edFile.Text = EmptyStr then
|
||||
edFile.Text:= FFiles[0].Path + 'default.log';
|
||||
mbForceDirectory(ExtractFileDir(edFile.Text));
|
||||
|
||||
if mbFileExists(edFile.Text) then
|
||||
begin
|
||||
hFile:= mbFileOpen(edFile.Text, fmOpenReadWrite);
|
||||
FileTruncate(hFile, 0);
|
||||
end
|
||||
else
|
||||
begin
|
||||
hFile:= mbFileCreate(edFile.Text);
|
||||
end;
|
||||
AFile:= TFile.Create(EmptyStr);
|
||||
AFile.Name:= FreshText(I);
|
||||
NewFiles.Add(AFile);
|
||||
end;
|
||||
for c:= 0 to FFiles.Count - 1 do
|
||||
FillChar({%H-}theNewProperties, SizeOf(TFileProperties), 0);
|
||||
Operation:= FFileSource.CreateSetFilePropertyOperation(OldFiles, theNewProperties);
|
||||
if Assigned(Operation) then
|
||||
begin
|
||||
sResult:= FFiles[c].Name;
|
||||
sNewName:= FreshText(c);
|
||||
if RenameFile(FFileSource, FFiles[c], sNewName, True) = True then
|
||||
begin
|
||||
FFiles[c].Name := sNewName; // Write new name to the file object
|
||||
sResult := 'OK ' + sResult + ' -> ' + sNewName;
|
||||
end
|
||||
else
|
||||
begin
|
||||
sResult := 'FAILED' + sResult + ' -> ' + sNewName;
|
||||
end;
|
||||
|
||||
if cbLog.Checked then
|
||||
FileWriteLn(hFile, sResult);
|
||||
with Operation as TFileSourceSetFilePropertyOperation do
|
||||
begin
|
||||
SetTemplateFiles(NewFiles);
|
||||
OnSetFilePropertyResult:= @SetFilePropertyResult;
|
||||
end;
|
||||
OperationsManager.AddOperationModal(Operation);
|
||||
end;
|
||||
finally
|
||||
if cbLog.Checked then
|
||||
FileClose(hFile);
|
||||
begin
|
||||
try
|
||||
FLog.SaveToFile(edFile.Text);
|
||||
except
|
||||
on E: Exception do msgError(E.Message);
|
||||
end;
|
||||
FLog.Free;
|
||||
end;
|
||||
OldFiles.Free;
|
||||
NewFiles.Free;
|
||||
end;
|
||||
|
||||
StringGridTopLeftChanged(StringGrid);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue