Script to grab date from file name and edit timestamp...

Discuss and share scripts and script files...
Post Reply
burrito9984
Posts: 4
Joined: 20 Jun 2011 19:42

Script to grab date from file name and edit timestamp...

Post by burrito9984 »

I know there is a similar thread already, but I didn't want to hijack his thread even though it is a similar question.

I have a bunch of folders, that have the year in (xxxx) format at the end of the filename. I have tried for the last hour or so modifying the current script, but cannot get it to read the date, any help? :)

Thanks in advance!
-Erik

CodeLobster

Re: Script to grab date from file name and edit timestamp...

Post by CodeLobster »

read the current date or each date stated in each folder?

you may want to look after formatdate(, "hh:nn:ss dd-mm-yyyy"); or substr respectively.

post your code or relevant parts of it describing your problem if this is not of help.

Stefan
Posts: 1360
Joined: 18 Nov 2008 21:47
Location: Europe

Re: Script to grab date from file name and edit timestamp...

Post by Stefan »

Hi Erik, welcome to the community!

Please describe in other words what you want:
- you have folders with an year,... and what next?

Show us a few real examples "before" and "after".

burrito9984
Posts: 4
Joined: 20 Jun 2011 19:42

Re: Script to grab date from file name and edit timestamp...

Post by burrito9984 »

First off, thanks for the prompt and willing replies!

Second, I have read this post and tried to modify it to work, but I'm not sure how to change it to use the ('s as the starting point.

I have a TON of movies ripped to my server, I have them all in separate folders, with the folder name in this format "Title (year made)". I would like the program to go through and edit all the timestamps of the folders to match the date so that when I sort by date, it sorts them by year they were made.

Thanks again for any/all help!
-Erik

Code: Select all

Before
Name           Date modified
Crash (2004)   6/20/2011

After
Name           Date modified
Crash (2004)   1/1/2004

Stefan
Posts: 1360
Joined: 18 Nov 2008 21:47
Location: Europe

Re: Script to grab date from file name and edit timestamp...

Post by Stefan »

Is it enough to provide you the needed regex?

Test with test files first, or make an backup first.

code fragment for your request:

Code: Select all

$file = "Crash (2004)";

   //regex to match one-or-more of any sign, till an space, following an (, following 4 digits, following an )
   $RegExFind = "^.+ \((\d\d\d\d)\)$";    //or "^.+ \((\d{4})\)$"
   // that's:     " ^    .+    \(     (\d\d\d\d)    \)  $ "

   //replace with what is matched by the pattern inside the parenthesis ()
   $RegExReplace = "$1";

   //execute the regex:
   $date = regexreplace($file, $RegExFind, $RegExReplace);
   //check if it works
   msg "Debug:  #$date#",1;

  //add day and month as you need for your local settings:
  //timestamp [type], [date], [itemlist]
  //type [optional] c|m|a or any combinations in any order (cm, am, cma, ac...); defaults to cma 
  //date [optional] must be in a format that the local system can understand 
  msg "Debug2: 1/1/$date",1;
  timestamp mc, "1/1/$date", $file;

The rest like the code in that other thread > http://www.xyplorer.com/xyfc/viewtopic. ... 924#p47924
or improved as you need... just ask.

burrito9984
Posts: 4
Joined: 20 Jun 2011 19:42

Re: Script to grab date from file name and edit timestamp...

Post by burrito9984 »

Stefan wrote:Is it enough to provide you the needed regex?
Yup, plenty good, I created a test share on my nas and copied a couple of movies over for initial trial, and works wonders! Below if the complete code in case someone else needs it. I left all the comments in for the newbs (including myself) :)

Code: Select all

