Split files into folders

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
Theodecarvalho
Posts: 49
Joined: 17 Jul 2022 11:59

Split files into folders

Post by Theodecarvalho »

Hi guys!

Is there a way to pack files by splitting them using part of a name?

I have a lot of files. Example:

Test01
Test02
Test03
Preview01
Preview02
Preview03
Done01
Done02
Done03

I need to split this files into folders creating and separating by part of the name. Like this:

Test
| - Test01
| - Test02
| - Test03
Preview
| - Preview01
| - Preview02
| - Preview03
Done
| - Done01
| - Done02
| - Done03

Maybe I can split by numer of characters and them, rename folders in case the number of characters is longer than other.... I have no clue how can I do that

RalphM
Posts: 2049
Joined: 27 Jan 2005 23:38
Location: Cairns, Australia

Re: Split files into folders

Post by RalphM »

Automatically without any manual input by you? I doubt that, especially if the common parts of the filenames have different lengths.
You could certainly script something that would sort the filelist alphabetically and then use the first n characters of a filename and move all files that match within these n chars into a subfolder of the same name.
In your example, checking for the first 4 chars would work but I'm sure there are cases where this logic would fail and group too many files in one folder.
Ralph :)
(OS: W11 24H2 Home x64 - XY: Current x32 beta - Office 2024 32-bit - Display: 1920x1080 @ 125%)

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

Re: Split files into folders

Post by highend »

For that pattern? By regex...
One of my scripts helped you out? Please donate via Paypal

Theodecarvalho
Posts: 49
Joined: 17 Jul 2022 11:59

Re: Split files into folders

Post by Theodecarvalho »

RalphM wrote: 04 Jan 2023 13:48 Automatically without any manual input by you? I doubt that, especially if the common parts of the filenames have different lengths.
You could certainly script something that would sort the filelist alphabetically and then use the first n characters of a filename and move all files that match within these n chars into a subfolder of the same name.
In your example, checking for the first 4 chars would work but I'm sure there are cases where this logic would fail and group too many files in one folder.
That works for me. Something about X characters do merge files in one folder. All files that I have to split has the inicial letters in common.

Like this (imagine 4 characters):

Test
| - Test01
| - Test02
| - Test03
Prev
| - Preview01
| - Preview02
| - Preview03
Done
| - Done01
| - Done02
| - Done03

Than, I rename folder if has more than 4 chars

matewo
Posts: 100
Joined: 03 Jun 2018 21:35

Re: Split files into folders

Post by matewo »

Hi Theodecarvalho,

maybe another way to find the common parts of file names:

Do the differing parts of the file names have something in common, which distinguish these from the common "base name"?

Regarding your samples (yes, I know, just samples), the differing parts are trailing two-digit numbers: ...01, ...02 and ...03.

Best regards, Markus

Theodecarvalho
Posts: 49
Joined: 17 Jul 2022 11:59

Re: Split files into folders

Post by Theodecarvalho »

matewo wrote: 04 Jan 2023 23:04 Hi Theodecarvalho,

maybe another way to find the common parts of file names:

Do the differing parts of the file names have something in common, which distinguish these from the common "base name"?

Regarding your samples (yes, I know, just samples), the differing parts are trailing two-digit numbers: ...01, ...02 and ...03.

Best regards, Markus
Always the first X chars are common that I want to move into a folder. Like an example:
MAP23-01XXXXXX.PNG
MAP23-01XXX.PNG
MAP25-02XXXXXXXX.PNG
MAP25-02XXX.PNG

All I need is to group this files into folders, removing useless variables to crate a folder and insert a file correspondent in this folder, like:
MAP23-01
MAP25-02

I already try: Used a script to create and movet each file in a folder that has the same name, and than rename a folder to merge, like this:
MAP23-01XXXXXX
- MAP23-01XXXXXX.PNG
MAP23-01XXX
- MAP23-01XXX.PNG
MAP25-02XXXXXXXX
- MAP25-02XXXXXXXX.PNG
MAP25-02XXX
- MAP25-02XXX.PNG

Than, my idea was to rename this folders, leaving inicial X chars and merge then. But... All rename softwares that I tried, dont merge the folder, leaving like this:
MAP23-01
- MAP23-01XXXXXX.PNG
MAP23-01(1)
- MAP23-01XXX.PNG
MAP25-02
- MAP25-02XXXXXXXX.PNG
MAP25-02(1)
- MAP25-02XXX.PNG

The problem is that I have so many files to organize into folders. And I need to organize manually.

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

Re: Split files into folders

Post by highend »

Select only those files that match the same pattern! E.g. that have the first 5 characters in common and then enter that number in the input line^^

Code: Select all

    $selected = <get SelectedItemsPathNames>;
    end (!$selected), "No item(s) selected, aborted!";

    $cntChars = trim(input("Enter the number of characters", , 4));
    end (regexmatches($cntChars, "[^0-9]") || $cntChars <= 0), "No characters allowed or bad value, aborted!";

    foreach($item, $selected, <crlf>, "e") {
        if (exists($item) == 2) { continue; }

        $base = gpc($item, "base");
        $path = gpc($item, "path");
        $pre  = trim(substr($base, 0, $cntChars));
        $dst  = $path . "\" . $pre;
        moveto $dst, $item, , 2, 2, 2, 1;
    }
One of my scripts helped you out? Please donate via Paypal

matewo
Posts: 100
Joined: 03 Jun 2018 21:35

Re: Split files into folders

Post by matewo »

Hi Theodecarvalho,

thanks for clarification! I tried the following, maybe it matches your requirements:

Initial situation: all files in one directory. The .\ means the "current directory":

Code: Select all


.\MAP23-01XXX.PNG
.\MAP23-01XXXXXX.PNG
.\MAP25-02XXX.PNG
.\MAP25-02XXXXXXXX.PNG

I selected files MAP23-01XXX.PNG and MAP25-02XXXXXXXX.PNG and opened Rename Special - RegExp Rename... in the context menu. As the regular expression, I entered ^(MAP[0-9]+-[0-9][0-9])(.*) > $1\$1$2. Starting the rename, I ended up with following:

Code: Select all


.\MAP23-01XXXXXX.PNG
.\MAP25-02XXX.PNG
.\MAP23-01\MAP23-01XXX.PNG
.\MAP25-02\MAP25-02XXXXXXXX.PNG

Doing the same with files MAP23-01XXXXXX.PNG and MAP25-02XXX.PNG, I ended up with:

Code: Select all


.\MAP23-01\MAP23-01XXX.PNG
.\MAP23-01\MAP23-01XXXXXX.PNG
.\MAP25-02\MAP25-02XXX.PNG
.\MAP25-02\MAP25-02XXXXXXXX.PNG

XYplorer renames the selected files and automatically moves them as well into the subdirectories ($1\$1$2), if option "Allow move on rename" is selected.

Additionally, no folders MAP23-01(1) nor MAP25-02(1) are created and thus no additional work for you.

Regards, Markus

Theodecarvalho
Posts: 49
Joined: 17 Jul 2022 11:59

Re: Split files into folders

Post by Theodecarvalho »

Highend, I´re a F***** genious! A time-saver and you are helping a lot of people.

Xy rules! btw, I send you a beer in paypal.

Cheers.

Post Reply