Autor Beitrag
Paddymann
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 58


Delphi 5 Professional
BeitragVerfasst: Sa 29.04.06 11:01 
Hallo!
Ich möchte ein Programm schreiben, mit dem ich eine Datei suchen kann. Wenn das Programm die Datei gefunden hat soll er mir in einer Caption den Pfad ausgeben. Ich habe dies mit der Procedure FindFirst probiert, bin aber nicht sehr weit gekommen. Wo kann ich in dem Code unten den Dateinamen eingeben und das verzeichnis in dem er suchen soll? Und wie kann ich den Pfad ausgeben lassen? Mit ExcludeAttr?
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:
type

TSearchRec = record
    Time: Integer;
    Size: Integer;
    Attr: Integer;
    Name: TFileName;
    ExcludeAttr: Integer;
    FindHandle: THandle;
    FindData: TWin32FindData;
  end;
  
  TForm1 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    procedure Button1Click(Sender: TObject);

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

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
F: TSearchRec;
begin
if FindFirst(c:\beispiel.txt, faAnyFile, F) = 0 then try
    Label1.Caption := F.Name;
    Label2.Caption := IntToStr(F.Size);
    Labl3.Caption  := ExcludeAttr;
  finally
    FindClose(F);
  end;
end;

end.


Gibt es vllt auch noch eine andere Möglichkeit Dateien zu suchen?

thx und mfg
Patti

Moderiert von user profile iconChristian S.: Code- durch Delphi-Tags ersetzt
Moderiert von user profile iconChristian S.: Topic aus Delphi Language (Object-Pascal) / CLX verschoben am Sa 29.04.2006 um 12:03
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Sa 29.04.06 11:07 
Moin!

Schonmal die Suche benutzt? Suche in: Delphi-Forum, Delphi-Library FINDFIRST

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
F34r0fTh3D4rk
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 5285
Erhaltene Danke: 29

Win Vista (32), Win 7 (64)
Eclipse, SciTE, Lazarus
BeitragVerfasst: Sa 29.04.06 11:33 
TSearcRec muss doch überhaupt net deklariert werden :?
Paddymann Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 58


Delphi 5 Professional
BeitragVerfasst: Sa 29.04.06 13:31 
Bevor ich den Thread erstellt habe habe ich die Suchfunktion benutzt. Ich bin allerdings nicht sehr viel schlauer draus geworden. Kann sein dass ich mittlerweile etwas weiter gekommen bin. Guckt euch bitte nochmal den Code-Schnipsel unten an. Ich hab FindFirst in eine andere Procedure reingepackt und will auf die dann zugreifen wenn der Button gedrückt wird. Jetztz ist aber wieder das Problem das er die Datei nach der ich suchen will(beispiel.txt) als Undefinierten Bezeichner ansieht.

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:
type
  TForm1 = class(TForm)
    Label1: TLabel;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    procedure DateiSuchen(DateiName, SuchPfad: String);
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.DateiSuchen(DateiName, SuchPfad: String);
var
F: TSearchRec;
begin
if(FindFirst(SuchPfad + '*.*', faAnyFile, F)=0)
then
Label1.Caption := F.Name;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
DateiSuchen(beispiel.txt,c:\);
end;

end.


wAs ist daran noch falsch?

thx und mfg
Patti

Moderiert von user profile iconraziel: Code- durch Delphi-Tags ersetzt
Paddymann Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 58