"Set date from filename"
   //collect the selected files into var $fileslist:   
   $filelist = getinfo('SelectedItemsPathNames', '|');
   //initialize loop-counter var:
   $count=1;
   //start an infinity loop:
   while(1){
      //put one file after the other into var $file:
      $file=gettoken($filelist,$count,"|");
      //if var $file is empty break the script:
      if("$file"==""){break;}
     
   //regex to match one-or-more of any sign, till an space, following an (, following 4 digits, following an )
   $RegExFind = "^.+ \((\d\d\d\d)\)$";    //or "^.+ \((\d{4})\)$"
   // that's:     " ^    .+    \(     (\d\d\d\d)    \)  $ "

   //replace with what is matched by the pattern inside the parenthesis ()
   $RegExReplace = "$1";

   //execute the regex:
   $date = regexreplace($file, $RegExFind, $RegExReplace);

  //add day and month as you need for your local settings:
  //timestamp [type], [date], [itemlist]
  //type [optional] c|m|a or any combinations in any order (cm, am, cma, ac...); defaults to cma 
  //date [optional] must be in a format that the local system can understand 
  timestamp mc, "1/1/$date", $file;


      $count++;}
   status "Done!",,ready;
   beep 800,100; beep 400,100; beep 600,100;
Thanks again!
-Erik

admin
Site Admin
Posts: 65338
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: Script to grab date from file name and edit timestamp...

Post by admin »

Note that this syntax construct is outdated:

Code: Select all

//start an infinity loop:
   while(1){
      //put one file after the other into var $file:
      $file=gettoken($filelist,$count,"|");
Since v9.90.0500 - 2011-02-21 12:00 XY supports foreach loops which makes this kind of stuff much easier to write.

Stefan
Posts: 1360
Joined: 18 Nov 2008 21:47
Location: Europe

Re: Script to grab date from file name and edit timestamp...

Post by Stefan »

admin wrote:Note that this syntax construct is outdated:

Code: Select all

//start an infinity loop:
   while(1){
      //put one file after the other into var $file:
      $file=gettoken($filelist,$count,"|");
Since v9.90.0500 - 2011-02-21 12:00 XY supports foreach loops which makes this kind of stuff much easier to write.
OK, i hear it :P

Code: Select all

"Set date from filename"

    //collect the selected files into var $fileslist:   
    $filelist = get('SelectedItemsPathNames', '|');
    
    //foreach($variable, listoftokens, [separator=|]) 
    foreach( $file, $filelist) {
         
          //regex to match one-or-more of any sign, till an space, following an (, following 4 digits, following an )
          $RegExFind = "^.+ \((\d\d\d\d)\)$";    //or "^.+ \((\d{4})\)$"
          // that's:     " ^    .+    \(     (\d\d\d\d)    \)  $ "
          
          //replace with what is matched by the pattern inside the parenthesis ()
          $RegExReplace = "$1";
          
          //execute the regex:
          $date = regexreplace($file, $RegExFind, $RegExReplace);
          
          //add day and month as you need for your local settings:
          //timestamp [type], [date], [itemlist]
          //type [optional] c|m|a or any combinations in any order (cm, am, cma, ac...); defaults to cma
          //date [optional] must be in a format that the local system can understand
          timestamp mc, "1/1/$date", $file;
        
    }
   status "Done!",,ready;
   beep 800,100; beep 400,100; beep 600,100;


or in short

Code: Select all

"Set date from filename"
    foreach( $file, get('SelectedItemsPathNames', '|') ) {
          $YEAR = regexreplace($file, "^.+ \((\d\d\d\d)\)$", "$1");
          timestamp mc, "1/1/$YEAR", $file;
    }
   status "Renaming done!",,ready;   beep 800,100; beep 400,100; beep 600,100;

- - -

1.)
Please note that the command getinfo() is officically renamed to get() long ago

2.)
I see that that is not needed anymore:

Code: Select all

    //if var $file is empty break the script:
      if("$file"==""){break;}
because of:
Help wrote:get("SelectedItemsPathNames");
Note that before v9.40.0007 the separator was actually a delimiter (i.e. it was returned after each item, not between the items.

burrito9984
Posts: 4
Joined: 20 Jun 2011 19:42

Re: Script to grab date from file name and edit timestamp...

Post by burrito9984 »

Stefan wrote: or in short

Code: Select all

"Set date from filename"
    foreach( $file, get('SelectedItemsPathNames', '|') ) {
          $YEAR = regexreplace($file, "^.+ \((\d\d\d\d)\)$", "$1");
          timestamp mc, "1/1/$YEAR", $file;
    }
   status "Renaming done!",,ready;   beep 800,100; beep 400,100; beep 600,100;
That is REALLY short, and works fine, thanks again!

-Erik

Post Reply