Foreach selected folder, copy, rename and move contents

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

Foreach selected folder, copy, rename and move contents

Post by hermhart »

Hello, I am hoping someone could help me out by writing a script. It's a little crazy, but it would need to:

For each selected folder, copy the latest modified file in that folder to (later determined folder) but also rename that one file removing six characters before the first underscore ("_") including itself and anything after it up to the extension.
Then move the contents of that folder to (later determined folder), overwriting anything there.

Thank you to anyone who can help with this. I might be able to figure out some of it, but I know the renaming portion of it would completely baffle me.

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

Re: Foreach selected folder, copy, rename and move contents

Post by highend »

Regarding the new file name...

Examples (original -> modified)?

folderreport is without the recursive flag...

Code: Select all

    setting "BackgroundFileOps", 0;
    foreach($folder, "<get SelectedItemsPathNames |>") {
        if (exists($folder) != 2) { continue; }
        $files = folderreport("files:{modified yyyymmddhhmmss}|{fullname}", "r", $folder);
        $sorted = formatlist($files, "r", "<crlf>");
        $latest = gettoken(gettoken($sorted, 1, "<crlf>"), 2, "|");

        $latestNewName = regexreplace(gettoken($latest, -1, "\"), "^([\w ]+?)([\w ]{6}_.*\.)(\w+)", "$1.$3");
        copyitem $latest, "R:\$latestNewName";

        $filesForCopy = regexreplace($files, "^(\d+\|)(.*(?=$))", "$2");
        // Your copy operation for all files ($filesForCopy)
    }
One of my scripts helped you out? Please donate via Paypal

armsys
Posts: 557
Joined: 10 Mar 2012 12:40
Location: Hong Kong

Re: Foreach selected folder, copy, rename and move contents

Post by armsys »

highend wrote:Regarding the new file name...
Examples (original -> modified)?
folderreport is without the recursive flag...
Hi Highend,
Thank you for taking time to show us the XYS scripting tricks. :appl:

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

Re: Foreach selected folder, copy, rename and move contents

Post by highend »

What tricks?
One of my scripts helped you out? Please donate via Paypal

armsys
Posts: 557
Joined: 10 Mar 2012 12:40
Location: Hong Kong

Re: Foreach selected folder, copy, rename and move contents

Post by armsys »

I mean:
highend wrote:

Code: Select all

    setting "BackgroundFileOps", 0;
    foreach($folder, "<get SelectedItemsPathNames |>") {
        if (exists($folder) != 2) { continue; }
        $files = folderreport("files:{modified yyyymmddhhmmss}|{fullname}", "r", $folder);
        $sorted = formatlist($files, "r", "<crlf>");
        $latest = gettoken(gettoken($sorted, 1, "<crlf>"), 2, "|");

        $latestNewName = regexreplace(gettoken($latest, -1, "\"), "^([\w ]+?)([\w ]{6}_.*\.)(\w+)", "$1.$3");
        copyitem $latest, "R:\$latestNewName";

        $filesForCopy = regexreplace($files, "^(\d+\|)(.*(?=$))", "$2");
        // Your copy operation for all files ($filesForCopy)
    }
Thanks a million! :appl: :cup:

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

Re: Foreach selected folder, copy, rename and move contents

Post by hermhart »

highend,

So far this thing does exactly what I want. My only question is, what does the last line do that starts off with $filesForCopy?

Thank you so much!

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

Re: Foreach selected folder, copy, rename and move contents

Post by highend »

what does the last line do that starts off with $filesForCopy?
Nothing. It's a comment...

You wanted to move _all_ files of a directory to a different one. $filesForCopy contains that list.

Reformat that variable to use e.g. "|" as a separator (instead of <crlf> atm) and a moveto command
to move them to the new destination...
One of my scripts helped you out? Please donate via Paypal

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

Re: Foreach selected folder, copy, rename and move contents

Post by hermhart »

Could I ask for one more possible option? That being, in case I can't work with the latest modified file time, would you be able to come up with an alternative method where instead of the last modified file time inside the folder, it could find the file in the folder where the number matches part of what the folder name is that it exists in?

Folder name: 12345678-NAME OF FOLDER (would have either a dash or a space after the number)

File name to match: 12345678blahbl_ah.ext

End filename that it would be copied to like before: 12345678.ext (like before)

And thank you for the explanation of the last question I had.

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

Re: Foreach selected folder, copy, rename and move contents

Post by highend »

What happens when there is more than one match?

Folder:
D:\test\12345678-helloworld

Filenames:
12345678blahbl_ah.ext
12345678xyzzxy_ah.ext

Which one should be chosen (and why)?
One of my scripts helped you out? Please donate via Paypal

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

Re: Foreach selected folder, copy, rename and move contents

Post by hermhart »

That case should never happen for my circumstances, luckily.

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

Re: Foreach selected folder, copy, rename and move contents

Post by highend »

Code: Select all

    setting "BackgroundFileOps", 0;
    foreach($folder, "<get SelectedItemsPathNames |>") {
        if (exists($folder) != 2) { continue; }
        $files = listfolder($folder, , 1, "<crlf>");
        $folderNumber = regexmatches(gettoken($folder, -1, "\"), "^\d+");
        $matchingFile = regexmatches($files, "^.*\\" . $folderNumber . "[^\\]+?(?=$)");

        if ($matchingFile) {
            $matchingNewName = regexreplace(gettoken($matchingFile, -1, "\"), "^([\w ]+?)([\w ]{6}_.*\.)(\w+)", "$1.$3");
            copyitem $matchingFile, "R:\$matchingNewName";
        }

        // Your copy operation for all files ($files)
    }
One of my scripts helped you out? Please donate via Paypal

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

Re: Foreach selected folder, copy, rename and move contents

Post by hermhart »

highend,

Both of these options will work out great for me. Glad to know that I will be able to work with both circumstances.

Thank you so much!

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

Re: Foreach selected folder, copy, rename and move contents

Post by hermhart »

Can you tell me why at the bottom of the code when I am using the backupto command I get an error that reads "The following source item does not exist:", but every file that it lists is actually right where it says it is?

Code: Select all

...
    //This will take name of the folder before any spaces or dashes and use that to find the name and rename the installation file
    setting "BackgroundFileOps", 0;
    foreach($folder, "<get SelectedItemsPathNames |>") {
        if (exists($folder) != 2) { continue; }
        $files = listfolder($folder, , 1, "<crlf>");
        $folderNumber = regexmatches(gettoken($folder, -1, "\"), "^\d+");
        $matchingFile = regexmatches($files, "^.*\\" . $folderNumber . "[^\\]+?(?=$)");

        $matchingNewName = regexreplace(gettoken($matchingFile, -1, "\"), "^([\w ]+?)([\w ]{6}_.*\.)(\w+)", "$1.$3");
        copyitem $matchingFile, "$installation\$matchingNewName";

        // Your copy operation for all files ($files)

        //Moveto (force overwrite) without prompt
        backupto "$repository", "$files", 2;
        delete 1, 0, "$files";
    }

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

Re: Foreach selected folder, copy, rename and move contents

Post by highend »

I quote myself:
Reformat that variable to use e.g. "|" as a separator (instead of <crlf> atm) and a moveto command
to move them to the new destination...
One of my scripts helped you out? Please donate via Paypal

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

Re: Foreach selected folder, copy, rename and move contents

Post by hermhart »

Only issue with that will be that I need to make sure it overwrites any files that are in that destination folder, and I know that there are no arguments for that with the moveto command. Any ways around it?

Post Reply