Script Request: Move folder that contains 2 same filetype

Discuss and share scripts and script files...
Post Reply
pleiades
Posts: 52
Joined: 13 Aug 2016 10:36

Script Request: Move folder that contains 2 same filetype

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

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

Re: Script Request: Move folder that contains 2 same filetype

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

pleiades
Posts: 52
Joined: 13 Aug 2016 10:36

Re: Script Request: Move folder that contains 2 same filetype

Post 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

bdeshi
Posts: 4249
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612 / Dhaka
Contact:

Re: Script Request: Move folder that contains 2 same filetype

Post 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?
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

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

Re: Script Request: Move folder that contains 2 same filetype

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

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

pleiades
Posts: 52
Joined: 13 Aug 2016 10:36

Re: Script Request: Move folder that contains 2 same filetype

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

pleiades
Posts: 52
Joined: 13 Aug 2016 10:36

Re: Script Request: Move folder that contains 2 same filetype

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

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

Re: Script Request: Move folder that contains 2 same filetype

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

Post Reply