Autor Beitrag
del1312
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 190



BeitragVerfasst: Fr 06.08.10 12:59 
Hallo Leute,

kann mir einer helfen und sagen wie ich aus einem Dateinamen z.B. die letzten 10 Zeichen (Dateiendung nicht eingerechnet) auslesen kann und den Dateinamen dann umbenennen ohne diesen String?
Sprich ich möchte aus z.B:

testdatei_2010_08_19.txt

die datei erstellen:

testdatei.txt

Danke für Eure Hilfe !!!!!
uko
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 220
Erhaltene Danke: 1

Win XP, VISTA, WIndows 7
Delphi 2007/2010 Prof
BeitragVerfasst: Fr 06.08.10 13:33 
Copy, Pos, ExtractFileExt, ExtractFileName sollten genügen.

Grüße
del1312 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 190



BeitragVerfasst: Fr 06.08.10 14:38 
Ok also ich bin schon nen ganzes Stück voran gekommen, danke erstmal für den Tipp. Ich stehe jetzt aber irgendwie vor dem Prob, wie speicher ich den neuen erstellen Namen in die Datei?
Kann mir da einer nochmal bitte nen kleinen Tipp geben? DANKE!

Als Beispiel habe ich Dateien im Ordner D:\test\ die ausgelesen werden. Dort liegen Filme im Format: blablabla_08-07-2010_872345987.mp4
Nur damit ihr das auch mal durchspielen könnte, falls jemand den Code probiert.



ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ShellApi;

type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    Label1: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Button3: TButton;
    Button1: TButton;
    procedure ListBox1DblClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ListBox1Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);

  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function RenameFileEx(const AOldName, ANewName: string;
   ARenameCheck: boolean = false): boolean;
var
  sh: TSHFileOpStruct;
begin
  sh.Wnd := Application.Handle;
  sh.wFunc := fo_Move;

  // String muss mit #0#0 Terminiert werden, um das Listenende zu setzen
  sh.pFrom := PChar(AOldName + #0);
  sh.pTo := PChar(ANewName + #0);
  sh.fFlags := fof_Silent or fof_MultiDestFiles;
  if ARenameCheck then
    sh.fFlags := sh.fFlags or fof_RenameOnCollision;
  Result:=ShFileOperation(sh)=0;
end;






procedure GetFilesInDirectory(Directory: stringconst Mask: string;
List: TStrings;
WithSubDirs, ClearList: Boolean);


procedure ScanDir(const Directory: string);
var
  SR: TSearchRec;
begin
  if FindFirst(Directory + Mask, faAnyFile and not faDirectory, SR) = 0 then try
    repeat
      List.Add(Directory + SR.Name)
    until FindNext(SR) <> 0;
  finally
    FindClose(SR);
  end;

  if WithSubDirs then begin
    if FindFirst(Directory + '*.*', faAnyFile, SR) = 0 then try
      repeat
        if ((SR.attr and faDirectory) = faDirectory) and
           (SR.Name <> '.'and (SR.Name <> '..'then
          ScanDir(Directory + SR.Name + '\');
      until FindNext(SR) <> 0;
    finally
      FindClose(SR);
    end;
  end;
end;

begin
  List.BeginUpdate;
  try
    if ClearList then
      List.Clear;
    if Directory = '' then Exit;
    if Directory[Length(Directory)] <> '\' then
      Directory := Directory + '\';
    ScanDir(Directory);
  finally
    List.EndUpdate;
  end;
end;







procedure TForm1.Button1Click(Sender: TObject);
begin
RenameFileEx(Edit3.text,  ??????????);
end;

procedure TForm1.Button3Click(Sender: TObject);
var
s : string;
i, i2 : integer;
Source, Target : string;
fullFileName : string;

begin
fullFileName := Edit1.Text;
//  Show the component parts of this full name
//  ShowMessage('Drive = '+ExtractFileDrive (fullFileName));
//  ShowMessage('Dir   = '+ExtractFileDir   (fullFileName));
//  ShowMessage('Path  = '+ExtractFilePath  (fullFileName));
//  ShowMessage('Name  = '+ExtractFileName  (fullFileName));
//  ShowMessage('Ext   = '+ExtractFileExt   (fullFileName));
Edit2.Text := ExtractFileName  (fullFileName);

s:=Edit2.text;
i:=Pos('-2010_',s);
i2:=Pos('.mp4',s);
Edit3.Text:=(IntToStr(i)+ ' / ' + IntToStr(i2));

i:=i-7;
Source := Edit2.Text;
Target := Copy(Source, 0, i);
Edit3.Text:=(Target + ExtractFileExt   (fullFileName)) ;


end;


procedure TForm1.FormCreate(Sender: TObject);
begin
GetFilesInDirectory('D:\test\''*.*', Listbox1.Items, True, True);
end;

procedure TForm1.ListBox1Click(Sender: TObject);
begin
Edit1.Text:=ListBox1.Items.Strings[ListBox1.ItemIndex];
end;

procedure TForm1.ListBox1DblClick(Sender: TObject);
begin
ShellExecute(Application.Handle, 'open', PChar(ListBox1.Items.Strings[ListBox1.ItemIndex]), NilNil, SW_SHOW);
end;

end.
uko
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 220
Erhaltene Danke: 1

Win XP, VISTA, WIndows 7
Delphi 2007/2010 Prof
BeitragVerfasst: Fr 06.08.10 14:42 
Wie wäre es damit: RenameFile (SysUtils)? Einfach mal die Hilfe durchsuchen, da findet man erstaunlich viel! :wink:
del1312 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 190



BeitragVerfasst: Fr 06.08.10 14:46 
ahh habs ;o) DANKE