Page 1 of 1

[Solved] Move up only files in subfolder

Posted: 03 Jul 2025 08:45
by perseid
Say I have multiple folders each with a subfolder and two files, one in the subfolder and the other one in the parent folder. How can I tell XYplorer to just move up the subfolder file but not the folder file? thanks

Re: Move up only files in subfolder

Posted: 03 Jul 2025 09:45
by highend

Code: Select all

D:\Temp (you are now inside of this folder!)
├ folder 1
│    ├ subfolder 1
│    │    └ abc.txt
│    └ def.txt
│
└ folder 2
     ├ subfolder 2
     │    └ gih.txt
     └ jkl.txt
You want the files
abc.txt
moved (individually) into folder 1
and
gih.txt
moved (individually) into folder 2

OR
abc.txt
AND
gih.txt
moved (both) into D:\Temp
?

Re: Move up only files in subfolder

Posted: 03 Jul 2025 19:21
by perseid
Hi highend, first option:
abc.txt
moved (individually) into folder 1
and
gih.txt
moved (individually) into folder 2

with the added option that in case "def.txt" or "jkl.txt" has the same filename as "abc.txt" or "gih.txt", that the move up command will not result in abc.txt or gih.txt overwriting the respective files in parent folder but rather moved up and renamed to "abc_1.txt" or "gih_1.txt" so both files are kept in parent folder
Thanks

Re: Move up only files in subfolder

Posted: 03 Jul 2025 21:34
by highend

Code: Select all

    $rootFolders = listfolder(, , 2, <crlf>);
    end (!$rootFolders), "No folder(s) in current directory, aborted!";

    // If one of the root folders has a subfolder and a single file in it, move it up
    setting "BackgroundFileOps", 0;
    foreach($rootFolder, $rootFolders, <crlf>, "e") {
        $subFolders    = listfolder($rootFolder, , 2, <crlf>);
        $cntSubFolders = gettoken($subFolders, "count", <crlf>);
        if ($cntSubFolders != 1) { continue; }

        $subFiles    = listfolder($subFolders, , 1, <crlf>);
        $cntSubFiles = gettoken($subFiles, "count", <crlf>);
        if ($cntSubFiles != 1) { continue; }

        // We have one file in one subfolder, move it up
        moveto $rootFolder, $subFiles, , 0, 2, 4, 1, 0, 0, 1, 0, 0;
    }

Re: Move up only files in subfolder

Posted: 03 Jul 2025 23:16
by perseid
highend, this is what I did:
1) Open parent folder with XYplorer (it will display all subfolders and parent files)
2) In the upper menu click on "Scripting" and then "Run Script..."
3) Copy your script and paste it in the "Run Script..." window
4) When I run the script nothing happens, file structure remains unaltered

Am I doing something wrong? Thanks again

Re: Move up only files in subfolder

Posted: 03 Jul 2025 23:46
by highend
It will skip if e.g. folder 1 has more subfolders than 1 (or none at all) or if a subfolder contains more than one file (or none).
You are sure that's not the case on your side?

Code: Select all

    $rootFolders = listfolder(, , 2, <crlf>);
    end (!$rootFolders), "No folder(s) in current directory, aborted!";

    // If one of the root folders has a subfolder and a single file in it, move it up
    $log = "";
    setting "BackgroundFileOps", 0;
    foreach($rootFolder, $rootFolders, <crlf>, "e") {
        $subFolders    = listfolder($rootFolder, , 2, <crlf>);
        $cntSubFolders = gettoken($subFolders, "count", <crlf>);
        if ($cntSubFolders != 1) {
            $log .= "Skipped [0 or > 1 subfolders] - " . $rootFolder . <crlf>;
            continue;
        }

        $subFiles    = listfolder($subFolders, , 1, <crlf>);
        $cntSubFiles = gettoken($subFiles, "count", <crlf>);
        if ($cntSubFiles != 1) {
            $log .= "Skipped [0 or > 1 files] in: " . $subFolders . <crlf>;
            continue;
        }
        // We have one file in one subfolder, move it up
        moveto $rootFolder, $subFiles, , 0, 2, 4, 1, 0, 0, 1, 0, 0;
    }
    if ($log) { text $log; }

