Autor Beitrag
herni
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 20
Erhaltene Danke: 1



BeitragVerfasst: So 26.03.06 19:24 
Hallo Leute,

und zwar habe ich eine existierende Textdatei. Nun möchte ich die Datei einfach nur kopieren. Oder um es präziser auszudrücken an einem beliebigem Ort unter einem beliebigen Namen abzuspeichern, wobei die alte Datei erhalten bleiben soll.

Ich will also nicht öffnen, um zu lesen oder so, sondern einfach die Datei wie oben beschrieben kopieren. Wie funktioniert das in Delphi?

mfg herni
Danniolo
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 693



BeitragVerfasst: So 26.03.06 19:29 
guck dir mal "CopyFile" genauer an...
Marco D.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2750

Windows Vista
Delphi 7, Delphi 2005 PE, PHP 4 + 5 (Notepad++), Java (Eclipse), XML, XML Schema, ABAP, ABAP OO
BeitragVerfasst: So 26.03.06 19:34 
Ich nehm ihm mal die Arbeit ab :mrgreen:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
uses
 ShellApi;

function CopyFileEx(const ASource, ADest: String;
 ARenameCheck: boolean = false): boolean;
var
  sh: TSHFileOpStruct;
begin
  sh.Wnd := Application.Handle;
  sh.wFunc := FO_COPY;

  // String muss mit #0#0 Terminiert werden, um das Listenende zu setzen
  sh.pFrom := PChar(ASource + #0);
  sh.pTo := PChar(ADest + #0);
  sh.fFlags := fof_Silent or fof_MultiDestFiles;
  if ARenameCheck then
    sh.fFlags := sh.fFlags or fof_RenameOnCollision;
  Result:=ShFileOperation(sh)=0;
end;

_________________
Pascal keeps your hand tied. C gives you enough rope to hang yourself. C++ gives you enough rope to shoot yourself in the foot
Danniolo
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 693



BeitragVerfasst: So 26.03.06 19:36 
Geht auch einfach über "CopyFile([...])".
Dieses Gedöns brauch man dafür garnet.
Blackheart666
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2195

XP
D3Prof, D6Pers.
BeitragVerfasst: So 26.03.06 19:43 
user profile iconDanniolo hat folgendes geschrieben:
Geht auch einfach über "CopyFile([...])".
Dieses Gedöns brauch man dafür garnet.


Stimmt !

ausblenden Delphi-Quelltext
1:
CopyFile(PChar('C:\Test.txt'), PChar('C:\Neuer Ordner\Test.txt'), true);					
herni Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 20
Erhaltene Danke: 1



BeitragVerfasst: So 26.03.06 19:55 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
procedure TForm1.speichernClick(Sender: TObject);
begin
  SaveDialog1.Filter:='Text Files (*.txt)';
  If SaveDialog1.Execute then
    begin
      CopyFile('partie.txt','hans.txt',False);
    end

  else close;
end;


Also, das mit dem Copy funzt soweit, besten Dank. Nur hab ich jetzt folgendes Problem. Leider kann ich anstatt des neuen Dateinnamens (hans.txt) nicht SaveDialog1.FileName schreiben, weil die Typen nicht passen. Was muss ich tun, damit er mir Pfad und Name der neuen Datei anhand der im SaveDialog gemachten Angaben macht? Ach und was hat die boolean variable am Ende für einen Zweck?

Moderiert von user profile iconChristian S.: Code- durch Delphi-Tags ersetzt
Danniolo
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 693



BeitragVerfasst: So 26.03.06 19:57 
Du musst SaveDialog1.Filename erst noch umwandeln:
ausblenden Delphi-Quelltext
1:
PCahr(SaveDialog1.Filename);					
Blackheart666
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2195

XP
D3Prof, D6Pers.
BeitragVerfasst: So 26.03.06 20:09 
Bei True schlägt die Funktion fehl falls die Datei schon exestiert.
Bei False wird eine bereits vorhandene Datei ersetzt.
herni Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 20
Erhaltene Danke: 1



BeitragVerfasst: So 26.03.06 20:31 
web25.ks1.kdsrv.de/leer/Projekt13.rar

Hier könnt ihr euch mein Programm runterladen an dem ich werkel.

Macht einfach Neues Spiel, setzt ein paar Steine und klickt dann Spiel Abbrechen.

Dann könnt ihr das Spiel speichern. (procedure speichern)
Und hier ist das Problem um das es geht. Ich hab das jetzt so gemacht wie ihr geschrieben habt, es lässt sich auch compilieren und man kann das dann auch machen ohne das es zu einem Fehler kommt. Aber er macht es nicht, sprich er kopiert die Datei nicht. Hat jemand einen Schimmer warum?

P.S.: Die Prozedur speichern ist im unteren Teil des Quelltextes
Danniolo
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 693



BeitragVerfasst: So 26.03.06 20:41 
So funktioniert es:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
procedure TForm1.speichernClick(Sender: TObject);
begin
  SaveDialog1.Filter:='Text Files (*.txt)';
  If SaveDialog1.Execute then
  begin
    CopyFile(PChar(ExtractFilePath(Application.Exename)+'\partie.txt'),PChar(SaveDialog1.FileName),False);
  end
  else
    close;
end;