Autor Beitrag
Smilebey
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 59

Win XP
D7 Ent
BeitragVerfasst: So 18.09.05 18:33 
Hallo.Also würde gerne wissen wie man einen Ordner löscht der nicht leer ist.
Denn ich habe folgenedes im Delphi Help gefunden:
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
The RemoveDirectory function deletes an existing empty directory. 

BOOL RemoveDirectory(

    LPCTSTR lpPathName   // address of directory to remove  
   );  
 

Parameters

lpPathName

Points to a null-terminated string that specifies the path of the directory to be removed. The path must specify an empty directory, and the calling process must have delete access to the directory. 

 

Return Values

If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.


Wie man sieht kan man diese Finktion nutzem um leere Ordner zu löschen.
Da müsste man zu erst alle Dateien u Unterordner löschen.
Aber wie??
Danke im Vorraus!!!


Moderiert von user profile iconraziel: Topic aus Windows API verschoben am Fr 18.11.2005 um 07:24
maxk
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1696
Erhaltene Danke: 1

Win XP, Debian Lenny
Delphi 6 Personal
BeitragVerfasst: So 18.09.05 19:40 
Hallo,
ich habe das mal so gelöst:
ausblenden 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:
function RemoveDirectories(Dir:string):boolean;
// This function deletes a directory and returns the result of deletion
var SR:TSearchRec;
    Continue:boolean;
