1

Topic: File saving. ordinal value

Someone can already solved the mentioned below task, I seek the code not to write the.
The task:
Before the next saving of a file,
- Opens SaveDialog,
- In it the file name is prompted
- We save always xls files
The file name is prompted on algorithm:
- The file title is used
-- Or transferred in the parameter "new file name", for example ' 2016-12-08 Waybill ', ' 2016-12-08 Accounts ' (can miss)
-- Or old SaveDialog. FileName
- If in a directory already there is a file with such name the double-valued ordinal value (02, 03) steals up so that the file such number yet was not in this directory.
- Before number always there is a hyphen.
2016-12-08 Waybill
2016-12-08 Nakladnaja-02
2016-12-08 Schet-98
2016-12-08 Schet-99
2016-12-08 Schet-00

2

Re: File saving. ordinal value

Rodriguez wrote:

I seek the code not to write the.

Why? The algorithm is worked. It is individual enough. Most likely you do not find the code which completely corresponded to the resulted algorithm. Something is not clear how to do? Or it is difficult enough for you?

3

Re: File saving. ordinal value

Not difficult, well and suddenly someone solved smile when I will solve, itself source codes I will publish.

4

Re: File saving. ordinal value

12 minutes? 2 minutes.
  a forum and to create a subject of more time occupies.

NameBase: = ' 2016-12-08 Waybill ';
NameTemplate: = ' %s' + ' (%u) ';//: = ' %s' + ' - % 02u '//: = ' %s' + ' a copy %u ' etc.
i: = 1;//: = 0;: =-1;....
NameCandidate: = NameBase + NameExtension;
while FileExists (NameCandidate) do begin
Inc (i);
NameCandidate: = Format (NameTemplate, [NameBase, i]) + NameExtension;
end;

5

Re: File saving. ordinal value

Thanks, for

6

Re: File saving. ordinal value

Here still a candidate solution (did not begin to wait, began to write):
To cause so

ReportToExcel (' the Waybill ');

Or so

