Move selected files to new folders with same file names

Discuss and share scripts and script files...
Post Reply
badrightful
Posts: 5
Joined: 25 Apr 2018 22:57

Move selected files to new folders with same file names

Post by badrightful »

Hey everyone, I want to move selected files to newly created folders which have the same files name without the extension.

So, C:\movies\clip1.mp4 should be moved to C:\movies\clip1
and, C:\movies\clip2.mp4 should be moved to C:\movies\clip2

I know that the "moveto" command will do the job but I'm stuck at name the new folders with the same name of the files part.

I know it seems an easy task and a stupid question but here I am.

highend
Posts: 13274
Joined: 06 Feb 2011 00:33

Re: Move selected files to new folders with same file names

Post by highend »

Code: Select all

    foreach($item, <get SelectedItemsPathNames |>, , "e") {
        moveto "<curpath>\" . gpc($item, "base"), $item, , 2;
    }
One of my scripts helped you out? Please donate via Paypal

badrightful
Posts: 5
Joined: 25 Apr 2018 22:57

Re: Move selected files to new folders with same file names

Post by badrightful »

Thanks! it worked. Another question; what if I want to use part of the file name only as a destination;

For Ex: I have group of jpgs like
C:\images\zooexample.png_date(randomenumber).jpg
C:\images\zooexample.png_date(randomenumber2).jpg
C:\images\zooexample.png_date(randomenumber23).jpg

I want to move these file to be in folder called "zooexample" which means it'll use the part before (.png_date) and ignore the rest of the name.

highend
Posts: 13274
Joined: 06 Feb 2011 00:33

Re: Move selected files to new folders with same file names

Post by highend »

And ALL your files follow the same pattern?

<some part of the file name>.<something else>.<extension>

And it's always: Destination folder is ONLY <some part of the file name>?
One of my scripts helped you out? Please donate via Paypal

badrightful
Posts: 5
Joined: 25 Apr 2018 22:57

Re: Move selected files to new folders with same file names

Post by badrightful »

Yes. <some part of the file name> only will change so:

C:\images\zooexample.png_date(randomenumber).jpg
C:\images\zooexample.png_date(randomenumber2).jpg
C:\images\zooexample.png_date(randomenumber23).jpg
C:\images\homeexample.png_date(randomenumber123213).jpg
C:\images\homeexample.png_date(randomenumber234324).jpg
C:\images\homeexample.png_date(randomenumber233242342).jpg

The first 3 files should be in C:\images\zooexample , and next group will be in C:\images\homeexample

highend
Posts: 13274
Joined: 06 Feb 2011 00:33

Re: Move selected files to new folders with same file names

Post by highend »

Code: Select all

    foreach($item, <get SelectedItemsPathNames |>, , "e") {
        moveto "<curpath>\" . gettoken(gpc($item, "base"), 1, "."), $item, , 2;
    }
One of my scripts helped you out? Please donate via Paypal

badrightful
Posts: 5
Joined: 25 Apr 2018 22:57

Re: Move selected files to new folders with same file names

Post by badrightful »

That is my bad, sometimes the <some part of the file name> could contain a [.] I imagine something like using regular expression that exclude the part from the middle which always have the same pattern (.png_date*.*) and return only the part before.

highend
Posts: 13274
Joined: 06 Feb 2011 00:33

Re: Move selected files to new folders with same file names

Post by highend »

Code: Select all

    foreach($item, <get SelectedItemsPathNames |>, , "e") {
        moveto "<curpath>\" . regexreplace(gpc($item, "base"), "^(.*?)\.png_date.*", "$1"), $item, , 2;
    }
One of my scripts helped you out? Please donate via Paypal

badrightful
Posts: 5
Joined: 25 Apr 2018 22:57

Re: Move selected files to new folders with same file names

Post by badrightful »

Man, you saved my day. Thanks

Skettalee
Posts: 99
Joined: 06 May 2019 20:27

Re: Move selected files to new folders with same file names

Post by Skettalee »

Hey im trying to do something somewhat similar i think. But mot exactly. I have 10000's of fonts all in one folder, I would like to quickly group each font family together into that family's folder name. so like Avera-Sans-Bold, Avera-sans-Cn-Bold,Avera-sans-light, and so on would all be placed in a new folder called "Avera-Sans" in that same folder. So whatever I select, it finds what the similarity of all the selected files are, and then creates a folder named that similarity and places all the selected files into the new folder. Is this something that I could accomplish using Xyplorer?

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

