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

Discuss and share scripts and script files...
Post Reply
avsfan
Posts: 554
Joined: 29 Jun 2006 09:00
Location: Fort Collins, Colorado

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

Post 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;
  }

PeterH
Posts: 2827
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

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

Post 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) {
    ...

avsfan
Posts: 554
Joined: 29 Jun 2006 09:00
Location: Fort Collins, Colorado

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

Post 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

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

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

Post 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.

highend
Posts: 14940
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

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

Post 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.
One of my scripts helped you out? Please donate via Paypal

avsfan
Posts: 554
Joined: 29 Jun 2006 09:00
Location: Fort Collins, Colorado

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

Post 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

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

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

Post 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.

PeterH
Posts: 2827
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

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

Post 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:

avsfan
Posts: 554
Joined: 29 Jun 2006 09:00
Location: Fort Collins, Colorado

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

Post 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!

Post Reply