#1 2013-04-28 06:37

KenK
Member
Registered: 2013-04-28
Posts: 6

Include weekday based on date within file name

Hello All,

Been using ReNamer on a casual basis for the past few months and it's saved a lot of time.  Very nice program.

However, I now have a need for something a bit more advanced and not sure if it's even possible.  I have a few thousand files which are named such as: 20121025 (yyyymmdd).

Would it be possible to rename the file to add the weekday and change the date structure for whatever date is in the file name?  For example:

Original:   20121025
Renamed: Thursday - 25 OCT 2012.

I have already searched the forum, but not seeing anything remotely related to what I'm trying to do.

Thanks very much.

Regards,

Ken

Offline

#2 2013-04-28 09:15

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

Re: Include weekday based on date within file name

Hi Ken, welcome.

I think this is possible to do with ReNamer and an PascalScript.

I guess we can use PascalScript functions from http://www.den4b.com/wiki/ReNamer:Pasca … e_and_Time

to format to dddd dd mmm yyyy and rename the files.


I think you should provide a few examples of your real file names so somebody can aid you by coding such script.


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

#3 2013-04-28 10:25

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

Re: Include weekday based on date within file name

I have taken an look onto the wiki and mocked-up an example POC script:

Thanks to the wiki maintainers for providing the wiki and to Denis for providing the PascalScript functions! :thumpsup:

PROOF OF CONCEPT script only. Not really productive working. Shows a message box only.

var
 sPartBefore, sDateOrg, sPartAfter, sDateForm :String;
 dtDateOrg :TDateTime;
 iYear, iMonth, iDay :Integer;
 dddd, rest :String;
 
