Script Request - Grab Date Info and Set as Name.

Discuss and share scripts and script files...
Post Reply
SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Script Request - Grab Date Info and Set as Name.

Post by SkyFrontier »

Please, I need a script that could:
1) read creation/modification dates of selected files and add that info as part as the file names, like: "MD_yyyy-MM-dd - HHmmss_CRyyyy-MM-dd - HHmmss_MD_Filename.ext", being "ext" any extension available.

2) after such files' conversion (which will be done by a 3rd party and remove such date attribs) do the opposite, ie, grab such "MD_yyyy-MM-dd - HHmmss_CRyyyy-MM-dd - HHmmss_MD_Filename.ext" info and set their date attribs accordingly.
Thank you!
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

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

Re: Script Request - Grab Date Info and Set as Name.

Post by Stefan »

Instead of renaming the files with the timestamps
i would prefer an separate file holding this meta data:

"Get timestamps"
* get name, creation date and modification date from selected files
* write to an file "__timestamps" into current folder

"Set timestamps"
* load info from an file "__timestamps" in current folder
* set the creation and modification date to $file

Works folder-wise:

Code: Select all


"Get timestamps"
  end(getinfo("CountSelected") < 1), "Please select some files first.";
  ////get name, creation date and modification date from selected files:
  $Records = report("{Name}, {Created}, {Modified}<crlf>",1);
  $Records = "{Name}, {Created}, {Modified}<crlf>" . $Records;
  ////write to an file "__timestamps" into current folder:
  writefile("__timestamps", $Records);
  ////cosmetics:
  status "Get timestamps is done.";
  while($loop<3){beep; beep 1000, 200;beep 1200, 400;incr $loop;wait 500;}
 
"Set timestamps"
  ////load info from an file "__timestamps" in current folder:
  $Records = readfile("__timestamps");
 
  ////For Each Item in Itemlist $Records DO:
  $LOOP=2;
  while(1)
  {
    ////Get next record set:
    $RecSet = gettoken($Records, $LOOP, "<crlf>");
    if ($RecSet==""){break;}

    ////split the record into parts and get 
    ////the file name (and add the current path):
    $file = "<curpath>\" . gettoken($RecSet, 1, ",");

    ////the creation date:
    $cDate = gettoken($RecSet, 2, ",");
      ////set the creation date to $file:
      timestamp c, "$cDate", "$file";

    ////the modification date:
    $mDate = gettoken($RecSet, 3, ",");
      ////set the modification date to $file:
      timestamp m, "$mDate", "$file";

  incr $LOOP;
  }
  ////cosmetics:
  status "Set timestamps is done.";
  while($loop<3){beep; beep 1000, 200;beep 1200, 400;incr $loop;wait 500;}


SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: Script Request - Grab Date Info and Set as Name.

Post by SkyFrontier »

Man.
IT'S AWESOME!!!
YOU ROCK, Stefan! Thank you! (again!!!)

...the beeping thing is turning into a musical career, eh?
Next thing: XY Musical Contest! :D
Thanks, man!
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: Script Request - Grab Date Info and Set as Name.

Post by SkyFrontier »

BUT (having tested it a bit more):
-Can you tweak it to completely ignore the extension, since the aim of this is a bunch of CONVERTED files?
I can't see how to tweak this into code, sorry...
-Is it possible to have a 3rd option to really write that info as specified for a name? That'll be part of another task - setting names to visually tell and sort by dates all of the time, disregarding NAME column.

---
-and YES, I don't need to select again the files inside a folder - the code looks after them itself and set date stamps for those who it finds and simply ignores the ones who it doesn't! It'll only complain if none was found! Great!
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

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

Re: Script Request - Grab Date Info and Set as Name.

Post by Stefan »

SkyFrontier wrote: -Can you tweak it to completely ignore the extension, since the aim of this is a bunch of CONVERTED files?
If the base names without the extension are that different to find the right one?

Example:
file1.txt, cDate, mDate
file2.exe, cDate, mDate
file3.txt, cDate, mDate

than the script could search for "file2" no matter what extension and set the timestamp for that. Should be work, isn't it? :roll:

SkyFrontier wrote: -Is it possible to have a 3rd option to really write that info as specified for a name? That'll be part of another task - setting names to visually tell and sort by dates all of the time, disregarding NAME column.
Test this with some test files.

The function "Rename file with C+M timestamp"
adds the timestamp as " * [C_12345678123456 M_12345678123456].*"

The function "Set timestamp from file name"
needs this above pattern to detect the timestamp to use.