Re: Move selected files to new folders with same file names

Post by RalphM »

If you are planning to select the files manually anyway you might just use "Move here to new subfolder" and the new subfolder name is suggested to be the same as the first selected file, then you just delete the part you don't need.

If you are planning to automate this further, the tricky part would be to find the common denominator of the names of all the selected files (the part that is similar for all of them) and if you want to run this through a folder without any interaction then the next challenge would be to find the groups of similar names, especially if you have let's say "Avera-sans" and another one "Avera-whatever", would they all end up in "Avera" or split up in two different folders?
Ralph :)
(OS: W11 22H2 Home x64 - XY: Current beta - Office 2019 32-bit - Display: 1920x1080 @ 125%)

Skettalee
Posts: 99
Joined: 06 May 2019 20:27

Re: Move selected files to new folders with same file names

Post by Skettalee »

Yeah I was thinking that myself, just figured I would try to ask. I appreciate it, your idea of the move all to subfolder and then a dialog popping up for the name isn't that bad. I figured that feature was there, just haven't dug that deep just yet until now. Thanks for the reply!

highend
Posts: 13274
Joined: 06 Feb 2011 00:33

Re: Move selected files to new folders with same file names

Post by highend »

This is done by using exiftool (the CLI VERSION!)

You have to go into the folder where your font files are, first.
If you want to have the font family name further reduced, you have to do this yourself, because there could be hundreds of extensions that would need to be removed, depending on your font files...

Code: Select all

    $exifToolCLI = "<path to exiftool>\exiftool.exe";
    end (exists($exifToolCLI) != 1), "exiftool does not exist, aborted!";

    $files = listfolder(, , 1, <crlf>);
    $cnt   = gettoken($files, "count", <crlf>);

    while ($i++ < $cnt) {
        status "Processing: " . format($i, strrepeat("0", strlen($cnt))) . " / $cnt", , "progress";
        $file   = gettoken($files, $i, <crlf>);
        $family = runret("""$exifToolCLI"" -fontfamily ""$file""", , 65001);
        if (!$family || regexmatches($family, "^error:")) { continue; }
        $family = trim(gettoken($family, 2, ":", "t"), <crlf>, "R");
        if ($family) {
            moveto <curpath> . "\" . $family , $file, , 2, 2;
        }
        wait 1;
    }
    echo "Finished!";
One of my scripts helped you out? Please donate via Paypal

Skettalee
Posts: 99
Joined: 06 May 2019 20:27

Re: Move selected files to new folders with same file names

Post by Skettalee »

Ah dude that worked like a charm. Thank you very very much!

highend
Posts: 13274
Joined: 06 Feb 2011 00:33

Re: Move selected files to new folders with same file names

Post by highend »

Btw, this would have been a hundred times faster (but really only works for all files in the same folder)...

Code: Select all

    $exifTool = "<path to>\exiftool.exe"; // https://exiftool.org/

    // ========================================================================
    // == DO NOT CHANGE ANYTHING BELOW IF YOU DO NOT KNOW WHAT YOU ARE DOING ==
    // ========================================================================
    end (exists($exifTool) != 1), "exiftool does not exist, aborted!";

    // Get metadata for all item(s) in <curpath>
    $results  = runret("""$exifTool"" -fontfamily -p ""${directory;s(/)(\\)g}\$filename|${fontfamily}"" ""<curpath>""", , 65001);
    // Keep only valid ones (no errors, warnings, etc.)
    $items    = regexmatches($results, "^([a-z]:|\\\\).+?(?=\r?\n|$)", <crlf>);
    $cntItems = gettoken($items, "count", <crlf>);
    $lenPath  = strlen($modPath);
    end (!$items), "No valid item(s) found, aborted!";

    while ($i++ < $cntItems) {
        status "Processing: " . format($i, strrepeat("0", strlen($cntItems))) . " / $cntItems", , "progress";
        $item = gettoken($items, $i, <crlf>);

        // Only process item with metadata...
        $metadata = gettoken($item, 2, "|", "t");
        if ($metadata) { moveto <curpath> . "\" . $metadata, gettoken($item, 1, "|"), , 2, 2; }
        wait 1;
    }
    echo "Finished!";

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

Post Reply