Foreach selected folder, copy, rename and move contents
Foreach selected folder, copy, rename and move contents
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.
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.
Re: Foreach selected folder, copy, rename and move contents
Regarding the new file name...
Examples (original -> modified)?
folderreport is without the recursive flag...
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
Re: Foreach selected folder, copy, rename and move contents
Hi Highend,highend wrote:Regarding the new file name...
Examples (original -> modified)?
folderreport is without the recursive flag...
Thank you for taking time to show us the XYS scripting tricks.
Re: Foreach selected folder, copy, rename and move contents
What tricks?
One of my scripts helped you out? Please donate via Paypal
Re: Foreach selected folder, copy, rename and move contents
I mean:
Thanks a million!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) }
Re: Foreach selected folder, copy, rename and move contents
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!
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!
Re: Foreach selected folder, copy, rename and move contents
Nothing. It's a comment...what does the last line do that starts off with $filesForCopy?
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
Re: Foreach selected folder, copy, rename and move contents
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.
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.
Re: Foreach selected folder, copy, rename and move contents
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)?
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
Re: Foreach selected folder, copy, rename and move contents
That case should never happen for my circumstances, luckily.
Re: Foreach selected folder, copy, rename and move contents
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
Re: Foreach selected folder, copy, rename and move contents
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!
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!
Re: Foreach selected folder, copy, rename and move contents
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";
}
Re: Foreach selected folder, copy, rename and move contents
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
Re: Foreach selected folder, copy, rename and move contents
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?