Page 1 of 1

Copy w/ Modified Date - Prompt if Folder or multi-item

Posted: 25 Jan 2012 04:54
by avsfan
In a thread in the Wishes forum, I asked for a confirmation when doing a "Copy Here With Last Modified Date" when both files and folders were selected. Don made the (correct) argument that it would be a pain to have so many confirmation dialogs.

So here's my first attempt at scripting for this (and it actually seems to work just great!) I bound it to Ctrl-Shift-D to replace the default command. Thoughts/comments/etc are welcome!

Code: Select all

$numitems = get("countselected",);
  foreach ($item, <get selecteditemspathnames |>) {
    $type = exists($item);
    if ($type==2) { //folder,etc
       $isfolder = 1;
    }
  }
  // if any folders are selected, or if the user has selected multiple items, ask the user if they want to continue
  if (($numitems>1) || ($isfolder==1)) {
     if (confirm("Multiple items and/or folders selected.  Continue?")==1) {
       #166;
     }
  } else {
    #166;
  }

Re: Copy w/ Modified Date - Prompt if Folder or multi-item

Posted: 25 Jan 2012 15:46
by PeterH
Hi avsfan,

one thing I would never do is getting the list of selected items in the foreach-statement. At least I'm not sure if this is resolved for every pass through the loop.
Further I'd replace the <get ...> with get(...), and use some Quotes.

So I would change the beginning to:

Code: Select all

  $numitems = get("countselected",);   // number of sel items
  $selitems = get("SelectedItemsPathNames", "|");   // list of selected items
  foreach ($item, $selitems) {
    ...

Re: Copy w/ Modified Date - Prompt if Folder or multi-item

Posted: 25 Jan 2012 16:08
by avsfan
Excellent! Thanks very much! I originally had saved the selected pathnames to a variable, but then changed it for some reason when cleaning it up... I know better than to do that -- thanks for catching my mistake!

I also had forgotten (having seen it before on the forum) that I could use get(...) instead of <get ...> -- thanks for the reminder on that as well!

Thanks again!

andy

Re: Copy w/ Modified Date - Prompt if Folder or multi-item

Posted: 25 Jan 2012 16:13
by admin
PeterH wrote:one thing I would never do is getting the list of selected items in the foreach-statement. At least I'm not sure if this is resolved for every pass through the loop.
Well, one might think so but it is not resolved for every pass through the loop.

Re: Copy w/ Modified Date - Prompt if Folder or multi-item

Posted: 25 Jan 2012 16:16
by highend

Code: Select all

foreach ($item, <get selecteditemspathnames |>) {
The <get selecteditemspathnames |> is only executed once so you don't have to use the
$selitems = ...

At least if you don't plan to use it anywhere else in the script.

Re: Copy w/ Modified Date - Prompt if Folder or multi-item

Posted: 25 Jan 2012 16:19
by avsfan
admin wrote:
PeterH wrote:one thing I would never do is getting the list of selected items in the foreach-statement. At least I'm not sure if this is resolved for every pass through the loop.
Well, one might think so but it is not resolved for every pass through the loop.
Ah, good to know! Thanks, Don!

andy

Re: Copy w/ Modified Date - Prompt if Folder or multi-item

Posted: 25 Jan 2012 16:39
by TheQwerty
If you only ever use this on the list, as opposed to duplicating tree items, then you can use Report to avoid the for-loop entirely:

Code: Select all

"Safer Copy with Modified Date"
  // Ensure something is selected.
  $numItems = Get('CountSelected');
  End $numItems < 1, 'Nothing selected';          // This could be replaced with Sel '+0'; to select the focused item.

  // Ensure there's not drives in the selection.
  $hasDrive = Report('{Dir ||1}', 1);          // Returns a string of 1s whose length equals number of selected drives.
  End $hasDrive != '', 'Cannot copy drives.';

  // Check for folders
  $hasDir = Report('{Dir 1||}', 1);          // Returns a string of 1s whose length equals number of selected folders.

  // Prompt if more than 1 item is selected or selection contains folders.
  if ($numItems > 1 || $hasDir != '') {
    End 0 == Confirm("Multiple items or folders selected.<br><br>Continue with copy?");
  }

  Focus;          // Ensure List is focused.
  #166;          // File | Duplicate | Copy Here with Last Modified Date
*Excessively commented to aid future readers - not a result of opinion on anyone's abilities.

Re: Copy w/ Modified Date - Prompt if Folder or multi-item

Posted: 25 Jan 2012 18:55
by PeterH
admin wrote:
PeterH wrote:one thing I would never do is getting the list of selected items in the foreach-statement. At least I'm not sure if this is resolved for every pass through the loop.
Well, one might think so but it is not resolved for every pass through the loop.
Good that I said "I'm not sure". And this "I'm not sure" would be reason enough for me not to do it. But if you are sure ... :D

And don't get me wrong: tomorrow I will be unsure again :oops:

Re: Copy w/ Modified Date - Prompt if Folder or multi-item

Posted: 25 Jan 2012 19:06
by avsfan
TheQwerty wrote:If you only ever use this on the list, as opposed to duplicating tree items, then you can use Report to avoid the for-loop entirely:

Code: Select all

"Safer Copy with Modified Date"
  // Ensure something is selected.
  $numItems = Get('CountSelected');
  End $numItems < 1, 'Nothing selected';          // This could be replaced with Sel '+0'; to select the focused item.

  // Ensure there's not drives in the selection.
  $hasDrive = Report('{Dir ||1}', 1);          // Returns a string of 1s whose length equals number of selected drives.
  End $hasDrive != '', 'Cannot copy drives.';

  // Check for folders
  $hasDir = Report('{Dir 1||}', 1);          // Returns a string of 1s whose length equals number of selected folders.

  // Prompt if more than 1 item is selected or selection contains folders.
  if ($numItems > 1 || $hasDir != '') {
    End 0 == Confirm("Multiple items or folders selected.<br><br>Continue with copy?");
  }

  Focus;          // Ensure List is focused.
  #166;          // File | Duplicate | Copy Here with Last Modified Date
*Excessively commented to aid future readers - not a result of opinion on anyone's abilities.
Wow, thanks! I didn't even know about "Report" -- this is great to know! And the comments are also much appreciated for scripting newbies like me!

Thanks again!