Autor Beitrag
Wolle92
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 1296

Windows Vista Home Premium
Delphi 7 PE, Delphi 7 Portable, bald C++ & DirectX
BeitragVerfasst: Do 01.11.07 15:44 
Hallo,
ich will mir grade ein Programm basteln, welches Teile von Dateien ersetzt...
Nur wie kann ich das am besten mit Delphi machen?
Geht es, dass ich Zeile für Zeile auslese, darin dann das gesuchte ersetzte und dann das neue wieder an die gleiche Stelle schreibe?


Moderiert von user profile iconjasocul: Topic aus Delphi Language (Object-Pascal) / CLX verschoben am Do 01.11.2007 um 15:46
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Do 01.11.07 15:49 
Wie wäre es mit einer TStringList? Da musst du lediglich die Datei hineinladen (LoadFromFile) und mit ReplaceString dann den gesuchten Text (der steht in der Eigenschaft Text der StringList) ersetzen. (Speichern mit SaveToFile wäre danach evtl. auch noch ne Idee. ;-))
Wolle92 Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 1296

Windows Vista Home Premium
Delphi 7 PE, Delphi 7 Portable, bald C++ & DirectX
BeitragVerfasst: Do 01.11.07 16:48 
so, jetzt komm ich wieder mit Source Code...
Es gibt kein Gemecker vom Compiler, auch sonst nichts...
Aber es funktioniert nicht...
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:
procedure TForm1.ReplaceFiles(dir: String; srch: String; rplc: String; ext: String; specExt: Boolean; searchSub: Boolean);
var sr: TSearchRec;
    r: Integer;
    curf: TStringList;
begin
  curf := TStringList.Create;
  if searchSub then
  begin
    //Mit Unterordnern
    r := FindFirst(dir + '*.*', faAnyFile, sr);
    while r = 0 do
    begin
      if (sr.Name <> '.') and (sr.Name <> '..') then
      begin
        if ((sr.Attr and faDirectory) <> 0) then
        begin
          Memo1.Lines.Add('Unterverzeichnis gefunden: ' + sr.Name);
          ReplaceFiles(dir+sr.Name+'\',srch,rplc,ext,specExt,searchSub);
        end
        else
        begin
          Memo1.Lines.Add(sr.Name);
          if (not specExt) or ((specExt) and (ExtractFileExt(sr.Name) = ext)) then
          begin
            Memo1.Lines.Add('Öffne Datei ' + sr.Name);
            curf.LoadFromFile(dir + sr.Name);
            Memo1.Lines.Add('Bearbeite ' + sr.Name);
            curf.Text := StringReplace(curf.Text,srch,rplc,[rfReplaceAll]);
            Memo1.Lines.Add('Schließe ' + sr.Name);
            curf.SaveToFile(dir + sr.Name);
            curf.Clear;
            Inc(total);
          end;
        end;
      end;
      Application.ProcessMessages;
      r := FindNext(sr);
    end;
    FindClose(sr);
  end
  else
  begin
    //Ohne Unterordner
    r := FindFirst(dir + '*.*', faAnyFile, sr);
    while r = 0 do
    begin
      if (sr.Name <> '.') and (sr.Name <> '..') then
      begin
        if ((sr.Attr and faDirectory) = 0) then
        begin
          Memo1.Lines.Add(sr.Name);
          if (not specExt) or ((specExt) and (ExtractFileExt(sr.Name) = ext)) then
          begin
            Memo1.Lines.Add('Öffne Datei ' + sr.Name);
            curf.LoadFromFile(dir + sr.Name);
            Memo1.Lines.Add('Bearbeite ' + sr.Name);
            curf.Text := StringReplace(curf.Text,srch,rplc,[rfReplaceAll]);
            Memo1.Lines.Add('Schließe ' + sr.Name);
            curf.SaveToFile(dir + sr.Name);
            curf.Clear;
            Inc(total);
          end;
        end;
      end;
      r := FindNext(sr);
    end;
    FindClose(sr);
  end;
end;
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Do 01.11.07 18:18 
Inwiefern gehts nicht? Ich habe es gerade mit einer Textdatei in einem Verzeichnis mit mehreren Dateien getestet, das ging...
Wolle92 Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 1296

Windows Vista Home Premium
Delphi 7 PE, Delphi 7 Portable, bald C++ & DirectX
BeitragVerfasst: Do 01.11.07 20:14 
Habs debuggt, r ist bei mir nicht gleich 0...
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Do 01.11.07 20:23 
Womit rufst du die Datei auf? Wie sieht der Code dafür aus? Und falls du den Wert von Variablen, Edits, oder so übergibst: was steht da drin?
Btw: Dass du für searchSub gar nicht so viel Quelltext doppelt schreiben musst, ist dir aber schon klar, oder?
Wolle92 Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 1296

Windows Vista Home Premium
Delphi 7 PE, Delphi 7 Portable, bald C++ & DirectX
BeitragVerfasst: Fr 02.11.07 10:13 
das mit searchSub ist mir klar, das ändere ich, sobald es wieder geht...
Also, ich kann dir ja mal die Werte von meinem Test, der ja nicht funktioniert geben:
dir: C:\Users\Wolle\Documents\Internetdateien\Pfadfinder HP\
srch: schwachsinn
rplc: unsinn
ext: entweder gar nichts oder .txt
specExt: true oder false (bei true ist die die ext .txt)
searchSub: auch true oder false, für zwei verschiedene tests...
Also, die ersten drei Parameter sind bei meinen Test immer gleich...

Edit: OK, hab den Fehler gefunden, einfacher Schusselfehler, es stand nicht C:\Users\Wolle drin sondern C:\UsersWolle
:autsch: :autsch: :autsch: :autsch: :autsch: :autsch: :autsch: :autsch:
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Fr 02.11.07 14:21 
user profile iconWolle92 hat folgendes geschrieben:
Edit: OK, hab den Fehler gefunden, einfacher Schusselfehler, es stand nicht C:\Users\Wolle drin sondern C:\UsersWolle
Da ich mir sowas dachte, habe ich ja auch nach den Aufrufwerten gefragt ;-). Wenn man die nochmal losgelöst vom Programm sieht (ich dachte ja, du kopierst die Werte aus dem Programm ;-)), dann merkt man solche Fehler eher.
(Ich hatte entweder mit solch einem Tippfehler aber eigentlich eher z.B. mit einem fehlenden Backslash am Ende gerechnet.)
Wolle92 Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 1296

Windows Vista Home Premium
Delphi 7 PE, Delphi 7 Portable, bald C++ & DirectX
BeitragVerfasst: Fr 02.11.07 14:23 
an den am Ende hab ich gedacht, hab sogar noch eine Funktion eingebaut, die den, falls er fehlt hinsetzt
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Fr 02.11.07 14:24 
Dafür gibt es ja IncludeTrailingPathDelimiter, die macht das ;-).
Wolle92 Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 1296

Windows Vista Home Premium
Delphi 7 PE, Delphi 7 Portable, bald C++ & DirectX
BeitragVerfasst: Mo 17.12.07 19:09 
ganz kurz noch: der funktionsname ist mir viel zu lang, hab eine "SetBackslash" gemacht

_________________
1405006117752879898543142606244511569936384000000000.