Code: Select all


"Rename file to add C+M timestamp"
  end(getinfo("CountSelected") < 1), "Please select some files first.";
  
  rename b, "* [C_<datec yyyymmddHHNNSS> M_<datem yyyymmddHHNNSS>]";
  beep; beep 1000, 200;beep 1200, 400;
  
  
"Set timestamp from file name"
  end(getinfo("CountSelected") < 1), "Please select some files first.";
  
  $Records = getinfo("SelectedItemsPathNames", "|");
  $LOOP=1;
  while(1){
    $RecSet = gettoken($Records, $LOOP, "|");
    if ($RecSet==""){break;}
    
      $cDate = regexreplace($RecSet, ".+C_(.+) M_(.+)\](\..+)*", "$1");
       $cDate = regexreplace($cDate, "(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)", "$1.$2.$3 $4:$5:$6");
       timestamp c, $cDate, $RecSet;
    
      $mDate = regexreplace($RecSet, ".+C_(.+) M_(.+)\](\..+)*", "$2");
       $mDate = regexreplace($mDate, "(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)", "$1.$2.$3 $4:$5:$6");
       timestamp m, $mDate, $RecSet;
     
     ////delete timestamp from file name:
     //rename r, "\s*\[C_\d\d\d\d\d\d\d\d\d\d\d\d\d\d M_\d\d\d\d\d\d\d\d\d\d\d\d\d\d\] > ",, $RecSet;
   
   incr $LOOP;
  }
  beep; beep 1000, 200;beep 1200, 400;


SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: Script Request - Grab Date Info and Set as Name.

Post by SkyFrontier »

Great, Stefan. Not thoroughly tested but enough to see that it'll cover me better.
But please, a regEx help over here!

Code: Select all

  rename b, "C_<datec yyyy-mmdd_HH^NN%SS>_M_<datem yyyy-mmdd_HH^NN%SS>_*"; rename s, "^/hr"; rename s, "%/min";
This works for the reanming part!

But this...

Code: Select all

      $cDate = regexreplace($RecSet, "*_.+C_(.+) M_(.+)\](\..+)", "$1");
       $cDate = regexreplace($cDate, "(\d\d\d\d)-(\d\d)(\d\d)_(\d\d)hr(\d\d)min(\d\d)", "$1.$2.$3 $4:$5:$6");
       timestamp c, $cDate, $RecSet;

      $mDate = regexreplace($RecSet, "*_.+C_(.+) M_(.+)\](\..+)", "$1"););
       $mDate = regexreplace($mDate, "(\d\d\d\d)-(\d\d)(\d\d)_(\d\d)hr(\d\d)min(\d\d)", "$1.$2.$3 $4:$5:$6");
       timestamp m, $mDate, $RecSet;
although doesn't throws me the "Invalid date" error msg, it doesn't set the proper dates.
Still head scratching, I NEED to learn this stuff a bit more... (you may already noticed that I TRY it sometimes, right?)
Thanks much! 8)

EDIT: Ah! This is a sample of something that'll throw the error msg:

Code: Select all

".+C_(.+) M_(.+)\](\..+)_*"
-it SHOULD work! :(
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

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

Re: Script Request - Grab Date Info and Set as Name.

Post by Stefan »

First, I think it's not RegEx itself but you should take more care about your "Set"- and "Get"-pattern?

"Set"
SS>_M_<dat
"Get"
.+) M_(.+)

Maybe that it was?



Second:
for $mDate you want the second back reference "$2" instead of "$1". Copy&paste mistake.



Third:
for the record. I forgot to add that extracted timestamp has to be valid for your language settings.
Syntax
timestamp [type], [date], [itemlist]
date [optional] must be in a format that the local system can understand;
That means for
$cDate = regexreplace($cDate, "(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)", "$1.$2.$3 $4:$5:$6");
you or an other maybe have to use something like
$cDate = regexreplace($cDate, "(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)", "$3/$2/$1 $4:$5:$6");

to make timestamp() work? Guessing here.
_

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: Script Request - Grab Date Info and Set as Name.

Post by SkyFrontier »

Right.
This did the trick:

Code: Select all

$cDate = regexreplace($cDate, "(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)", "$3/$2/$1 $4:$5:$6");
...but I really can't adapt the code to grab this
rename b, "C_<datec yyyy-mmdd_HH^NN%SS>_M_<datem yyyy-mmdd_HH^NN%SS>_*"; rename s, "^/hr"; rename s, "%/min";
(that outputs C_1996-0819_18hr36min26_M_2009-0819_18hr36min34_keyremapper_setup.exe, for instance) and get this info as base to date stamping.
This

