Autor Beitrag
Apo95
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 129

Win2000, WinXP, WinServer 2003 Standard x86, Win7x64
Delphi 6 Enterprise, D2009 Architect, RAD Studio XE6 Architect
BeitragVerfasst: So 24.11.13 02:47 
Hi,

ich suche nach einem Befehl, der den Dateinamen um 1 erhöht. Sollte der Name anders sein als der vorhergehende, muss eine neue Datei mit einem neuen Namen erzeugt werden.

Bsp. Alte Datei : Data.qz3
Neue Datei (neuer Spielername) : Data1.qz3

Wie kann ich das am besten lösen ? Mit ner while-Schleife?

Danke im Voraus !

_________________
Lette-Verein MIA
Bergmann89
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1742
Erhaltene Danke: 72

Win7 x64, Ubuntu 11.10
Delphi 7 Personal, Lazarus/FPC 2.2.4, C, C++, C# (Visual Studio 2010), PHP, Java (Netbeans, Eclipse)
BeitragVerfasst: So 24.11.13 03:02 
Ja, ne while-Schleife geht da. :P

_________________
Ich weiß nicht viel, lern aber dafür umso schneller^^
Apo95 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 129

Win2000, WinXP, WinServer 2003 Standard x86, Win7x64
Delphi 6 Enterprise, D2009 Architect, RAD Studio XE6 Architect
BeitragVerfasst: So 24.11.13 03:37 
Ich habe einen Beispiel-Code , aber der geht nicht... :-(

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
while FileExists(cFRileName)
 do
begin
i := 1;
cFilename := 'Data'+IntotoStr(i)+'.qz3';
inc(i);
end;

_________________
Lette-Verein MIA
Bergmann89
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1742
Erhaltene Danke: 72

Win7 x64, Ubuntu 11.10
Delphi 7 Personal, Lazarus/FPC 2.2.4, C, C++, C# (Visual Studio 2010), PHP, Java (Netbeans, Eclipse)
BeitragVerfasst: So 24.11.13 03:50 
Ist ja schon fast richtig was dort steht. Ließ mal Zeile für Zeile vor was dort gemacht wird.

_________________
Ich weiß nicht viel, lern aber dafür umso schneller^^
Perlsau
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: So 24.11.13 06:46 
@Apo95: "Geht aber nicht" ist keine aussagekräftige Fehlermeldung.
Du setzt in der Schleife erst mal i auf 1, dann erzeugst du den Dateinamen, erhöhst i um 1 und die Schleife beginnt von Neuem, wobei du i dann wieder auf 1 setzt.
Dann verwendest du einmal cFRileName und ein andermal cFilename, also zwei unterschiedliche Variablen.
Und zu guter Letzt möchte ich dir von relativen Dateinamen dringendst abraten. Verwende immer nur vollständige Dateibezeichner, also incl. Pfad.

Kleiner Tip: Wenn du Abläufe kontrollieren willst, setze einen Breakpoint (F5) und steppe die einzelnen Schritt mit F8 durch ... dann siehst du immer ganz genau, welchen Wert z.B. i gerade annimmt und dir fallen solche Fehler ganz alleine auf.
WasWeißDennIch
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 653
Erhaltene Danke: 160



BeitragVerfasst: So 24.11.13 12:38 
Außerdem bietet sich IMO hier eher eine Fußschleife an, da sie ja mindestens einmal durchlaufen werden soll.
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
Pfad := IncludeTrailingPathDelimiter(Zielverzeichnis);
i := 0;
repeat
  inc(i);
  cFilename := Format('%sData%d.qz3',[Pfad, i]);
until not FileExists(cFilename);
Bergmann89
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1742
Erhaltene Danke: 72

Win7 x64, Ubuntu 11.10
Delphi 7 Personal, Lazarus/FPC 2.2.4, C, C++, C# (Visual Studio 2010), PHP, Java (Netbeans, Eclipse)
BeitragVerfasst: So 24.11.13 15:33 
Hey,

ne while-Schleife war schon richtig, weil du ja checken musst, ob die Datei ohne Ziffer vorhanden ist...

MfG Bergmann.

_________________
Ich weiß nicht viel, lern aber dafür umso schneller^^
WasWeißDennIch
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 653
Erhaltene Danke: 160



BeitragVerfasst: So 24.11.13 16:03 
Hä? Ohne Ziffer musst Du ja in jedem Fall prüfen, bevor Du überhaupt in die Schleife eintrittst.
Bergmann89
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1742
Erhaltene Danke: 72

Win7 x64, Ubuntu 11.10
Delphi 7 Personal, Lazarus/FPC 2.2.4, C, C++, C# (Visual Studio 2010), PHP, Java (Netbeans, Eclipse)
BeitragVerfasst: So 24.11.13 17:23 
Ne, warum?

ausblenden Quelltext
1:
2:
3:
4:
5:
i initialisieren
solange datei vorhanden:
  neuen dateiname erstellen
  i erhöhen
end

_________________
Ich weiß nicht viel, lern aber dafür umso schneller^^
hathor
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: So 24.11.13 17:49 
www.delphipraxis.net/1209764-post4.html

Die Funktion GetNextFreeFileName ist selbsterklärend.
Die Procedure ist nur beispielhaft.
Ergebnis: Data001.qz3, Data002.qz3 usw

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:
function GetNextFreeFileName(const FileName: string;
const sFormat: string = '%.3d'): string;
var i: integer;
    sPath, sExt, fileconst: string;
begin
fileconst:='Data'//womit Das File beginnt
  sPath := IncludeTrailingBackslash(ExtractFilePath(FileName));
  sExt := ExtractFileExt(FileName);
  i := 0;
  repeat
    inc(i);
    Result := sPath +fileconst+ Format(sFormat,[i]) + sExt;
  until not FileExists(Result);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin  //
Memo1.lines.add('test');
Memo1.lines.savetofile(GetNextFreeFileName(ExtractFilePath(Application.ExeName) +'Data.qz3'));
end;

//Ergebnis: Data001.qz3, Data002.qz3 usw
Apo95 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 129

Win2000, WinXP, WinServer 2003 Standard x86, Win7x64
Delphi 6 Enterprise, D2009 Architect, RAD Studio XE6 Architect
BeitragVerfasst: Di 03.12.13 20:57 
Danke für die schnellen Antworten. Leider, Hathor, habe cih Probleme beim Einbinden deiner Function; w2arum fehlt eine Klammer bei der Function? Der Compiler meckerte, dass die overload-Kennzeichnung fehlt.

_________________
Lette-Verein MIA
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19314
Erhaltene Danke: 1747

W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Di 03.12.13 21:50 
user profile iconApo95 hat folgendes geschrieben Zum zitierten Posting springen:
Leider, Hathor, habe cih Probleme beim Einbinden deiner Function; w2arum fehlt eine Klammer bei der Function? Der Compiler meckerte, dass die overload-Kennzeichnung fehlt.
Overload fehlt bedeutet zwei gleichnamige Funktionen. Da es hier nur eine gibt, kann das Problem unmöglich an dem Code liegen, den user profile iconhathor gepostet hat. Und eine fehlende Klammer kann ich da auch nirgends entdecken.
hathor
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Di 03.12.13 22:21 
Diese beiden Zeilen sind in Wirklichkeit nur eine:

ausblenden Delphi-Quelltext
1:
2:
function GetNextFreeFileName(const FileName: string;
const sFormat: string = '%.3d'): string;


Hast Du da etwas geändert?
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19314
Erhaltene Danke: 1747

W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Di 03.12.13 23:37 
user profile iconhathor hat folgendes geschrieben Zum zitierten Posting springen:
Diese beiden Zeilen sind in Wirklichkeit nur eine:
Da man das schreiben kann wie man will, sollte das ja vollkommen egal sein. Aber du hast Recht, man weiß ja nie....
WasWeißDennIch
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 653
Erhaltene Danke: 160



BeitragVerfasst: Mi 04.12.13 09:20 
Vermutlich gibt es in seinem Code jetzt 2 Versionen von GetNextFreeFileName, wobei in der unbekannten eine Klammer fehlt. Das würde beide Meldungen erklären.
Apo95 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 129

Win2000, WinXP, WinServer 2003 Standard x86, Win7x64
Delphi 6 Enterprise, D2009 Architect, RAD Studio XE6 Architect
BeitragVerfasst: Mi 18.12.13 12:02 
Danke! Ich habe das Problem mit der while-Schleife lösen können. Danke an alle!

_________________
Lette-Verein MIA
hathor
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Mi 18.12.13 14:13 
Wie, was ?

Du kommst auf meine schwarze Liste, wenn Du nicht "vernünftig" antwortest!