Page 1 of 1
Script Request: Move folder that contains 2 same filetype
Posted: 06 Jul 2017 06:44
by pleiades
Hi,
I like to request a script if its possible, the script with scan all the folders, if a folder contains 2 same filetype, it will then move it to another folder outside.
Example
FolderA contains Folders F1, F2, F3
C:\FolderA\
C:\FolderA\F1\
C:\FolderA\F1\picture.jpg
C:\FolderA\F1\elephant.jpg
contains 2 jpgs - contains 2 same filetype
C:\FolderA\F2\
C:\FolderA\F2\picture.jpg
C:\FolderA\F2\animal.jpg
C:\FolderA\F2\dog.pdf
contains 2 jpgs and 1 pdf - contains 2 same filetype
C:\FolderA\F3\
C:\FolderA\F3\book.epub
C:\FolderA\F3\logbook.pdf
contains 2 different filetype - does not contain 2 same filetype
If we run the script in C:\FolderA
it will move Folders F1 and F2 and leave F3 alone
The folder will be moved to another folder example C:\FolderB
The Result will be after running the script
Folder A
C:\FolderA\
C:\FolderA\F3
C:\FolderA\F3\book.epub
C:\FolderA\F3\logbook.pdf
Folder B
C:\FolderB\
C:\FolderB\F1
C:\FolderB\F1\picture.jpg
C:\FolderB\F1\elephant.jpg
C:\FolderB\F2\
C:\FolderB\F2\picture.jpg
C:\FolderB\F2\animal.jpg
C:\FolderB\F2\dog.pdf
Thank you!
Re: Script Request: Move folder that contains 2 same filetype
Posted: 06 Jul 2017 07:58
by highend
1. How exactly should a new folder be named when
it has to be created?
Use a real world example for the original folder name and
any new one(s)
2. Does it matter which file type is found twice in a folder at all?
E.g. if C:\FolderA\F2\
Contains: image1.iso, image2.iso
Still move this folder out?
3. Is this example bound to only a single subfolder deep structure?
In other words, all
C:\FolderA\F1\
C:\FolderA\F2\
...
do NOT contain other sub folders themselves?
Re: Script Request: Move folder that contains 2 same filetype
Posted: 06 Jul 2017 10:37
by pleiades
Hi highend, thank you for the help,
1. How exactly should a new folder be named when
it has to be created?
Use a real world example for the original folder name and
any new one(s)
The new folder could be named "tmp" to mean temporary, if the user run the script in the tmp folder, it will not run because it won't be able to move the folders to its own folder.
2. Does it matter which file type is found twice in a folder at all?
E.g. if C:\FolderA\F2\
Contains: image1.iso, image2.iso
Still move this folder out? Yes
It does not matter which file type, as long as the folder contains 2 same file type, it will move it out. In the example above, the script will move it to the c:\tmp folder
3. Is this example bound to only a single subfolder deep structure?
In other words, all
C:\FolderA\F1\
C:\FolderA\F2\
...
do NOT contain other sub folders themselves?
Yes, the script will search only single subfolder deep, if the first subfolder contain another folder, it will skip the folder and only check the files.
Example
Search in FolderA
C:\FolderA\F1\
C:\FolderA\F2\SF1\SSF1
If the script is run in FolderA, it will search Folders F1 and F2
Search in F2
C:\FolderA\F1\
C:\FolderA\F2\SF1\SSF1
If the script is run in Folder F2, it will only search Folder SF1
Search in F1
C:\FolderA\F1\
C:\FolderA\F2\SF1\SSF1
If the script is run in F1, it will not search anything because it does not have any folders
Re: Script Request: Move folder that contains 2 same filetype
Posted: 06 Jul 2017 10:49
by bdeshi
Also, is it
exactly 2 of same filetype or
at least 2 of same filetype?
Code: Select all
C:\FolderA\F2\
C:\FolderA\F2\picture.jpg
C:\FolderA\F2\animal.jpg
C:\FolderA\F2\onemore.jpg
C:\FolderA\F2\dog.pdf
contains 3 jpgs and 1 pdf - contains
at least 2 same filetype.
Move this folder too?
Re: Script Request: Move folder that contains 2 same filetype
Posted: 06 Jul 2017 10:57
by highend
Code: Select all
$newFolder = "tmp"; // The prefix for the new folder name(s)
setting "BackgroundFileOps", 0;
$subfolders = listfolder(, , 2+4, <crlf>);
$moved = "";
foreach($subfolder, $subfolders, <crlf>, "e") {
$files = listfolder("<curpath>\$subfolder", , 1+4, <crlf>);
$extensions = regexreplace($files, "^.*\.");
$pre = formatlist($extensions, "se", <crlf>);
$post = formatlist($extensions, "sed", <crlf>);
// If $pre and $post are different, at least one extension does exist twice
if ($pre != $post) {
$moved = $moved . "<curpath>\$subfolder" . <crlf>;
$parent = gpc("<curpath>\$subfolder", "component", -3, 1);
moveas "$newFolder-*", "$parent", "<curpath>\$subfolder";
}
}
if ($moved) { text "The following folder(s) were moved to the parent folder:<crlf 2>$moved"; }
else { echo "No sub folder(s) found that contain two of the same file type..."; }
Re: Script Request: Move folder that contains 2 same filetype
Posted: 07 Jul 2017 02:31
by pleiades
SammaySarkar wrote:Also, is it
exactly 2 of same filetype or
at least 2 of same filetype?
Code: Select all
C:\FolderA\F2\
C:\FolderA\F2\picture.jpg
C:\FolderA\F2\animal.jpg
C:\FolderA\F2\onemore.jpg
C:\FolderA\F2\dog.pdf
contains 3 jpgs and 1 pdf - contains
at least 2 same filetype.
Move this folder too?
Yes sir SammaySarkar, as long as it has 2 of the same filetype, in the example it will be moved.
Re: Script Request: Move folder that contains 2 same filetype
Posted: 08 Jul 2017 02:48
by pleiades
Thank you highend, the script works!
if you have the time, can you explain this part of the script?
$moved = "";
foreach($subfolder, $subfolders, <crlf>, "e") {
$files = listfolder("<curpath>\$subfolder", , 1+4, <crlf>);
$extensions = regexreplace($files, "^.*\.");
$pre = formatlist($extensions, "se", <crlf>);
$post = formatlist($extensions, "sed", <crlf>);
you changed the file extension there?
Re: Script Request: Move folder that contains 2 same filetype
Posted: 08 Jul 2017 07:06
by highend
It gets the list of all files within each sub folder
From this list, everything apart from the extensions is removed
This list is filtered two times, one time without removing duplicates and one time with removing them
And if they differ, the list must have contained at least on file type two times...