• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 160
  • Last Modified:

File naming from a DB field

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
SRP
Asked:
SRP
  • 8
  • 5
  • 2
  • +1
1 Solution
 
edeyCommented:
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
 
AttarSoftwareCommented:
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
 
edeyCommented:
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Phoenix_sCommented:
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
 
edeyCommented:
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
 
Phoenix_sCommented:
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
 
SRPAuthor Commented:
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
 
edeyCommented:
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
 
SRPAuthor Commented:
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
 
edeyCommented:
No problem, glad to be of service


GL
Mike
0
 
SRPAuthor Commented:
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
 
edeyCommented:
"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
 
SRPAuthor Commented:
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
 
edeyCommented:
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
 
SRPAuthor Commented:
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
 
edeyCommented:
Excellent

Gl
Mike
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 8
  • 5
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now