Autor Beitrag
Coder
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1383
Erhaltene Danke: 1

WinXP
D2005 PE
BeitragVerfasst: Do 07.02.08 07:11 
Folgendes hab ich vor:
Man kann eine Datei1 auswählen.
Dann gibt man den Pfad zu Datei2 an. Der kann relativ zu Datei1 sein, muss aber nicht.
Bis hierhin kein Problem.
Nun will ich überprüfen ob Datei2 existiert mit FileExists.
Ich denke am einfachsten wirds sein das Arbeitsverzeichnis auf Datei1 zu setzen.
Wie geht das?

MfG
Xong
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 113

WIN 2000
Borland Developer Studio 2006
BeitragVerfasst: Do 07.02.08 08:19 
function SetCurrentDir(const Dir: string): Boolean;

Hast du schon versucht FileExists den kompletten Pfad mit Dateinamen zu übergeben? (Ich weiß nicht, ob das funktioniert!)
Coder Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1383
Erhaltene Danke: 1

WinXP
D2005 PE
BeitragVerfasst: Do 07.02.08 22:13 
Variante #1 ohne SetCurrentDir

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
procedure TForm1.Button1Click(Sender: TObject);
resourcestring
  Datei1 = 'C:\abc\123.txt';
  Datei2 = '..\456.txt';
begin
  if FileExists(ExtractFilePath(Datei1)+Datei2) then
    beep; //beep!
end;


ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
procedure TForm1.Button1Click(Sender: TObject);
resourcestring
  Datei1 = 'C:\abc\123.txt';
  Datei2 = 'C:\456.txt';
begin
  if FileExists(ExtractFilePath(Datei1)+Datei2) then
    beep; //kein beep
end;


Variante #2 mit SetCurrentDir

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
procedure TForm1.Button1Click(Sender: TObject);
resourcestring
  Datei1 = 'C:\abc\123.txt';
  Datei2 = '..\456.txt';
begin
  SetCurrentDir(ExtractFilePath(Datei1));
  if FileExists(Datei2) then
    beep; //beep!
end;


ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
procedure TForm1.Button1Click(Sender: TObject);
resourcestring
  Datei1 = 'C:\abc\123.txt';
  Datei2 = 'C:\456.txt';
begin
  SetCurrentDir(ExtractFilePath(Datei1));
  if FileExists(Datei2) then
    beep; //beep!
end;
Yogu
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2598
Erhaltene Danke: 156

Ubuntu 13.04, Win 7
C# (VS 2013)
BeitragVerfasst: Do 07.02.08 23:45 
@Variante #1

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
procedure TForm1.Button1Click(Sender: TObject);
resourcestring
  Datei1 = 'C:\abc\123.txt';
  Datei2 = 'C:\456.txt';
begin
  if FileExists(ExtractFilePath(Datei1)+Datei2) then
    beep; //kein beep
end;

DA kommt dann wohl 'C:\abc\C:\456.txt' raus. Wie soll das denn klappen?
Coder Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1383
Erhaltene Danke: 1

WinXP
D2005 PE
BeitragVerfasst: Fr 08.02.08 00:35 
user profile iconYogu hat folgendes geschrieben:
@Variante #1

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
procedure TForm1.Button1Click(Sender: TObject);
resourcestring
  Datei1 = 'C:\abc\123.txt';
  Datei2 = 'C:\456.txt';
begin
  if FileExists(ExtractFilePath(Datei1)+Datei2) then
    beep; //kein beep
end;

DA kommt dann wohl 'C:\abc\C:\456.txt' raus. Wie soll das denn klappen?

Eben. Drum nehm ich auch Variante 2 weil die bei relativen und absoluten Pfaden klappt.
Yogu
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2598
Erhaltene Danke: 156

Ubuntu 13.04, Win 7
C# (VS 2013)
BeitragVerfasst: Fr 08.02.08 11:04 
Ich bin aus irgendeinem Grund gegen relative Pfadangaben. Du könntest doch erst überprüfen, ob es sich um einen relativen Pfad handelt, und dann entsprechend reagieren.

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
procedure TForm1.Button1Click(Sender: TObject);
resourcestring
  Datei1 = 'C:\abc\123.txt';
  Datei2 = 'C:\456.txt';
begin
  if ExtractFileDrive(Datei2) then
  begin
    if FileExists(Datei2) then beep;
  end
  else
  begin
    if FileExists(ExtractFilePath(Datei1)+Datei2) then beep;
  end;
end;