### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: File creation logic  (Read 1121 times)

#### BIT

• Full Member
• Posts: 126
##### 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)

#### winni

• Hero Member
• Posts: 2807
##### 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: 126
##### 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: 126
##### 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