Re: Move up only files in subfolder

Posted: 04 Jul 2025 02:01
by perseid
This is the file structure I created to test your script the results of which I reported in my previous post:
c:\1 folder with subfolders 2 and 3
2.txt in folder 1
1.txt and 2.txt in subfolder 2
3.txt in subfolder 3

Re: Move up only files in subfolder

Posted: 05 Jul 2025 06:12
by perseid
highend wrote: 03 Jul 2025 23:46 It will skip if e.g. folder 1 has more subfolders than 1 (or none at all) or if a subfolder contains more than one file (or none).
You are sure that's not the case on your side?
Just to make sure I understood, I modified the file structure so that it meets the condition to run the script, i.e., only one subfolder and only 1 file in the subfolder. The new tested file structure is:
c:\1\3\3.txt
When running the script in XYplorer again nothing is modified, instead I get the following message:
"Skipped [0 or > 1 subfolders] - C:\1\3"

Re: Move up only files in subfolder

Posted: 05 Jul 2025 06:38
by highend
That is how your file structure looked like (at least that's how you've described it in the first posting):

Code: Select all

D:\Temp (you are now inside of this folder!)
├ folder 1
│    ├ subfolder 1
│    │    └ abc.txt
│    └ def.txt
│
└ folder 2
     ├ subfolder 2
     │    └ gih.txt
     └ jkl.txt
But in your current case you're (adapted to this example) not currently in D:\Temp but in D:\Temp\folder 1 when executing the script
Or with your example directly:
C:\1\3\3.txt but you're executing the script while being in C:\1 and not in C:\

In other words: C:\1 does NOT contain two hierarchies of folders inside but only one (the "3" subfolder)
Hence the error log message

For the structure you've just posted, this would have been enough:

Code: Select all

    $rootFolders = listfolder(, , 2, <crlf>);
    end (!$rootFolders), "No folder(s) in current directory, aborted!";

    // If the root folder has one subfolder and one file in it, move it up
    setting "BackgroundFileOps", 0;
    $cntRootFolders = gettoken($rootFolders, "count", <crlf>);
    end ($cntRootFolders != 1), "No or more than one root folders, aborted!";

    $subFiles    = listfolder($rootFolders, , 1, <crlf>);
    $cntSubFiles = gettoken($subFiles, "count", <crlf>);
    end ($cntSubFiles != 1), "No or more than one sub files, aborted!";

    moveto <curpath>, $subFiles, , 0, 2, 4, 1, 0, 0, 1, 0, 0;

Re: Move up only files in subfolder

Posted: 05 Jul 2025 21:24
by perseid
highend wrote: 05 Jul 2025 06:38 For the structure you've just posted, this would have been enough:

Code: Select all

    $rootFolders = listfolder(, , 2, <crlf>);
    end (!$rootFolders), "No folder(s) in current directory, aborted!";

    // If the root folder has one subfolder and one file in it, move it up
    setting "BackgroundFileOps", 0;
    $cntRootFolders = gettoken($rootFolders, "count", <crlf>);
    end ($cntRootFolders != 1), "No or more than one root folders, aborted!";

    $subFiles    = listfolder($rootFolders, , 1, <crlf>);
    $cntSubFiles = gettoken($subFiles, "count", <crlf>);
    end ($cntSubFiles != 1), "No or more than one sub files, aborted!";

    moveto <curpath>, $subFiles, , 0, 2, 4, 1, 0, 0, 1, 0, 0;
Thanks, just tested and it works now! I did not pay attention to the hierarchy (depth), I thought that your script was hierarchy-independent