Code: Select all

$cDate = regexreplace($RecSet, ".+C_(.+) M_(.+)\](\..+)*", "$1");
       $cDate = regexreplace($cDate, "(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)", "$3/$2/$1 $4:$5:$6");
should be translated into something like this:

Code: Select all

$cDate = regexreplace($RecSet, "*_.+C_(.+)_M_(.+)\](\..+)", "$1");
       $cDate = regexreplace($cDate, "(\d\d\d\d)-(\d\d)(\d\d)(\d\d)hr(\d\d)min(\d\d)", "$3/$2/$1 $4:$5:$6");
but all attempts results in "invalid date" or... no stamps changes at all despite the "Done!" status I've set instead of the beep - each "beep chant" was sounding like the 'puter laughing defiantly at me... :D
...still head scratching, but... >sigh<
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: Script Request - Grab Date Info and Set as Name.

Post by SkyFrontier »

Almost there...
This works:

Code: Select all

"Rename file to add C+M timestamp"
  end(getinfo("CountSelected") < 1), "Please select some files first.";
  //rename b, "* [C_<datec yyyymmddHHNNSS> M_<datem yyyymmddHHNNSS>]";
  //rename b, "C_<datec yyyy-mmdd_HH^NN%SS>*"; rename s, "^/hr"; rename s, "%/min";
  rename b, "* [C_<datec yyyy-mmdd_HH^NN%SS> M_<datem yyyy-mmdd_HH^NN%SS>]"; rename s, "^/hr"; rename s, "%/min";
  status SUCCESS!;
 
 
"Set timestamp from file name"
  end(getinfo("CountSelected") < 1), "Please select some files first.";
 
  $Records = getinfo("SelectedItemsPathNames", "|");
  $LOOP=1;
  while(1){
    $RecSet = gettoken($Records, $LOOP, "|");
    if ($RecSet==""){break;}
    //$cDate = regexreplace($RecSet, ".+C_(.+) M_(.+)\](\..+)*", "$1");
      $cDate = regexreplace($RecSet, ".+C_(.+) M_(.+)\](\..+)*", "$1");
       $cDate = regexreplace($cDate, "(\d\d\d\d)-(\d\d)(\d\d)_(\d\d)hr(\d\d)min(\d\d)", "$3/$2/$1 $4:$5:$6");
       timestamp c, $cDate, $RecSet;
   
      $mDate = regexreplace($RecSet, ".+C_(.+) M_(.+)\](\..+)*", "$2");
       $mDate = regexreplace($mDate, "(\d\d\d\d)-(\d\d)(\d\d)_(\d\d)hr(\d\d)min(\d\d)", "$3/$2/$1 $4:$5:$6");
       timestamp m, $mDate, $RecSet;
     
     ////delete timestamp from file name:
     //rename r, "\s*\[C_\d\d\d\d\d\d\d\d\d\d\d\d\d\d M_\d\d\d\d\d\d\d\d\d\d\d\d\d\d\] > ",, $RecSet;
   
   incr $LOOP;
  }
  status DONE!;
The thing now is to make this

Code: Select all

keyremapper_setup [C_2001-0829_15hr26min02_M_2009-0819_15hr26min06].txt
turn into this:

Code: Select all

C_2001-0829_15hr26min02_M_2009-0819_15hr26min06_keyremapper_setup.txt
...
:?

EDIT: No way. I've tried all possible combinations of the damn "*._+" sequence, everywhere in the ".+C_(.+) M_(.+)\](\..+)*" part of the code. Help. Please. I'm going nuts with this. :evil:
:)
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

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

Re: Script Request - Grab Date Info and Set as Name.

Post by Stefan »

SkyFrontier wrote: turn into this:

Code: Select all

C_2001-0829_15hr26min02_M_2009-0819_15hr26min06_keyremapper_setup.txt
...

Code: Select all

"Rename file to add C+M timestamp"
  end(getinfo("CountSelected") < 1), "Please select some files first.";
  rename b, "C_"."<datec yyyy-mmdd_HH>"."hr"."<datec NN>"."min"."<datec SS>"
          ."_M_"."<datem yyyy-mmdd_HH>"."hr"."<datem NN>"."min"."<datem SS>_*"; 
  status SUCCESS!;