ReportToExcel (");

In the second case there will be a Report .xls
It is possible to make function
Can , but works

function GetNameCandidate (AOldFileName, ANewFileName, ADefFileName, ADefExt, AInitDir: string): string;
//Prompts new file name proceeding from the last saved file;
//the new extension, a new name ().
//If the new name is not specified, uses the old.
//Replaces the extension with the new
//the Process directory takes from an old name;
//if saves the first time the directory takes from AInitDir if it empty - takes the Desktop
//If the file in the directory already is selects a file adding of a numeral index since 02
//(a file-02, a file-03) so that such file in the directory yet was not, and so to 99;
//the last index does not change.
//
//
//AOldFileName - old a way c file name
//ANewFileName - new file name without the extension if is
//ADefFileName - file name on-default if it is not specified ANewFileName
//ADefExt - the new extension, or the extension on-default
//AInitDir - the initialization directory if it is not specified in an old way
var
NewFileName: string;
i, FileNumber: integer;
function GetDeskTopPath: string;
//Returns a desktop folder
var
SpecialDir: PItemIdList;
begin
SetLength (result, MAX_PATH);
SHGetSpecialFolderLocation (Form1.Handle, CSIDL_DESKTOP, SpecialDir);
SHGetPathFromIDList (SpecialDir, PChar (Result));
SetLength (result, lStrLen (PChar (Result)));
end;
function IsFNHaveNumber (AFileName: string; var AFNameWONumber: string; var ANumber: integer): boolean;
Whether//Checks there is in file name in the end format number-NN
//If is - returns number
//- in AFNameWONumber file name without-NN
//- in ANumber - number
//If is not present
//- in AFNameWONumber file name
var
FileExt: string;
begin
AFileName: = Trim (AFileName);
//we Remove and we remember the extension:
//we Take the extension
FileExt: = ExtractFileExt (AFileName);
//we Remove at file name the extension
AFileName: = StringReplace (AFileName, FileExt, ", []);
AFNameWONumber: = AFileName;
ANumber: = 0;
//on-default - all is bad
Result: = False;
//If a name small an output
if Length (AFileName) <3 then
Exit;
//If in the end-NN
if (AFileName [Length (AFileName)-2] = ' - ')
and (AFileName [Length (AFileName)-1] in [' 0 '. ' 9 '])
and (AFileName [Length (AFileName)] in [' 0 '. ' 9 ']) then
begin
//we Receive file name without-NN
AFNameWONumber: = Copy (AFileName, 0, Length (AFileName)-3);
//we receive number
ANumber: = StrToInt (Copy (AFileName, Length (AFileName)-1, 2));
//the Output.
Result: = True
end;
end;
begin
//If  directories do not exist, we nullify it
if not DirectoryExists (AInitDir)
then AInitDir: = ";
//If did not transfer any way
if AOldFileName = "then
begin
//If the directory of initialization nonblank
//we install a way on-default
if AInitDir <>"
then AOldFileName: = IncludeTrailingBackslash (AInitDir)//the initialization directory
else AOldFileName: = IncludeTrailingBackslash (GetDeskTopPath);//a desktop
//If were not saved before and
//If there is no new name and there is a name on-default
//that to an old way we add default name and  extension
if (ANewFileName = ")
and (ADefFileName <> ") then
if ADefExt <>"
then AOldFileName: = AOldFileName + ADefFileName + '. ' + ADefExt
else AOldFileName: = AOldFileName + ADefFileName;
end;
//If there is a new file name and at it is not present  and transferred the extension on-default
//that we add the extension to a file
if (ANewFileName <> ")
and (ExtractFileExt (ANewFileName) = ")
and (ADefExt <> ")
then ANewFileName: = ANewFileName + '. ' + ADefExt;
//If the new file name is set
//we substitute it in an old name with a way
if ANewFileName <> "then
if ExtractFileDir (AOldFileName) <>"
then AOldFileName: = IncludeTrailingBackslash (ExtractFileDir (AOldFileName)) + ANewFileName
else AOldFileName: = ANewFileName;
//If the extension of an old file differs from the extension on-default
//we change the extension
if (ADefExt <> ")
and (ExtractFileExt (AOldFileName) <> ")
and (ExtractFileExt (AOldFileName) <> ('. ' + ADefExt))
then AOldFileName: = StringReplace (AOldFileName, ExtractFileExt (AOldFileName), ", []) + '. ' + ADefExt;
Result: = AOldFileName;
//If the file of the prompted file is not present on a disk, we return it, an output
if not FileExists (AOldFileName) then
Exit;
//the file on a disk Means is
//we will search for numbers...
//If the file does not have number
if not IsFNHaveNumber (ExtractFileName (AOldFileName), NewFileName, FileNumber) then
FileNumber: = 2;//we Give number
//Anyway
//in NewFileName now file name without number, in FileNumber number
//it is changed all numbers with found to 99
for i: = FileNumber to 99 do
begin
Result: = ExtractFileDir (AOldFileName) + ' \' + NewFileName + ' - ' + format (' %.2d ', [i]) + ExtractFileExt (AOldFileName);
//If it is that a file on a disk is not present, we return it and an output
if not FileExists (Result) then
Exit;//the Output
end;
//Even if 99th file is it all we return
end;
procedure TForm1.ReportToText (AFileName: String);
//Asks the user where to save a file 
//Prompts a name  AFileName if it is specified
var
OldFileName: String;//old file name
begin
SaveDialog1.FilterIndex: = 1;
SaveDialog1.DefaultExt: = ' txt ';
OldFileName: = SaveDialog1.FileName;//we save old file name
//we prompt file name
SaveDialog1.FileName: = GetNameCandidate (SaveDialog1.FileName, AFileName, ' the Text file ', SaveDialog1.DefaultExt, SaveDialog1.InitialDir);
if SaveDialog1.Execute
then CreateDummyFile (SaveDialog1.FileName)
else SaveDialog1.FileName: = OldFileName;//If pushed a canceling, we recover old file name
end;
procedure TForm1.ReportToExcel (APrj: TRvProject; AFileName: String);
//Asks the user where to save a file 
//Prompts a name  AFileName if it is specified
var
OldFileName: String;//old file name
begin
SaveDialog1.FilterIndex: = 2;
SaveDialog1.DefaultExt: = ' xls';
OldFileName: = SaveDialog1.FileName;//we save old file name
//we prompt file name
SaveDialog1.FileName: = GetNameCandidate (SaveDialog1.FileName, AFileName, ' Otchet Eksel ', SaveDialog1.DefaultExt, SaveDialog1.InitialDir);
if SaveDialog1.Execute
then CreateDummyFile (SaveDialog1.FileName)
else SaveDialog1.FileName: = OldFileName;//If pushed a canceling, we recover old file name
end;

7

Re: File saving. ordinal value

;
Late saw smile)

8

Re: File saving. ordinal value

wrote:

Not that  is passed...

Specially for you in comments there were variants of design of the main course

Arioch wrote:

//: = ' %s' + ' - % 02u'

9

Re: File saving. ordinal value

Yes, it is cool made.
Can I will rewrite something

10

Re: File saving. ordinal value

11

Re: File saving. ordinal value

Xupypx;
These adults, they such strange!
http://docwiki.embarcadero.com/Librarie … y.GetFiles

12

Re: File saving. ordinal value

AS. People here like to complicate! "It is necessary to program as more as possible stupidly" ()
Xupypx;
Counterexample. In a folder three files:
AAAA_. lis
AAAA_A.lis
AAAA_4.lis
We cause function with DevMnem ='AAAA'
It fairly finds three files on a mask - and offers us "AAAA_4.lis" which already is
~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
Counterexample 2. In a folder there are no files, is empty!
We cause function with DevMnem ='AAAA'
It it is fair a horse-radish does not find - and offers us "AAAA_1.lis" though it would be logical   to leave nothing AAAA.lis

13

Re: File saving. ordinal value

wrote:

to You the decision  respected Arioch

is ideal
And still...
It not only not ideal, it AT ALL the DECISION
It purely demo, proof of concept and  an iota is more!
http://www.gunsmoker.ru/2010/05/90.html

14

Re: File saving. ordinal value

Rodriguez wrote:

SaveDialog1.FilterIndex: = 2;
SaveDialog1.DefaultExt: = ' xls';
OldFileName: = SaveDialog1.FileName;//we save old file name
//we prompt file name
SaveDialog1.FileName: = GetNameCandidate (SaveDialog1.FileName, AFileName, ' Otchet Eksel ', SaveDialog1.DefaultExt, SaveDialog1.InitialDir);
if SaveDialog1.Execute
then CreateDummyFile (SaveDialog1.FileName)

What for Create Dummy?
And what for generally with any components-dialogues ?
http://www.delphibasics.ru/PromptForFileName.php

15

Re: File saving. ordinal value

Arioch wrote:

We cause function with DevMnem ='AAAA'
It fairly finds three files on a mask - and offers us "AAAA_4.lis" which already is
It it is fair a horse-radish does not find - and offers us "AAAA_1.lis" though it would be logical   to leave nothing AAAA.lis

Well in my case of functionality it is enough. Files  the automatic machine and just in case. And under the second remark in my case logically as-time always a file with number.
I at first wanted to be confused, made even the list that if there is a file with number that to do a trace from . But then solved it nafig-is necessary.

16

Re: File saving. ordinal value

Arioch wrote:

Xupypx;
These adults, they such strange!
http://docwiki.embarcadero.com/Librarie … y.GetFiles

Thanks did not know about such unit.

17

Re: File saving. ordinal value

Xupypx;
It in places , for example with long ways (in pair thousand characters) badly works
But generally - it is.
And even to it it seems similar functions of obtaining of the file list on a mask walked on all libraries

18

Re: File saving. ordinal value

Xupypx wrote:

Well in my case of functionality it is enough. Files  the automatic machine and just in case. And under the second remark in my case logically as-time always a file with number.
I at first wanted to be confused, made even the list that if there is a file with number that to do a trace from . But then solved it nafig-is necessary.

If at all of us it is automatic also the user it is not asked absolutely not "in what file " - then yes, it is possible and so.
But it is all the same curve - what for you TStringList fill then generally? Then to throw out?
Consider an amount of files in an integer-variable and not  to remember names which are not necessary to you.

19

Re: File saving. ordinal value

Without saying about that TStringList too  different buns and consequently brakes.
Normal TList <string> much faster, as the primitive container.
If Delphi 2010 +