Autor Beitrag
mlspider
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 33



BeitragVerfasst: So 28.11.04 15:52 
Hallo,

ich habe mal in ein anderen Forum einen Code-Schnipsel gefunden, das eine Datei merfach löscht, so das diese nicht wieder hergestellt werden kann. Hier mal das Teil:
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:
procedure EraseFile(Filename: string); 
const 
  Buffer = 1024; 
  Counttowrite = 34; 
  FillBuffer: array[0..5] of Integer = ($00, $FF, $00, $F0, $0F, $00); //<-- Hier 
var 
  arr: array[1..Buffer] of byte; 
  f: file; 
  i, j, n: integer; 
begin 
  AssignFile(f, FileName); 
  Reset(f, 1); 
  n := FileSize(f); 
  for j := 0 to Counttowrite do 
  begin 
    for i := 1 to n div Buffer do 
    begin 
      BlockWrite(f, FillBuffer[j], Buffer); 
    end; 
  end; 
  CloseFile(f); 
  RenameFile(Filename, ExtractFilepath(Filename)+'$000000.tmp'); 
  DeleteFile(ExtractFilepath(Filename)+'$000000.tmp'); 
end;


Nun meine Frage: Was muß ich ändern, um alle Dateien in einem Ordner auf diese Art und Weise zu löschen?

Danke!
jasocul
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 6395
Erhaltene Danke: 149

Windows 7 + Windows 10
Sydney Prof + CE
BeitragVerfasst: So 28.11.04 16:16 
Mit FindFirst, FindNext und einer Schleife sollte das zu lösen sein.
mlspider Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 33



BeitragVerfasst: So 28.11.04 16:19 
Leider bin ich noch blutiger Anfänger und habe daher keinen Plan, wie ich das anstellen soll. Hätte vieleicht jemand ein kleines Code-Beispiel zur Hand, wie das mit FindFirst, FindNext in einer Schleife funktionieren soll???
herzi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 168

WINXP pro, SUSE Linux 9.0
D7 Pers.
BeitragVerfasst: So 28.11.04 16:28 
Diese FAQ ist sehr interessant ;)
mad-kow
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 22



BeitragVerfasst: So 28.11.04 16:28 
guck mal in die delphi hilfe, das, was da drin steht, sollte dir weiterhelfen.
mlspider Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 33



BeitragVerfasst: So 28.11.04 16:40 
Ich habe jetzt folgendes übernommen:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
Procedure FindFiles (aPath, aFindMask: String; aWithSub: Boolean; aResult: tStrings);
Var
  FindRec: tSearchRec;
Begin
If FindFirst (aPath + aFindMask, faAnyFile, FindRec) = 0 Then
    Repeat
      If (FindRec.Name <> '.') and (FindRec.Name <> '..') Then
        // ...Ergebnis in die Stringlist einfügen
        aResult.Add (aPath + FindRec.Name);  
    Until FindNext (FindRec) <> 0;


  FindClose (FindRec);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
 FindFiles ('J:\Test','*.mp3', True, ListBox1.Items);
end;


Es werden aber keine Dateien mit der Endung mp3 angezeigt, obwohl ich gerade für diesem Zweck welche in diesen Ordner kopiert habe.
mlspider Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 33



BeitragVerfasst: So 28.11.04 16:46 
Problem gelöst. Hatte im Aufruf:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
procedure TForm1.Button1Click(Sender: TObject);
begin
 FindFiles ('J:\Test','*.mp3', True, ListBox1.Items);
end;


ein Backslash hinter Test vergessen. Nun weiß ich aber nicht, wie ich nun alle gefundene Dateien in einem Rutsch unwiederbringlich (also nicht wiederherstellbar) löschen soll.
herzi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 168

WINXP pro, SUSE Linux 9.0
D7 Pers.
BeitragVerfasst: So 28.11.04 16:49 
Mit ......
ausblenden Delphi-Quelltext
1:
  deletefile(aPath + FindRec.Name);					
Johannes Maier
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 173

Win XP
D7 Prof
BeitragVerfasst: So 28.11.04 16:56 
Hast du deinen CodeSchnipsel mal getestet? Wollte ich nämlich eben mal machen, aber anstatt mir eine Datei zu löschen, hat er sie zwar umbenannt, jedoch auch bis ich abgebrochen hatte schon ein GB vollgeschrieben :shock: Vorher stand da nur "Test" drin ....
Ich verstehe diese Teile irgendwie nicht:
ausblenden Delphi-Quelltext
1:
2:
3:
for j := 0 to CountToWrite do
  for i := 1 to n do
    BlockWrite(F, FillBuffer[j], Buffer); // Hier schalte ich ab ... Wieso denn j? FillBuffer geht doch nur bis 5 und CountToWrite ist 34 ....

