#1 2013-05-05 23:51

Fiorello
Member
Registered: 2008-01-21
Posts: 10

Using the current system date, actual time, Now(), Today

Hi!

I want to rename my files based upon the current system date.

The code sample at http://www.den4b.com/wiki/ReNamer:Scripts:Date_and_Time results the file date.

var
  DateTime: TDateTime;
 
begin
  DateTime := FileTimeModified(FilePath);
  FileName := WideExtractBaseName(FileName) +
    FormatDateTime(' (dd-mmm-yyyy)', DateTime) +
    WideExtractFileExt(FileName);
end.

Test (05-May-2013).mp3

I want to get the current system date. So I disable "DateTime := FileTimeModified(FilePath);" at line 5. But the result is not the current system date, but:

Test (30-Dec-1899).mp3 (Ooops, what's up???)


What do I have to do to get the current date? Doesn't fill DateTime: TDateTime; the DateTime variable with the current date?

Hope somebody can help me...

Fiorello

Offline

#2 2013-05-06 04:11

narayan
Senior Member
Registered: 2009-02-08
Posts: 471

Re: Using the current system date, actual time, Now(), Today

Offline

#3 2013-05-06 07:52

Stefan
Moderator
From: Germany, EU
Registered: 2007-10-23
Posts: 1,161

Re: Using the current system date, actual time, Now(), Today

 

FormatDateTime() needs an DateTime object as second parameter, not just an string called DateTime.

The first parameter of FormatDateTime() is a string in single quotes without parentheses, if you not want those in the result.
(The example you have found use parentheses in the first parameter as literal text for the output only)



See help > http://www.den4b.com/wiki/ReNamer:Pasca … e_and_Time

"function FormatDateTime(const Fmt: String; D: TDateTime): String;"
That means:
function FormatDateTime( wanted format as String ; working on a Date as TDateTime object): resulting in a String object;



See help from where you can get a "TDateTime" object for the second FormatDateTime() parameter, for example:
function FileTimeModified(const FileName: WideString): TDateTime;
function Date: TDateTime;
function Time: TDateTime;
function Now: TDateTime;
function EncodeDate(Year, Month, Day: Word): TDateTime;
+++
all this functions gives you a TDateTime object.



So you can use for example:
(To make it more clear, I would name the var "DateTime" rather "dtDateTime")

BEFORE:
French.txt
AFTER:
French26-Mai-2013.txt
USE:

var
  dtDateTime  :TDateTime;
  strDateTime :String;

begin
  dtDateTime  := Date();
  strDateTime := FormatDateTime('dd-mmm-yyyy', dtDateTime);
  
    FileName := WideExtractBaseName(FileName) +
                strDateTime +
                WideExtractFileExt(FileName);
end.




Or, if you prefer the short form, you can just use the function 'Date()' inside the function 'FormatDateTime()':

begin
    FileName := WideExtractBaseName(FileName) +
                FormatDateTime(  'dd-mmm-yyyy',  Date()  ) +
                WideExtractFileExt(FileName);
end.

You could even use the function Date() without the parenthesis,
but with () it makes more clear where  'Date' comes from.


- - -

To format the output (the new filename) you can add more literal text in single quotes:

BEFORE:
French.txt
AFTER:
French (26-Mai-2013).txt
USE:

begin
    FileName := WideExtractBaseName(FileName) + 
                ' (' + FormatDateTime('dd-mmm-yyyy', Date() ) + ')' +
                WideExtractFileExt(FileName);
end.;

BEFORE:
French.txt
AFTER:
French - 26-Mai-2013.txt
USE:

begin
    FileName := WideExtractBaseName(FileName) + ' - ' +
                FormatDateTime('dd-mmm-yyyy', Date() ) +
                WideExtractFileExt(FileName);
end.

BEFORE:
French.txt
AFTER:
2013.05.26 French.txt
USE:

begin
    FileName := FormatDateTime('yyyy.mm.dd', Date() ) + ' ' +
                WideExtractBaseName(FileName) +
                WideExtractFileExt(FileName);
end.

- - -

You may see that it makes sense to use the long form
to do it step-by-step and that way making the code not that confusing ;-)

BEFORE:
French.txt
AFTER:
French (26-Mai-2013).txt
USE:

var
  dtDateTime  :TDateTime;
  strDateTime :String;

begin
  dtDateTime  := Date();
  strDateTime := FormatDateTime('dd-mmm-yyyy', dtDateTime);
  
    FileName := WideExtractBaseName(FileName) +
                ' (' + strDateTime + ')' +
                WideExtractFileExt(FileName);
end.


Hope that makes sense?


Read the  *WIKI* for HELP + MANUAL + Tips&Tricks.
If ReNamer had helped you, please *DONATE* to Denis or buy a PRO license. (Read *Lite vs Pro*)

Offline

#4 2013-05-06 22:59

Fiorello
Member
Registered: 2008-01-21
Posts: 10

Re: Using the current system date, actual time, Now(), Today

Stefan, many thanks for your great and comprehensive help!!! It's very, very interesting and I am figuring out how to use pascal script for other issues, too. Narayan, many thanks to you, too! -Fiorello

Offline

Board footer

Powered by FluxBB