Delphi 5 Professional
BeitragVerfasst: Sa 29.04.06 13:46 
OK ich habe den Fehler entdeckt und den Quelltext so abgeändert:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
procedure TForm1.Button1Click(Sender: TObject);
begin
DateiSuchen('beispiel.txt','c:\');
end;


Jetztz gibt der Compiler keinen Fehler mehr aus. Allerdings gibt die Caption immer den Namen des ersten Ordner in meinem C-Verzeichnis an. Dazu brauch mann doch FindNext?
Ich weiss nicht wie ich das einsetzen muss und kommen auch mit der Suchfunktioni nicht weiter.

thx und mfg
Patti

Moderiert von user profile iconraziel: Code- durch Delphi-Tags ersetzt
F34r0fTh3D4rk
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 5285
Erhaltene Danke: 29

Win Vista (32), Win 7 (64)
Eclipse, SciTE, Lazarus
BeitragVerfasst: Sa 29.04.06 14:13 
benutze bitte die delphi tags (auf das + neben delphi klicken unter bereiche)

Easy Helper hat folgendes geschrieben:
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:
procedure GetFilesInDirectory(Directory: String; const Mask: String;
                              List: TStrings;
                              WithSubDirs, ClearList: Boolean);

procedure ScanDir(const Directory: String);
var
  SR: TSearchRec;
begin
  if FindFirst(Directory + Mask, faAnyFile - 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;
Paddymann Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 58


Delphi 5 Professional
BeitragVerfasst: Sa 29.04.06 14:20 
Ja das kenne ich auch! Allerdings hab ich es nicht mit Directorys zu tun sodnernnur mit einer einzelen Datei und ich möchte auch nicht das ganze Verzeichnis in einer ListBox auflisten.
F34r0fTh3D4rk
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 5285
Erhaltene Danke: 29

Win Vista (32), Win 7 (64)
Eclipse, SciTE, Lazarus
BeitragVerfasst: Sa 29.04.06 14:21 
da steht aber wie du es machen musst, du musst solange die datei ein verzeichnis ist immer wieder findnext aufrufen, bis es keins mehr ist
Paddymann Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 58


Delphi 5 Professional
BeitragVerfasst: Sa 29.04.06 14:38 
hmmm also ich weiss auch da mit nicht wie's gehen soll...:(
so vllt

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
procedure TForm1.DateiSuchen(DateiName, SuchPfad: String);
var
F: TSearchRec;
begin
repeat
until(FindFirst(SuchPfad + '*.*', faAnyFile, F)<>0)
then
Label1.Caption := F.Name;

FindClose(F);
end;
F34r0fTh3D4rk
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 5285
Erhaltene Danke: 29

Win Vista (32), Win 7 (64)
Eclipse, SciTE, Lazarus
BeitragVerfasst: Sa 29.04.06 14:41 
vielleicht solltest du erstmal mit was leichterem anfangen.

probiers evtl mal so:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
var
  F: TSearchRec;
begin
  findfirst(SuchPfad + '*.*', faAnyFile, F);
  while ((F.attr and faDirectory) = faDirectory) and (F.Name <> '.') and (F.Name <> '..') do
    findnext(F);
end;
Paddymann Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 58


Delphi 5 Professional
BeitragVerfasst: Sa 29.04.06 14:58 
Okay das habe ich jezttz so gemacht:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
procedure TForm1.DateiSuchen(Suchpfad: String);
var
F: TSearchRec;
begin
findfirst(SuchPfad + '*.*', faAnyFile, F);
  while ((F.attr and faDirectory) = faDirectory) and (F.Name <> '.') and (F.Name <> '..') do
    findnext(F);
    Label1.Caption := F.Name;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
DateiSuchen('c:\');

end;


Aber jetztz finde ich doch damit die erste Datei die nicht in einem Ordner ist oder nicht? Und was ist F.attr , '.' und '..' ?

thx
patti
F34r0fTh3D4rk
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 5285
Erhaltene Danke: 29

Win Vista (32), Win 7 (64)
Eclipse, SciTE, Lazarus
BeitragVerfasst: Sa 29.04.06 16:20 
die erste datei die kein ordner ist
Paddymann Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 58


Delphi 5 Professional
BeitragVerfasst: Sa 29.04.06 16:28 
ja und wie find ich jetzt irgendeine beliebige datei?
F34r0fTh3D4rk
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 5285
Erhaltene Danke: 29

Win Vista (32), Win 7 (64)
Eclipse, SciTE, Lazarus
BeitragVerfasst: Sa 29.04.06 16:29 
was meinst du mit beliebig ? eine zufällige ? dann musst du das ganze zufällig oft weitermachen.
Paddymann Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 58


Delphi 5 Professional
BeitragVerfasst: Sa 29.04.06 16:40 
nein z.B. die beispiel.txt Datei, die nicht die erste Datei nach einem Ordner ist...sondern irgendwo unter c:\
F34r0fTh3D4rk
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 5285
Erhaltene Danke: 29

Win Vista (32), Win 7 (64)
Eclipse, SciTE, Lazarus
BeitragVerfasst: Sa 29.04.06 16:43 
das macht doch genau die funktion die ich dir geschrieben hab, bau die doch so um, dass die aufhört wenn die erste datei gefunden wurde (was nicht sonderlich sinnvoll ist, da die date ja mehrfach vorkommen kann)
Paddymann Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 58


Delphi 5 Professional
BeitragVerfasst: Sa 29.04.06 16:44 
das kann ich nicht :( das hab ich ja oben probiert...
F34r0fTh3D4rk
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 5285
Erhaltene Danke: 29

Win Vista (32), Win 7 (64)
Eclipse, SciTE, Lazarus
BeitragVerfasst: Sa 29.04.06 17:06 
dann solltest du erstmal was leichteres versuchen oder dich an christians anfänger tutorial ranmachen ;)
Paddymann Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 58


Delphi 5 Professional
BeitragVerfasst: So 30.04.06 13:16 
Wieso werden in der Listbox Dateien angezeigt wie \. und \.. obwohl es die Dateien eigentlich gar nicht gibt?
F34r0fTh3D4rk
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 5285
Erhaltene Danke: 29

Win Vista (32), Win 7 (64)
Eclipse, SciTE, Lazarus
BeitragVerfasst: So 30.04.06 14:39 
darum eigentlich diese zeile:
ausblenden Delphi-Quelltext
1:
 (SR.Name <> '.') and (SR.Name <> '..') then