Btw: Wo kommt denn bitte arr nochmal vor?

Gruß

_________________
MfG
Johannes ehem. jbmaier
mlspider Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 33



BeitragVerfasst: So 28.11.04 16:57 
hallo herzi,

irgendwie will Deine Procedure nicht funktionieren.
mlspider Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 33



BeitragVerfasst: So 28.11.04 17:00 
Hallo Johannes,

Ja ich habe das Teil auch getestet. Zugegeben, die Dateien werden sehr groß. Allerdings läßt sich der Inhalt nicht wieder herstellen.
Johannes Maier
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 173

Win XP
D7 Prof
BeitragVerfasst: So 28.11.04 17:07 
Also überschreibt er sie nur? Bei mir hat eine nur 4 KB große Datei schon geraume Zeit gebraucht, bis sie mal 1 GB groß war, und nach 10s oder so hab ich abgebrochen. gelöscht hat er sie da noch nicht gehabt .... Wie lange dauert das Löschen bei dir, bei einer .txt mit dem Wort "Test" drin?

_________________
MfG
Johannes ehem. jbmaier
mlspider Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 33



BeitragVerfasst: So 28.11.04 17:10 
Genau 2 sec.
herzi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 168

WINXP pro, SUSE Linux 9.0
D7 Pers.
BeitragVerfasst: So 28.11.04 17:10 
Damit kannst du Dateien normal löschen mit FindFirst etc....

Jetzt musst du nur noch deine Prozedur hier einbinden!!
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:
Procedure DeleteFiles(aPath, aFindMask: String; aWithSub: Boolean);
Var
  FindRec: tSearchRec;
Begin
  // Wenn aPath oder aFind nicht angegeben ist
  // dann raus
  If (aPath = '') or (aFindMask = '') Then
    Exit;

  // Wenn am Ende der Pfadangabe noch kein \ steht, dieses hinzufügen
  If aPath[Length (aPath)] <> '\' Then
    aPath := aPath + '\';

  // Im aktuellen Verzeichnis nach der Datei suchen
  If FindFirst (aPath + aFindMask, faAnyFile, FindRec) = 0 Then
    Repeat
      If (FindRec.Name <> '.') and (FindRec.Name <> '..') Then
        // ...Ergebnis löschen
        deletefile(aPath + FindRec.Name);
    Until FindNext (FindRec) <> 0;

  FindClose (FindRec);

  // Wenn nicht in Unterverzeichnissen gesucht werden soll dann raus
  If Not aWithSub Then
    Exit;

  // In Unterverzeichnissen weiter suchen
  If FindFirst (aPath + '*.*', faAnyFile, FindRec) = 0 Then
    Repeat
      If (FindRec.Name <> '.') and (FindRec.Name <> '..') Then
        // Feststellen, ob es sich um ein Verzeichnis handelt
        If Boolean (FindRec.Attr and faDirectory) Then
          // Funktion erneut aufrufen, um Verzeichnis zu durchsuchen (Rekursion)
          DeleteFiles (aPath + FindRec.Name, aFindMask, aWithSub);
    Until FindNext (FindRec) <> 0;

   FindClose (FindRec);
End;

procedure TForm1.Button1Click(Sender: TObject);
begin
  DeleteFiles ('c:\test', '*.*', True);
end;
mlspider Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 33



BeitragVerfasst: So 28.11.04 17:14 
Das klappt schon. Allerdings lassen sich die gelöschten Dateien wieder herstellen und der Inhalt ist intakt. Wie kann ich nun die Eingang eingebrachten Code zum überschreiben in Deinem Code integrieren???????
herzi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 168

WINXP pro, SUSE Linux 9.0
D7 Pers.
BeitragVerfasst: So 28.11.04 17:30 
Hab Die WipeFile Funktion von MathiasSimmack(Klick mich) genommen.

Alles zu Finden in der SuchFunktion ;)


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:
function WipeFile(const szFilename: string): boolean;
var
  f      : file;
  buf    : array[0..65535]of byte;
  i,
  iRead,
  iWrite : integer;
  fpos   : longint;
