Recent

Author Topic: File creation logic  (Read 1469 times)

BIT

  • Full Member
  • ***
  • Posts: 158
File creation logic
« on: September 19, 2021, 04:45:23 pm »
In many editors, when creating a file, the name is given New1 New2 ... How to track that for example New * exists and specify the number of which does not exist.? (I am writing my own file editor)
: Possibly bad translation.

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: File creation logic
« Reply #1 on: September 19, 2021, 04:51:06 pm »
Hi!

Just do this:

Code: Pascal  [Select][+][-]
  1. var i: integer =0;
  2. fname : string;
  3. ...
  4. repeat
  5.  inc(i);
  6. fname := 'New'+IntToStr(i)+'.txt';
  7. until not fileExists(fname);
  8. showMessage (fname+'  is the new filename');
  9. end;

Winni

BIT

  • Full Member
  • ***
  • Posts: 158
Re: File creation logic
« Reply #2 on: September 19, 2021, 04:59:01 pm »
Hi!

Just do this:

Code: Pascal  [Select][+][-]
  1. var i: integer =0;
  2. fname : string;
  3. ...
  4. repeat
  5.  inc(i);
  6. fname := 'New'+IntToStr(i)+'.txt';
  7. until not fileExists(fname);
  8. showMessage (fname+'  is the new filename');
  9. end;

Winni
This is understandable, but not clear) If there was New1, New2, New3, New4. And removed New2. When creating a new file, name it New2.

BIT

  • Full Member
  • ***
  • Posts: 158
Re: File creation logic
« Reply #3 on: September 19, 2021, 05:09:10 pm »
Hi!

Just do this:

Code: Pascal  [Select][+][-]
  1. var i: integer =0;
  2. fname : string;
  3. ...
  4. repeat
  5.  inc(i);
  6. fname := 'New'+IntToStr(i)+'.txt';
  7. until not fileExists(fname);
  8. showMessage (fname+'  is the new filename');
  9. end;

Winni
All perfectly! I read your code inattentively, Thank you!

jamie

  • Hero Member
  • *****
  • Posts: 6077
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018