begin
  // Add your code here
  
  //extract date string from file name first (e.g. 20121025):
  //NOTE: normally you have to use an regex here to match 
  //parts before and after and the date string, if you want to reuse them
  //sPartBefore := regex on FileName 
  sDateOrg      := '20121025';
  //sPartAfter  := regex on FileName 
  
  //extract the date parts for EncodeDate() function (2012,10,25):
  iYear  := StrToInt(ReplaceRegEx(sDateOrg, '(\d{4})(\d{2})(\d{2})','$1',false,true));
  iMonth := StrToInt(ReplaceRegEx(sDateOrg, '(\d{4})(\d{2})(\d{2})','$2',false,true));
  iDay   := StrToInt(ReplaceRegEx(sDateOrg, '(\d{4})(\d{2})(\d{2})','$3',false,true));
    
  //format the date string
  dtDateOrg := EncodeDate(iYear, iMonth, iDay);
  sDateForm := FormatDateTime('dddd dd mmm yyyy', dtDateOrg);
  
  //some cosmetics for KenK:
  //Split date string into parts 'dddd' and the rest 'dd mmm yyyy' 
  //for inserting an hyphen between 'dddd' and the 'rest' and upper case the rest to get 'OCT'
  dddd := ReplaceRegEx(sDateForm, '(\w+\b)(.+)','$1',false,true);
  rest := ReplaceRegEx(sDateForm, '(\w+\b)(.+)','$2',false,true);  
  rest := WideUpperCase(rest);
  
  //rename the file (here as dummy example only)
  ShowMessage(sDateOrg + #10#13 + dddd + ' - ' + rest);
end.

Example Result (here with german locale):

---------------------------
        ReNamer
---------------------------
      20121025
     Donnerstag -  25 OKT 2012
---------------------------
     OK   
---------------------------

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-04-28 12:30

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

Re: Include weekday based on date within file name

Stefan,

Your script works fine for English locale also.

BTW when the script is compiled, it gives the following message:
---------------------------
Information
---------------------------
Compiled successfully!

[Line 0] Hint: Variable 'FILEPATH' never used
[Line 0] Hint: Variable 'FILENAME' never used
[Line 2] Hint: Variable 'SPARTBEFORE' never used
[Line 2] Hint: Variable 'SPARTAFTER' never used
---------------------------
So I guess they should be removed to clean up the script. (For future use of others).

Offline

#5 2013-04-28 14:09

KenK
Member
Registered: 2013-04-28
Posts: 6

Re: Include weekday based on date within file name

Hello Stefan,

Many thanks for the quick replies and example script.

I have tried it out and I can get it to display a window showing what I wish the file names to become.  As per the example result you provided.  But how can I get it to actually function on the "real" files?  I added a few files to test, and get a message that files were successfully renamed, but nothing at all happens to the files themselves.  The file names have remained the same upon exiting from ReNamer.  I admit that I know nothing about Pascal Scripts and understand them even less.  I'm sure that there is something that I need to be changing on my side to get it working properly.

For instance, in the script you posted, there is this line at the end: ShowMessage(sDateOrg + #10#13 + dddd + ' - ' + rest);
Does this need to be changed so the script is changing the actual file rather than displaying a message?

The full file names would be like these examples:

20120502.rdc
20121025.rdc
20121224.rdc
20130403.rdc
20130110.rdc

Just that date as a name, plus the extension.

Thank you again for your efforts and I apologize for being such a newbie at all of this.

Regards,

Ken

Offline

#6 2013-04-28 16:33

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

Re: Include weekday based on date within file name

KenK wrote:

For instance, in the script you posted, there is this line at the end: ShowMessage(sDateOrg + #10#13 + dddd + ' - ' + rest);
Does this need to be changed so the script is changing the actual file rather than displaying a message?

Right.
Instead of ShowMessage() use "FileName :=  foobar;" to rename to foobar.

Or, for your case, we would use "FileName :=  dddd + ' - ' + rest;" to rename by what the script had spat out.

But also we will need the original extension, here for example hard-coded: "FileName :=  dddd + ' - ' + rest + '.rdc';"


And, to be flexible on the current file name we working on, we have to extract the current date string and the current extension (no matter for your examples, but...)
That is why we need to know how your complete file name looks like.  Therefore you have provided this examples:

The full file names would be like these examples:
20120502.rdc
20121025.rdc
20121224.rdc
20130403.rdc
20130110.rdc
Just that date as a name, plus the extension.

So we add this code to the script:

  sBaseName := WideExtractBaseName(FileName);
  //sPartBefore := regex on sBaseName
  sDateOrg      := sBaseName;
  //sPartAfter  := regex on sBaseName

and

  FileName := dddd + ' - ' + rest + WideExtractFileExt(FileName);


(Yes, FileName is the var for the current input and also used for the output to provide the new filename)

So our script could look like this...



FROM:
20120502.rdc
20121025.rdc
20121224.rdc
20130403.rdc
20130110.rdc

TO:
Mittwoch -  02 MAI 2012.rdc
Donnerstag -  25 OKT 2012.rdc
Montag -  24 DEZ 2012.rdc
Mittwoch -  03 APR 2013.rdc
Donnerstag -  10 JAN 2013.rd

USE:
SCRIPT Version A
(For digits-only file names with an date format YYYYMMDD like '20121025.ext' only!!!
Will give errors on other date formats like DDMMYYYY, on non-valid dates and on alphanumeric file names)
(Exception: Invalid argument to date encode)

var
 sBaseName, sPartBefore, sDateOrg, sPartAfter, sDateForm :String;
 dtDateOrg :TDateTime;
 iYear, iMonth, iDay :Integer;
 dddd, rest :String;
 
begin
  // Add your code here
  
  //extract date string from file name first (e.g. 20121025):
  //NOTE: normally you have to use an regex here to match 
  //parts before and after and the date string, if you want to reuse them
  sBaseName := WideExtractBaseName(FileName);
  //sPartBefore := regex on sBaseName to get the part before the date string
  sDateOrg      := sBaseName;
  //sPartAfter  := regex on sBaseName to get the part after the date
  
  //extract the date parts for EncodeDate() function (2012,10,25):
  iYear  := StrToInt(ReplaceRegEx(sDateOrg, '(\d{4})(\d{2})(\d{2})','$1',false,true));
  iMonth := StrToInt(ReplaceRegEx(sDateOrg, '(\d{4})(\d{2})(\d{2})','$2',false,true));
  iDay   := StrToInt(ReplaceRegEx(sDateOrg, '(\d{4})(\d{2})(\d{2})','$3',false,true));
    
  //format the date string
  dtDateOrg := EncodeDate(iYear, iMonth, iDay);
  sDateForm := FormatDateTime('dddd dd mmm yyyy', dtDateOrg);
  
  //some cosmetics for KenK:
  //Split date string into parts 'dddd' and the rest 'dd mmm yyyy' 
  //for inserting an hyphen between 'dddd' and the 'rest' and upper case the rest to get 'OCT'
  dddd := ReplaceRegEx(sDateForm, '(\w+\b)(.+)','$1',false,true);
  rest := ReplaceRegEx(sDateForm, '(\w+\b)(.+)','$2',false,true);  
  rest := WideUpperCase(rest);
  
  //rename the file (here as dummy example only)
  //ShowMessage(sDateOrg + #10#13 + dddd + ' - ' + rest);
  FileName := dddd + ' - ' + rest + WideExtractFileExt(FileName);
end.

See our wiki for how to use ReNamer PascalScript rule > http://www.den4b.com/wiki/ReNamer:Rules:PascalScript



- - -

EDIT 1 - to modify the script to match trailing and leading parts around numbers also
EDIT 2 - to modify the script and the used regex to handle leading and trailing non-digits parts better with error detection


(e.g. '20121025.ext' or 'abc20120104def.ext' or '20121025abc.ext')


FROM:
abcdefg.ext
20121025.txt
abc20120104def.ext
A20121025.txt
20121025 AB.txt
ABC 20121025.txt
20121025ZYX.txt
ZYX 20121025XYZ.txt
hijklmnop.ext

(NOTE the format of the original date YYYYMMDD like 20121025 !!!
You have to modify the script for other date formats like e.g. DDMMYY !!!)

TO:
abcdefg.ext
Donnerstag -  25 OKT 2012.txt
abcMittwoch -  04 JAN 2012def.ext
ADonnerstag -  25 OKT 2012.txt
Donnerstag -  25 OKT 2012 AB.txt
ABC Donnerstag -  25 OKT 2012.txt
Donnerstag -  25 OKT 2012ZYX.txt
ZYX Donnerstag -  25 OKT 2012XYZ.txt
hijklmnop.ext

USE:

SCRIPT Version B
(this code will STILL give error message and stop working on non-valid date extracted or on other date formating than YYYYMMDD)
(Exception: Invalid argument to date encode)

var
 sBaseName, sPartBefore, sDateOrg, sPartAfter, sDateNew :WideString;
 sMyDateFormat, sMyDateRegex, sDDDD, sRest :WideString;
 dtDateOrg :TDateTime;
 iYear, iMonth, iDay :Integer;
 
begin
  // Add your code here
  //==== USER SETTINGS ======================================
  //format for how to format the date string:
  //(see http://www.den4b.com/wiki/ReNamer:Date_and_Time_format)
  sMyDateFormat := 'dddd dd mmm yyyy';
  
  //regex used to split the extracted date string into three parts for EncodeDate():
  //here e.d. for YYYYMMDD like 20121025:
  sMyDateRegex := '(\d{4})(\d{2})(\d{2})';
  //NOTE: if you change the regex you also have to exchange the order of iYear, iMonth and iDay or the $1,$2,$3 also.

  //==== THE CODE =========================================== 
  //extract date string from file name first:
  sBaseName   := WideExtractBaseName(FileName);

  //handle if there are leading alpha parts or not.
  //(e.g. '20121025.ext' or 'abc20120104def.ext' or '20121025abc.ext'):
  if IsWideCharAlpha(FileName[1]) then
  begin
   sPartBefore := ReplaceRegEx(sBaseName, '(\D+)(\d+)(\D*)','$1',false,true);
   sDateOrg    := ReplaceRegEx(sBaseName, '(\D+)(\d+)(\D*)','$2',false,true);
   sPartAfter  := ReplaceRegEx(sBaseName, '(\D+)(\d+)(\D*)','$3',false,true);
  end
  else
  begin
   sPartBefore := '';
   sDateOrg    := ReplaceRegEx(sBaseName, '(\d+)(\D+)','$1',false,true);
   sPartAfter  := ReplaceRegEx(sBaseName, '(\d+)(\D+)','$2',false,true);
  end;  
  //handle non-matching regex results: 
    if(sPartBefore=sBaseName) then sPartBefore:='';
    if(sPartAfter=sBaseName)  then sPartAfter :='';
  
  //proceed renaming only if a digit could be extracted:
  if IsWideCharDigit(sDateOrg[1]) then
  begin
    //Note: sDateOrg HAVE TO BE in yyyymmdd format!!! for *this* used regex 
    //extract the date parts for EncodeDate() function (2012,10,25):
    iYear  := StrToInt(ReplaceRegEx(sDateOrg, sMyDateRegex, '$1', false,true));
    iMonth := StrToInt(ReplaceRegEx(sDateOrg, sMyDateRegex, '$2', false,true));
    iDay   := StrToInt(ReplaceRegEx(sDateOrg, sMyDateRegex, '$3', false,true));
    
    //format the date string
    dtDateOrg := EncodeDate(iYear, iMonth, iDay);
    sDateNew  := FormatDateTime(sMyDateFormat, dtDateOrg);
  
    //some cosmetics for KenK:
    //Split date string into parts 'dddd' and the rest 'dd mmm yyyy' 
    //for inserting an hyphen between 'dddd' and the 'rest' and upper case the rest to get 'OCT'
    sDDDD := ReplaceRegEx(sDateNew, '(\w+\b)(.+)','$1',false,true);
    sRest := ReplaceRegEx(sDateNew, '(\w+\b)(.+)','$2',false,true);  
    sRest := WideUpperCase(sRest);
  
    //rename the file:
    FileName := sPartBefore + sDDDD + ' - ' + sRest + sPartAfter + WideExtractFileExt(FileName);
  end;

  FilePath:='dummy only to prevent the hint dialog';
end.

Last edited by Stefan (2013-04-29 12:42)


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

#7 2013-04-28 17:37

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

Re: Include weekday based on date within file name

Hi narayan,

many thanks for your feedback!


narayan wrote:

Stefan,

Your script works fine for English locale also.

BTW when the script is compiled, it gives the following message:
---------------------------
Information
---------------------------
Compiled successfully!

[Line 0] Hint: Variable 'FILEPATH' never used
[Line 0] Hint: Variable 'FILENAME' never used
[Line 2] Hint: Variable 'SPARTBEFORE' never used
[Line 2] Hint: Variable 'SPARTAFTER' never used
---------------------------
So I guess they should be removed to clean up the script. (For future use of others).

Please let me explain;

One would see this message, which is a hint only anyway, only inside the PascalScript modifying dialog on pressing at [Try to compile].
FilePath and FileName are default "build-in" PascalScript variables of ReNamer and there is always that hint if that vars are not used.
The both sPart vars are defined by me but not used yet in the script hence the kindly hint of ReNamer to remind me about them.
They both are meant as reference for users having file names like "Before 20121025 After.ext" where we have to catch the before and after part also to reuse them in the output.


I 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

#8 2013-04-28 19:17

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

Re: Include weekday based on date within file name

Hi Stefan,

Actually I thought that if a variable is declared, but if none of the branches/loops make any use of that variable, then the warning is issued.
So I didn't expect that warning during compilation.

Offline

#9 2013-04-29 06:12

KenK
Member
Registered: 2013-04-28
Posts: 6

Re: Include weekday based on date within file name

Hello Stefan,

That works perfectly.  Just brilliant.  Thanks so much.

My next request is going beyond the intent of my original post, but is it also possible to change the file create or modify date to match the date of the actual file name?  This would be beneficial for the actual sorting within Explorer.  I do prefer that the actual weekday is first within the file name, but during sorting in English language Windows, all the dates with Friday appear first, followed by Monday, and so on.

Thank you again Stefan!

Regards,

Ken

Offline

#10 2013-04-29 08:58

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

Re: Include weekday based on date within file name

Hi Ken,

KenK wrote:

Hello Stefan,
That works perfectly.  Just brilliant.  Thanks so much.

You are welcome. Thanks for feedback!


is it also possible to change the file create or modify date to match the date of the actual file name?

Please open always new threads for new questions of search the forum for related threads.

I have checked our wiki > ReNamer for 'timestamp' and 'date' keywords and found:
http://www.den4b.com/wiki/ReNamer:Pasca … #File_Time

function SetFileTimeCreated(const FileName: WideString; const DateTime: TDateTime): Boolean;     Sets creation time for the specified file.
function SetFileTimeModified(const FileName: WideString; const DateTime: TDateTime): Boolean;     Sets last modified time for the specified file.


Then I have googled for SetFileTimeCreated and found f.ex.:
Index    » Help and Support    » Folder Date and Time Stamps
http://www.den4b.com/forum/viewtopic.php?id=341
(there may be others also)

Right now I don't have the time to provide you an out-of-the-box solution,
but if you can't figure it out yourself,... just ask later again. Or someone others may aid you *hinthint*

.


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

Board footer

Powered by FluxBB