AutoRen sert à renommer automatiquement une série de fichiers déposés par Drag n' drop.
Le tout à partir d'une GUI : on entre un nom de base, l'incrément de base et c'est partit...
Des petits détails bien utiles dans la pratique sont implémentés dans l'ensemble :
choix du format de renommage (entre format DOS 8.3 et compte de l'incrément),
correction automatique de ces paramètres,
choix de l'ordre de renommage,
interface graphique...
Je ne vous cacherai pas que le code de pepitto (35217) contribue au mien, ce qui est annoté.
Attention : j'ai travaillé et compilé mon code sous Delphi 2.0, testé sur Win 2000 et XP.
Source / Exemple :
unit RenWinCode;
interface
{
AutoRen Application for automatic-rename
of Windows file from an Explorer Window.
Developped with Delphi 2.0
Description: If you need to rename multiple files from a specific
scheme AutoRen might be a usefull solution.
It takes the files from Drag 'n Drop.
Then it changes their names from a specific count, with possibility
for 8.3 DOS format restriciton, and/or bigger count scheme.
The Start-number-spin allows to assemble multiple directories by
starting from a specific count.
Version: 1.0 and the last.
Platform: Windows 95 and upper
Author: Lionel Tailhardat
E-Mail: lionel@2Ears.net
Home Page: www.2Ears.net
Created: September 06 2006
Compiled: September 25 2006
Comments: French comments, parts from
http://www.codes-sources.com/code.aspx?ID=35217.
Legal: Copyright (c) 2006, Lionel Tailhardat
NOTE:
1). It is an 'as-is' software
2). Take respect to the authors' work
}
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Buttons, ComCtrls, ExtCtrls, Spin, ShellApi;
type
TAutoRenWin = class(TForm)
StatusBar : TStatusBar;
RenameBtn : TBitBtn;
ListBox : TListBox;
StartSpinEdit : TSpinEdit;
NameEdit : TEdit;
ExtEdit : TEdit;
Label1 : TLabel;
Label2 : TLabel;
Label3 : TLabel;
NumberLabel : TLabel;
Bevel1 : TBevel;
ExampleLabel : TLabel;
ExtCheckBox : TCheckBox;
Bevel2 : TBevel;
Bevel3 : TBevel;
ClearBtn : TBitBtn;
ProgressBar : TProgressBar;
UTFCheckBox : TCheckBox;
UpBtn : TBitBtn;
DownBtn : TBitBtn;
AlphabetBtn : TBitBtn;
FileLabel : TLabel;
Label4 : TLabel;
NumberSpinEdit : TSpinEdit;
HelpLabel : TLabel;
procedure FormCreate(Sender: TObject);
procedure AppMessage(var Msg: TMsg; var Handled: Boolean);
procedure ClearBtnClick(Sender: TObject);
procedure ExtCheckBoxClick(Sender: TObject);
procedure ListBoxClick(Sender: TObject);
procedure RenameBtnClick(Sender: TObject);
procedure AlphabetBtnClick(Sender: TObject);
procedure DownBtnClick(Sender: TObject);
procedure UpBtnClick(Sender: TObject);
procedure StartSpinEditChange(Sender: TObject);
procedure NumberSpinEditChange(Sender: TObject);
procedure NameEditChange(Sender: TObject);
procedure UTFCheckBoxClick(Sender: TObject);
procedure ExtEditChange(Sender: TObject);
private
{ Déclarations privées }
public
{ Déclarations publiques }
end;
var
AutoRenWin: TAutoRenWin;
implementation
{$R *.DFM}
// =============================================================================
procedure TAutoRenWin.FormCreate(Sender: TObject);
begin
// Préparation de la Form pour le Drag'n Drop
DragAcceptFiles(ListBox.handle,true);
Application.OnMessage := AppMessage;
end;
procedure TAutoRenWin.AppMessage(var Msg: TMsg; var Handled: Boolean);
var Index,
FileCount,
FileSize : integer;
FileName : array[0..255] of char;
begin
// Gestion des messages Windows
if Msg.message=WM_DROPFILES then
begin
FileCount := DragQueryFile(Msg.wParam, $FFFFFFFF, FileName, SizeOf(FileName));
// Ajout des fichiers à ListBox
for Index := 0 to FileCount - 1 do begin
FileSize := DragQueryFile(Msg.wParam, Index, FileName, SizeOf(FileName));
ListBox.Items.Add(FileName);
end;
NumberLabel.Caption := 'Fichiers sélectionnés : ' + IntToStr(ListBox.Items.Count);
// Sélection automatique du premier élément
ListBox.ItemIndex := 0;
AutoRenWin.ListBoxClick(self);
end;
end;
procedure TAutoRenWin.ListBoxClick(Sender: TObject);
var FileName,
RealFormat,
Format,
DOSName,
ExAp : String;
Index,
Position : Integer;
begin
If ListBox.ItemIndex = -1 then Exit;
// Affichage de la sélection en cours
FileName := ListBox.Items[ListBox.ItemIndex];
FileLabel.Caption := IntToStr(ListBox.ItemIndex + 1) + ' - .\' + ExtractFileName(FileName);
// Affichage d'un résultat ...
// Correction automatique du format prévu selon le nb de fichiers total
Index := ListBox.Items.Count + StartSpinEdit.Value;
RealFormat := IntToStr(Index);
if length(RealFormat) > NumberSpinEdit.Value then NumberSpinEdit.Value := Length(RealFormat);
// Préparation du format
Format := '';
for Index := 1 to NumberSpinEdit.Value do Format := Format + '0';
// Récupération du numéro initial
Index := StartSpinEdit.Value;
// Création du nouveau nom du fichier
Position := ListBox.ItemIndex;
If UTFCheckBox.checked = True then DOSName := Copy(NameEdit.Text, 0, 8 - length(Format))
else DOSName := NameEdit.Text;
ExAp := FormatFloat(DOSName + Format, Index + Position);
// Selon l'option modifier l'Extension,
if ExtCheckBox.Checked = True then ExAp := ExAp + '.' + ExtEdit.Text
// extraction de l'extention du fichier
else ExAp := ExAp + ExtractFileExt(ListBox.Items.Strings[Position]);
// Affichage de l'exemple
ExampleLabel.Caption := ExAp;
end;
procedure TAutoRenWin.RenameBtnClick(Sender: TObject);
Var RealFormat,
Format,
DOSName,
Ap : string;
Index : integer;
begin // Partie reprise et améliorée de 35217
// Correction automatique du format prévu selon le nb de fichiers total
Index := ListBox.Items.Count + StartSpinEdit.Value;
RealFormat := IntToStr(Index);
if length(RealFormat) > NumberSpinEdit.Value then NumberSpinEdit.Value := Length(RealFormat);
// Préparation du format
Format := '';
for Index := 1 to NumberSpinEdit.Value do Format := Format + '0';
// Récupération du numéro initial
Index := StartSpinEdit.Value;
// Réglage de la ProgressBar
ProgressBar.Max := ListBox.Items.Count;
// Vérification puis démarrage du renommage
if ListBox.Items.Count > 0 then
begin
repeat
// Extraction du répertoire dans lequel se trouve le fichier
Ap := extractfilePath(Listbox.Items.Strings[0]);
// Création du nouveau nom du fichier
If UTFCheckBox.checked = True then DOSName := Copy(NameEdit.Text, 0, 8 - length(Format))
else DOSName := NameEdit.Text;
Ap := Ap + FormatFloat(DOSName + Format, Index);
// Selon l'option modifier l'Extension,
if ExtCheckBox.Checked = True then Ap := Ap + '.' + ExtEdit.Text
// extraction de l'extention du fichier
else Ap := Ap + ExtractFileExt(ListBox.Items.Strings[0]);
// Renomage du Fichier
RenameFile(Listbox.Items.Strings[0],Ap);
// Suppression du 1er Fichier dans la listbox
NumberLabel.Caption := 'Fichiers sélectionnés : ' + IntToStr(ListBox.Items.Count);
ListBox.Items.Delete(0);
Repaint;
ProgressBar.Position := Index;
// Incrémentation du compteur
Inc(Index);
until ListBox.Items.Count = 0;
// R.A.Z.
ProgressBar.Position := 0;
FileLabel.Caption := '';
ExampleLabel.Caption := '';
end;
end;
// =============================================================================
procedure TAutoRenWin.ClearBtnClick(Sender: TObject);
begin
// R.A.Z. de la ListBox
ListBox.Items.Clear;
NumberLabel.Caption := 'Fichiers sélectionnés : 0';
FileLabel.Caption := '';
ExampleLabel.Caption := '';
end;
procedure TAutoRenWin.ExtCheckBoxClick(Sender: TObject);
begin
// Mise en service de ExtEdit
ExtEdit.Enabled := ExtCheckBox.Checked;
AutoRenWin.ListBoxClick(self);
end;
procedure TAutoRenWin.AlphabetBtnClick(Sender: TObject);
Var St : TStringList;
begin // Partie reprise et améliorée de 35217
// TlistBox ne permet pas de faire un tri à un instant voulu, j'utilise donc un TstringList pour le faire
If ListBox.Items.Count <0 then Exit;
St := TStringList.Create;
St.Assign(ListBox.Items); // Stocke la liste des fichiers dans un TStringList
St.Sort; // Exécution du tri par ordre alphabétique
Listbox.Items.Assign(St); // restitution de la liste trié dans la listbox
St.Free;
ListBox.ItemIndex := 0;
AutoRenWin.ListBoxClick(self);
end;
procedure TAutoRenWin.DownBtnClick(Sender: TObject);
Var n : integer;
begin // Partie reprise et améliorée de 35217
If ListBox.ItemIndex = ListBox.Items.Count - 1 then Exit;
n := ListBox.ItemIndex; // enregistre la position du fichier selectionné dans la liste
ListBox.Items.Move(n, n + 1); // déplace le fichier d'un cran vers le bas dans la liste
ListBox.ItemIndex := n + 1; // permet de garder le fichier en cours sélectionné
AutoRenWin.ListBoxClick(self);
end;
procedure TAutoRenWin.UpBtnClick(Sender: TObject);
Var n : integer;
begin // Partie reprise et améliorée de 35217
If ListBox.ItemIndex < 1 then Exit;
n := ListBox.ItemIndex; // enregistre la position du fichier selectionné dans la liste
ListBox.Items.Move(n, n - 1); // déplace le fichier d'un cran vers le haut dans la liste
ListBox.ItemIndex := n - 1; // permet de garder le fichier en cours sélectionné
AutoRenWin.ListBoxClick(self);
end;
procedure TAutoRenWin.StartSpinEditChange(Sender: TObject);
begin
// Mise à jour des afficheurs
AutoRenWin.ListBoxClick(self);
end;
procedure TAutoRenWin.NumberSpinEditChange(Sender: TObject);
begin
// Mise à jour des afficheurs
AutoRenWin.ListBoxClick(self);
end;
procedure TAutoRenWin.NameEditChange(Sender: TObject);
begin
// Mise à jour des afficheurs
AutoRenWin.ListBoxClick(self);
end;
procedure TAutoRenWin.UTFCheckBoxClick(Sender: TObject);
begin
// Mise à jour des afficheurs
AutoRenWin.ListBoxClick(self);
end;
procedure TAutoRenWin.ExtEditChange(Sender: TObject);
begin
// Mise à jour des afficheurs
AutoRenWin.ListBoxClick(self);
end;
end. // = Fin AutoRen ==========================================================
Conclusion :
Vis à vis du 35217, il n'y a pas la fonction de rajouter des fichiers par la ligne de commande.
Mais dans l'ensemble, mis-à-part l'interface, ce qui m'a paru le plus important d'intégrer était le point de démarrage de l'incrémentation, ce afin de pouvoir, par ex., rassembler plusieurs répertoires.
Je ne ferai pas de mises-à-jour ultérieures.
A+
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.