"Set timestamp from file name"
  end(getinfo("CountSelected") < 1), "Please select some files first.";
  
  $Records = getinfo("SelectedItemsPathNames", "|");
  $LOOP=1;
  while(1){
    $RecSet = gettoken($Records, $LOOP, "|");
    if ($RecSet==""){break;}
      $cDate = regexreplace($RecSet, ".+C_(.+)_M_(.+)_.+", "$1");
       $cDate = regexreplace($cDate, "(\d\d\d\d)-(\d\d)(\d\d)_(\d\d)hr(\d\d)min(\d\d)", "$3/$2/$1 $4:$5:$6");
       timestamp c, $cDate, $RecSet;
   
      $mDate = regexreplace($RecSet, ".+C_(.+)_M_(.+)_.+", "$2");
       $mDate = regexreplace($mDate, "(\d\d\d\d)-(\d\d)(\d\d)_(\d\d)hr(\d\d)min(\d\d)", "$3/$2/$1 $4:$5:$6");
       timestamp m, $mDate, $RecSet;
 
   incr $LOOP;
  }
  status DONE!;
------------

Explanation:

"Rename file to add C+M timestamp"
renames an file
"LESER.txt"
to
C_2010-0820_09hr06min01_M_2009-0112_10hr28min23_LESER.txt

by adding creation and modification timestamp in front of the original file name.

------

"Set timestamp from file name"
gets the full path with "SelectedItemsPathNames" since timestamp() wants this.


$cDate = regexreplace($RecSet, ".+C_(.+)_M_(.+)_.+", "$1");
means:
.+ ==> all signs till an "C_" ==> e.g. "X:\path\folder\"
C_ ==> match "C_"
(.+)_ ==> match all signs till an underscore "_" ==> "2010-0820_09hr06min01" ==> stored in (...)-group 1
M_ ==> match "M_"
(.+)_ ==> match all signs till an underscore "_" ==> "2009-0112_10hr28min23" ==> stored in (...)-group 2
.+ ==> match all signs till the end ==> "LESER.txt"


"$1" returns what is matched and stored in group 1 ==> "2010-0820_09hr06min01"


The second
$mDate = regexreplace($mDate, "(\d\d\d\d)-(\d\d)(\d\d)_(\d\d)hr(\d\d)min(\d\d)", "$3/$2/$1 $4:$5:$6");
matched the digits groups for year, month and day,...

(\d\d\d\d) ==> "2010" ==> backreference group $1
-
(\d\d) ==> "08" ==> backreference group $2
(\d\d) ==> "20" ==> backreference group $3
_
(\d\d) ==> "09" ==> backreference group $4
hr
(\d\d) ==> "06" ==> backreference group $5
min
(\d\d) ==> "01" ==> backreference group $6


and rearrange this matched groups in an new order,
and also adds some typical signs for the timestamp() command ==> "$3/$2/$1 $4:$5:$6"



Sorry, no time to explain it better. An RegEx tutorial and XYplorers debug dialog is everybody friend :wink:

------------


------------


------------sidenote:
I have tried to use this too:

Code: Select all

  rename b, "C_<datec yyyy-mmdd_HH>hr<datec NN>min<datec SS>
          _M_<datem yyyy-mmdd_HH>hr<datem NN>min<datem SS>_*"; 
but this doesn't work while i get:
C_2010-0820_09hr06min20.08.2010 09:27:26 26_M_2009-0112_10hr28min8 26_*
It seams i tricked the parser out?

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: Script Request - Grab Date Info and Set as Name.

Post by SkyFrontier »

(trying to laugh)
You're my hero, Stefan.

Code: Select all

.+C_(.+)_M_(.+)_.+
was the answer, huh? *WITH* explanation (even better!)
I've considered looking into some regEx references (some scripts of yours have some) but I'm so involved in few time-demanding, dead-lined projects that I simply "couldn't" do. The main reason why I refuse to study programming is that: I'm a workaholic, perfectionist and obsessed person. See how many edits each post I do has. And if I make some absurd mistakes now and then is for *I'm trying to get myself a bit more relaxed*, as I'm not simply lazy, ungracious. If I start learning this thing as I'd like, I'd have to do it heart-fully, and currently no time for that.
So once more - THANK YOU!
-have to test the code against selected/unselected/multi-extension files (I do realize that this has some limitations when put in comparison with the "__timestamps" solution from the 1st script and will try to integrate it into this, but not for now. Anyway, thank you also for the didactic part you put in *each and every single script* you post here (as you went yet more explanatory with this one as you probably noticed that I'M TRYING to learn at least the basics of the thing, despite what I said above...).
Feel free to PM me if you believe I can help you on any matter, some day.
Best regards,

SF.
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

Post Reply