Script with foreach is deselecting my selection

Discuss and share scripts and script files...
Post Reply
hermhart
Posts: 213
Joined: 13 Jan 2015 18:41

Script with foreach is deselecting my selection

Post by hermhart »

Hello, I'm wondering if someone may have an idea about an issue I'm having. I have a script that does a copyto of files in selected directories to another location. But within this script I have it call another script that deletes all the sub-directories of any selected directories first. Issue is, after it runs this script that deletes any sub-directories, it deselects my originally selected directories. I need the original directories that were selected to remain selected for the copyto portion of my script.

The called script that removes the sub-directories and deselects my original directories is as follows:

Code: Select all

msg "Remove all Sub-Directories and their files of selected Folder(s)?", 1;

    foreach($folder, "<get SelectedItemsNames |>") {
        if (exists("<curpath>\$folder") != 2) { continue; }

    $m = report("{Modified yyyy-mm-dd hh:nn:ss}", "<curpath>\$folder");
    $c = report("{Created yyyy-mm-dd hh:nn:ss}", "<curpath>\$folder");
    $a = report("{Accessed yyyy-mm-dd hh:nn:ss}", "<curpath>\$folder");

    setting "BackgroundFileOps", 0;
    $foldersRoot = listfolder("<curpath>\$folder", , 2);
    if (listfolder("<curpath>\$folder", , 2) != "") {
    delete 1, 0, $foldersRoot;

    timestamp m, $m, "<curpath>\$folder";
    timestamp c, $c, "<curpath>\$folder";
    timestamp a, $a, "<curpath>\$folder";
   }
   }
    status Complete!;
Thank you for the help.

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

Re: Script with foreach is deselecting my selection

Post by highend »

Why do you need the selection at all (for the second script)?

When your first script is invoked, let it store the selection in a permanent variable and let the second
script run over that one (and if you like, remove the permanent variable afterwards from the second script)

Apart from that I see nothing obvious that would do any deselection, you'll need to post both scripts to
look into that issue...

A few notes regarding your script, though...

Code: Select all

    $foldersRoot = listfolder("<curpath>\$folder", , 2);
    if (listfolder("<curpath>\$folder", , 2) != "") {
Why do you do that two times? The if can check $foldersRoot...

Code: Select all

    $m = report("{Modified yyyy-mm-dd hh:nn:ss}", "<curpath>\$folder");
    $c = report("{Created yyyy-mm-dd hh:nn:ss}", "<curpath>\$folder");
    $a = report("{Accessed yyyy-mm-dd hh:nn:ss}", "<curpath>\$folder");
Just use one report for all dates and when you timestamp the folder, get
each date via gettoken() from the variable you've stored the report in

Code: Select all

setting "BackgroundFileOps", 0;
Do it before you start the loop, instead all over again

You're wasting time doing things multiple times inside a loop...
One of my scripts helped you out? Please donate via Paypal

hermhart
Posts: 213
Joined: 13 Jan 2015 18:41

Re: Script with foreach is deselecting my selection

Post by hermhart »

Hopefully I've simplified this. I'm just working on copying the files within the selected folders to my destination, then deleting the folder. I'm hoping that this will only look at files immediately below the selected directory(ies), which it looks like listfolder should do since it says it is non-recursive.

Question is, I put in a pattern for the listfolder, and if it doesn't find a file that matches that, then it seems to copy the entire folder, sub-directories included, to the destination. Shouldn't it just skip everything then and delete the selected directory(ies)?

Code: Select all

$destination = inputfolder("C:\Exports", "Choose Repository to move files to:");

 msg "Move all files to $destination?", 1;

    setting "BackgroundFileOps", 0;
    foreach($folder, "<get SelectedItemsPathNames |>") {
        $files = listfolder($folder, "*.prt", 1, "|");

        status Copying files from $folder..., , "progress";
        copyto "$destination", "$files", , 1, 2, 3, , , 1, , , 1;
        delete 1, 0, "$folder";
    }

    status "Complete!";

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

Re: Script with foreach is deselecting my selection

Post by highend »

Why don't you check that condition then?

Code: Select all

    foreach($folder, "<get SelectedItemsPathNames |>") {
        $files = listfolder($folder, "*.prt", 1, "|");

        if ($files) {
            status Copying files from $folder..., , "progress";
            copyto $destination, $files, , 1, 2, 3, , , 1, , , 1;
        }
        delete 1, 0, $folder;
    }
One of my scripts helped you out? Please donate via Paypal

hermhart
Posts: 213
Joined: 13 Jan 2015 18:41

Re: Script with foreach is deselecting my selection

Post by hermhart »

highend,

Thank you very much for the help. That does the trick that I need. :appl:

hermhart
Posts: 213
Joined: 13 Jan 2015 18:41

Re: Script with foreach is deselecting my selection

Post by hermhart »

Could I ask one more thing relating to this? Would you be able to add the option so that if I was to pick actual files instead of folders that it would use the copyto to my destination? So it would either be working with files or be working with folders, it would never be both.

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

Re: Script with foreach is deselecting my selection

Post by highend »

This would probably work (but it assumes that all selected files are of type "prt" if the first selected item has this extension)...

Code: Select all

    $destination = inputfolder("C:\Exports", "Choose Repository to move files to:");
    msg "Move all files to $destination?", 1;

    setting "BackgroundFileOps", 0;
    $selection = "<get SelectedItemsPathNames |>";
    $firstItem = gettoken($selection, 1, "|");
    if (exists($firstItem) == 1 && gpc($firstItem, "ext") == "prt") {
        status "Copying files from " . gpc($firstItem, "path") . "...", , "progress";
        copyto $destination, $selection, , 1, 2, 3, , , 1, , , 1;
        delete 1, 0, $selection;
    } elseif (exists($firstItem) == 2) {
        foreach($item, $selection) {
            $files = listfolder($item, "*.prt", 1, "|");

            if ($files) {
                status "Copying files from $folder...", , "progress";
                copyto $destination, $files, , 1, 2, 3, , , 1, , , 1;
            }
            delete 1, 0, $item;
        }
    }
    status "Complete!";
One of my scripts helped you out? Please donate via Paypal

hermhart
Posts: 213
Joined: 13 Jan 2015 18:41

Re: Script with foreach is deselecting my selection

Post by hermhart »

highend,

Amazing! That works beautifully. Thank you so much for your time and your help. I really appreciate it.

Post Reply