begin
 Result:=False;
 Dir:=BackSlash(Dir);
 Continue:=FindFirst(Dir+'*.*',faAnyFile,SR)=0;
 while Continue do try
  if (SR.Name='.'or (SR.Name='..'then System.Continue;
  if not DirectoryExists(Dir+SR.Name) then begin
   SetFileAttributes(PChar(Dir+SR.Name),FILE_ATTRIBUTE_NORMAL);
   DeleteFile(Dir+SR.Name);
  end else RemoveDirectories(Dir+SR.Name);
 finally
   Continue:=FindNext(SR)=0;
 end;
 FindClose(SR);

 SetFileAttributes(PChar(Dir),FILE_ATTRIBUTE_NORMAL);
 Delete(Dir,length(Dir),1);
 if length(Dir)>0 then Result:=RemoveDir(Dir);
end;


Diese Funktion sucht im Verzeichnis nach Dateien, ändert die Attribute und löscht dann die Datei. Wird ein anderes Verzeichnis gefunden, so wird für dieses die Funktion ebenfalls aufgerufen (Rekursion) und anschließend gelöscht.


Gruß,
maxk

_________________
Ein Computer wird das tun, was Du programmierst - nicht das, was Du willst.
Smilebey Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 59

Win XP
D7 Ent
BeitragVerfasst: Mi 21.09.05 15:50 
Rekursion ist hier die Lösung nur funzt bei mir diese function nicht.Löscht nur die Dateien im Ordner, aber nicht die Unterordner und die Dateien der Unterordner.Liegt es daran das ich die BackSlash function nicht mitgeschrieben habe(weil ich nicht weiß was ich in uses für diese func schreiben muss)?Ich glaube aber dass das das Problem nicht ist.
Was mach ich???
maxk
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1696
Erhaltene Danke: 1

Win XP, Debian Lenny
Delphi 6 Personal
BeitragVerfasst: Mi 21.09.05 16:51 
Es gibt Fehler, die müssen einem so richtig weh tun. Dieser tut mir weh, da ich zugeben muss die Funktion auf einem alten Programm kopiert zu haben. Die Funktion Backslash habe ich aber vergessen. Diese macht eigentlich nichts anders, als an den Pathnamen einen Backslash anzuhängen:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
function Backslash(const Dir:string):string;
begin
 if copy(Dir, length(Dir), 1) <> '\' then Result:=Dir+'\' else Result:=Dir;
end;


Ohne den Aufruf wird zwischen Verzeichnisname und Dateiname nichts eingefügt. Aus Test\Datei.txt wird TestDatei.txt, was (natürlich) nicht gelöcht werden kann.

Gruß,
maxk

_________________
Ein Computer wird das tun, was Du programmierst - nicht das, was Du willst.
flieger-michl
Hält's aus hier
Beiträge: 14

W2K, Linux
D5, FPC
BeitragVerfasst: Do 22.09.05 15:49 
Hallo,

Eine Alternative zum rekursiven löschen wäre die Funktion Unit ShellAPI, Das hätte den Vorteil, daß der gelöschte Goulasch wahlweise sogar in den Papierkorb wandert.

ausblenden 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:
Program KillDir;
uses Dialogs, ShellAPI;

 procedure DoRemoveDir(Dir : string);
 var sh : TSHFileOpStruct;
 begin
  if Dir <> '' then if Dir[Length(Dir)] = '\' then system.delete(Dir, Length(Dir), 1);
  sh.Wnd := 0;
  sh.wFunc  := FO_DELETE;
  sh.pFrom  := PChar(Dir + #0#0);
  sh.pTo    := #0#0;
  sh.fFlags := FOF_NOERRORUI or FOF_NOCONFIRMMKDIR or FOF_SILENT or FOF_NOCONFIRMATION;
  sh.hNameMappings := NIL;
  sh.lpszProgressTitle := NIL;
  SHFileOperation(sh);
 end;

var Dir : String;

begin
 Dir := 'C:\Windows';
 if InputQuery('Welches Verzeichnis darf ich denn löschen?''Zu löschendes Verzeichnis', Dir) then
  DoRemoveDir(Dir);
end.


Moderiert von user profile iconraziel: Delphi-Tags hinzugefügt.
Miri
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 657


Delphi 3 Prof., Delphi 2005 PE
BeitragVerfasst: Mo 14.11.05 10:52 
Hallo,

hab das mit der Funktion von maxk versucht, das Löschen funktioniert auch.
Jetzt verwende ich aber in der gleichen Prozedur, auch ForceDirectories um den Ordner neu zu erstellen. Noch etwas später soll eine Datei in diesen neuerstellten Ordner kopiert werden. Und genau das funktioniert nicht...

Jemand ne Idee??

Gruß,
Miri
jojo-sp
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 317

Windows XP Prof, Vista Ultimate & Home Premium, Windows 7
Delphi 7 Enterprise, Delphi 2009
BeitragVerfasst: Mo 14.11.05 11:05 
Funktioniert das anlegen der neuen Datei in dem besagten Ordner manuell?

_________________
Ist der Ruf erst ruiniert, lebts sich gänzlich ungeniert...
Wilhelm Busch (1832 - 1908)
Miri
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 657


Delphi 3 Prof., Delphi 2005 PE
BeitragVerfasst: Mo 14.11.05 11:10 
ja
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Mo 14.11.05 11:26 
Gib bitte mal kurz einen Auszug, wie Du die Datei anlegst (+evtl. Source wo der Ordner angelegt wird).

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
Miri
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 657


Delphi 3 Prof., Delphi 2005 PE
BeitragVerfasst: Mo 14.11.05 11:32 
Ich lege die Datei über GetConsoleOutput an. Ich habe ein Prog namens converter.exe, um ne Zeichensatzkonvertierung von iso nach 850 durchzuführen. Die konvertierte Datei wird dann abgespeichert.
Mir ist gerade aufgefallen, dass es funktioniert, wenn ich den Pfad zum converter nicht in " " setze. Die brauche ich aber, wenn in dem Pfad Leerzeichen vorkommen. :(
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Mo 14.11.05 11:34 
Konvertier die Pfad-Angabe zum Konverter in Kurze Dateinamen. Dann sollte das eigentlich auch funzen.

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
Miri
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 657


Delphi 3 Prof., Delphi 2005 PE
BeitragVerfasst: Mo 14.11.05 11:36 
Das Problem ist, dass das Programm auch auf anderen Rechnern installiert werden soll. Und installieren kann man grundsätzlich überall hin. Klar kann ich standardmäßig kurze Pfade ohne Leerzeichen verwenden, aber letztendlich ist's jedem freigestellt, wohin er's installiert, und so solls auch bleiben...