[Solved] Move up only files in subfolder

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
perseid
Posts: 13
Joined: 25 Mar 2012 06:41

[Solved] Move up only files in subfolder

Post 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
Last edited by perseid on 06 Jul 2025 21:09, edited 1 time in total.

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

Re: Move up only files in subfolder

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

perseid
Posts: 13
Joined: 25 Mar 2012 06:41

Re: Move up only files in subfolder

Post 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

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

Re: Move up only files in subfolder

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

perseid
Posts: 13
Joined: 25 Mar 2012 06:41

Re: Move up only files in subfolder

Post 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

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

Re: Move up only files in subfolder

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

perseid
Posts: 13
Joined: 25 Mar 2012 06:41

Re: Move up only files in subfolder

Post 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

perseid
Posts: 13
Joined: 25 Mar 2012 06:41

Re: Move up only files in subfolder

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

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

Re: Move up only files in subfolder

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

perseid
Posts: 13
Joined: 25 Mar 2012 06:41

Re: Move up only files in subfolder

Post 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

Post Reply