begin
  {$I-}
  AssignFile(f,szFileName);
  ReSet(f,1);

  if(IoResult = 0) then begin
    repeat
      fPos := filepos(f);                    // aktuelle Position merken
      BlockRead(f,buf,sizeof(buf),iRead);    // max. 65k lesen

      // der tatsächlich gelesene Wert steckt in "iRead" und
      // ist irgendwann Null, wenn das Ende der Datei erreicht
      // wurde und keine Bytes mehr gelesen werden können
      if(iRead > 0) then
        // (alte?) Methode des DOD (US-Verteidigungsministerium):
        // einmal 00 schreiben; einmal FF schreiben (3 Wiederholungen)
        // dann F6 schreiben
        // (Norton WipeInfo macht´s heute noch so; außer, das 0x01
        // anstelle von 0xff angegeben ist)
        for i := 1 to 7 do begin
          case i of
            1,3,5 : fillchar(buf,iRead,  0); // Inhalt mit 0x00 überschreiben
            2,4,6 : fillchar(buf,iRead,255); // Inhalt mit 0xff überschreiben
            7     : fillchar(buf,iRead,246); // Inhalt mit 0xf6 überschreiben
          end;

          seek(f,fPos);                      // gespeicherte Position suchen
          BlockWrite(f,buf,iRead,iWrite);    // Puffer schreiben
          if(iWrite <> iRead) then break;    // Fehler! :o(
        end;
    until(iRead = 0) or (iWrite <> iRead);

    CloseFile(f);                            // alte Datei schließen
    Rename(f,'D');                           // alten Namen "vernichten"
    ReWrite(f,1);                            // alte Position "vernichten"
    CloseFile(f);
    Erase(f);                                // Datei löschen
  end;
  {$I+}

  Result := (IoResult = 0);
end;

Procedure DeleteFiles(aPath, aFindMask: String; aWithSub: Boolean);
Var
  FindRec: tSearchRec;
Begin
  // Wenn aPath oder aFind nicht angegeben ist
  // dann raus
  If (aPath = '') or (aFindMask = '') Then
    Exit;

  // Wenn am Ende der Pfadangabe noch kein \ steht, dieses hinzufügen
  If aPath[Length (aPath)] <> '\' Then
    aPath := aPath + '\';

  // Im aktuellen Verzeichnis nach der Datei suchen
  If FindFirst (aPath + aFindMask, faAnyFile, FindRec) = 0 Then
    Repeat
      If (FindRec.Name <> '.') and (FindRec.Name <> '..') Then
        // ...Ergebnis löschen
        WipeFile(aPath + FindRec.Name);
    Until FindNext (FindRec) <> 0;

  FindClose (FindRec);

  // Wenn nicht in Unterverzeichnissen gesucht werden soll dann raus
  If Not aWithSub Then
    Exit;

  // In Unterverzeichnissen weiter suchen
  If FindFirst (aPath + '*.*', faAnyFile, FindRec) = 0 Then
    Repeat
      If (FindRec.Name <> '.') and (FindRec.Name <> '..') Then
        // Feststellen, ob es sich um ein Verzeichnis handelt
        If Boolean (FindRec.Attr and faDirectory) Then
          // Funktion erneut aufrufen, um Verzeichnis zu durchsuchen (Rekursion)
          DeleteFiles (aPath + FindRec.Name, aFindMask, aWithSub);
    Until FindNext (FindRec) <> 0;

   FindClose (FindRec);
End;

procedure TForm1.Button1Click(Sender: TObject);
begin
  DeleteFiles ('c:\test', '*.*', True);
end;
mlspider Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 33



BeitragVerfasst: So 28.11.04 17:35 
Hallo Heri,

du bist einfach super!!! Danke!!!
mlspider Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 33



BeitragVerfasst: So 28.11.04 17:47 
Herzi, die mp3 Dateien lassen sich immernoch wieder herstellen. Leider.
herzi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 168

WINXP pro, SUSE Linux 9.0
D7 Pers.
BeitragVerfasst: So 28.11.04 18:03 
Sind das evtl die Alten.... ?

Von vorigen Löscheversuchen !
mlspider Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 33



BeitragVerfasst: So 28.11.04 18:08 
Ja Du hast recht. Ich habe gerade nochmals ein paar Dateien hinzu gefügt. Sie sind tatsächlich nicht wieder herstellbar!!!

Der Code ist einfach genial!!!!! Nun kann ich mein Vorhaben, einen Shredder, verwirklichen. Danke!!!