Solved

File naming from a DB field

Posted on 2000-02-17
16
154 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Question has a verified solution.

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

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

739 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