Solved

File naming from a DB field

Posted on 2000-02-17
16
145 Views
Last Modified: 2010-04-04
I have a Db of shows, each has upto 5 dates, I need to create a file for each date that has a name relating to that date. i.e. some kind of date-to-string function. And then how do i assign this name to the file in an onclick event?
0
Comment
Question by:SRP
  • 8
  • 5
  • 2
  • +1
16 Comments
 
LVL 6

Expert Comment

by:edey
ID: 2532723
you can use this:

DateTimeToString converts a variable to a string using the specified Format.

procedure DateTimeToString(var NewString: string; const Format:
  string; DateTime: TDateTime);

Description

DateTimeToString converts the date and time value given by DateTime using the format string given by Format into the string variable given by NewString. For Formats supported, refer to the description of the FormatDateTime function.


to convert the date to a string, then use:

Unit

System
AssignFile associates the name of an external file with a file variable.

procedure AssignFile(var F; FileName: string);

and reset/rewrite to create/edit a file with that particular name.


Gl
Mike
0
 
LVL 1

Expert Comment

by:AttarSoftware
ID: 2533126
But of course, you will have to replace the / in the dates...

something like:

  for c := 1 to length( s ) do
    if( s[ c ] = '/' ) then s[ c ] := '_' ;

Should do it :O)

Good luck

Tim.
0
 
LVL 6

Expert Comment

by:edey
ID: 2533348
Actually the format string param lets you (far more flexibly) specify the format of the returned string.

or, you could simply try:

file_name := intToStr(trunc(some_date_time);



GL
Mike
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 1

Expert Comment

by:Phoenix_s
ID: 2535782
This is D4 code

for making a name out of a date-time string... it's kinda windy in the effect there's a lotta **** plunked in one filename, but so far as the date/time seperators go, there's a clue on how to make it pretty and easy.

Yah... it should be a function, but it ain't right now... sue me ;)  hehehehehe
----------------------------------

procedure tform1.genoutputname;
var tempdateseparator : char;
    temptimeseparator : char;
begin
  //copy original date/time seperators into temp file
  //replace with "_" char
  tempdateseparator := dateseparator;
  dateseparator := '_';
  temptimeseparator := timeseparator;
  timeseparator := '_';
  outputname := '';
  outputname := outputname + 'c:\my documents\';
  outputname := outputname + 'aging_to_';
  outputname := outputname + datetostr(runtodate);
  outputname := outputname + '_run_on_'+datetostr(date)+'_'+timetostr(time)+'.xls';
  //revert the date time seperator to the "/"
  dateseparator := tempdateseparator;
  timeseparator := temptimeseparator;
end;
0
 
LVL 6

Expert Comment

by:edey
ID: 2535842
It is not generally considered polite to rephrase previous comments as an answer.  Do you have anything new/different/better then the previous sugestions?


GL
Mike
0
 
LVL 1

Expert Comment

by:Phoenix_s
ID: 2536468
edey:

k fine... it's a comment now   you can smooth your fur back down.

I gave a complete working example which, I might add, I had not seen yet as a comment or answer...
0
 

Author Comment

by:SRP
ID: 2538012
All of the 'comments', seem to answer bits of my problem, but I can't seem to see what will do the job the best.

the dates are in fields and the file needs creating when the button is pressed.

is there an easy way i can just take the date and take out the / (easily), and put it in as the file name. most of the comments seem a bit messy?!!
0
 
LVL 6

Accepted Solution

by:
edey earned 50 total points
ID: 2538589
var
 fileName : string;
begin
//option one
 fileName := intToStr(trunc(date));
//option two
 DateTimeToString(fileName,'dd-mm-yy',date);
//then
 assignFile(some_file_var,fileName);
//use reset/rewrite & write to file here
 closeFile(some_file_var);
end;



GL
Mike
0
 

Author Comment

by:SRP
ID: 2543648
Thats the simple sort of thing I was looking for. (It should work well, yeah?)

I may comment and up the points again if i have trouble with it.

Cheers
SAM

p.s appologies for the late response I am on holiday and have limited access to the internet!!

0
 
LVL 6

Expert Comment

by:edey
ID: 2543909
No problem, glad to be of service


GL
Mike
0
 

Author Comment

by:SRP
ID: 2565137
edey- one thing i couldn't sort out from this was how to get the date from the edit box, can i assign date:=editbox1.text;
to get the date that needs to be the file name from this editbox?
0
 
LVL 6

Expert Comment

by:edey
ID: 2565312
"I have a Db of shows, each has upto 5 dates, I need to create a file for each date that has a name relating to that date. i.e. some kind of date-to-string function. And then how do i assign this name to the file in an onclick event?"

Do you not already have the dates in the db?



GL
Mike
0
 

Author Comment

by:SRP
ID: 2565425
The way it works is i have an entry form and the user fills in all the fields in the table, in editboxes. then posts these in the table.
When the date field is filled in the user can click on a button which creates a file the name being that date.

For instance editbox holds: 27/02/00
button click creates file: 270200.dat

Does that make it a bit clearer?

Cheers
SAM

i can reask this question to give more points if you like!?
0
 
LVL 6

Expert Comment

by:edey
ID: 2565603
You do have a couple of options here:

1)replace the edit with a db aware TDateTimePicker, then the field will always contain a TDateTime (a number) and the above suggestion will work, you can try these:
http://www.torry.webnorth.com/vcl/database/ardcale.zip
http://www.torry.webnorth.com/vcl/shared/calend.zip


2)use a TMaskEdit to ensure the user input will always have the form ##/##/## and use this:
  fileName := date[0]+date[1]+date[3]+date[4]+date[6]+date[7]+'.dat';
Note you could also use an edit, put the above in a try/except block, and give the user an error/warning if they didn't give acceptable input

GL
Mike
0
 

Author Comment

by:SRP
ID: 2566332
Thanks again for that. Very helpful.
I used this method
fileName := date[0]+date[1]+date[3]+date[4]+date[6]+date[7]+'.dat';
as the field had validation on it anyway, without needing maskedit.

Cheers
SAM
0
 
LVL 6

Expert Comment

by:edey
ID: 2566435
Excellent

Gl
Mike
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to convert wav to mp3 in delphi 9 200
Downloading email attachments 2 73
How to build JSON File in Delphi 6 3 33
Dev express lookupcombo 3 27
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question