Sorting Audio Files Into Folder Script

Discuss and share scripts and script files...
cldcp00
Posts: 52
Joined: 20 Jun 2019 15:26

Sorting Audio Files Into Folder Script

Post by cldcp00 »

Hi There,
I am new to Xyplorer and scripting and I just wanted to know it capabilities.

My situation is that I have many thousands of audio files that need to be sorted into folders so I have to visually look at the file name and drag them or cut and paste them into the relevant folder.

So I was wondering is it possible that I could have a shortcut key or a button that when triggered would present the user with a list of target folders then clicking on one of the folders would cause the script to copy/move to that folder

Is this possible and it it is could somebody point me in the right directions as to how this could be done

Thanks in advance for any help.

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

Re: Sorting Audio Files Into Folder Script

Post by highend »

Set your destination folders in the script first, one line for each folder.
Select one file, run the script once. Click outside the menu! but remember where it popped up.
Change the two position variables to better fit your needs.

Code: Select all

    $moveItem = 0; // 0 = Copy item, 1 = Move item
    $menuPosX = 500;
    $menuPosY = 500;

    $destinations = <<<>>>
C:\Temp
D:\Temp
    >>>;

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

    setting "BackgroundFileOps", 0;
    foreach($item, $selected, <crlf>, "e") {
        $base = gpc($item, "file");
        $menu = <<<>>>
            $base||$item|4
            -
            $destinations
        >>>;
        $menu   = regexreplace($menu, "^[ \t]+");
        $target = popupmenu($menu, $menuPosX, $menuPosY, 6:=<crlf>, 7:="|");
        if (!$target) { continue; }

        copyto $target, $item, , 2, 2, 2, 1, 0, 0, 1, 0, 0;
        if ($moveItem == 1) { runret(lax("cmd" /c DEL /F /Q "$item"), "%TEMP%"); }
    }
One of my scripts helped you out? Please donate via Paypal

admin
Site Admin
Posts: 60622
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

Re: Sorting Audio Files Into Folder Script

Post by admin »

This (manually sorting a bunch of files into a set of folders) seems to be a common task, and I'm getting some ideas on how to streamline it in an elegant way (see viewtopic.php?t=26566).

Your destination folders, where are they? Is there a deep hierarchy of folders?

cldcp00
Posts: 52
Joined: 20 Jun 2019 15:26

Re: Sorting Audio Files Into Folder Script

Post by cldcp00 »

Generally there would be no more that 4 level
Top level Music Library
level 2 Genre
level 3 Artist
level 4 Year

But solution should be able to move / copy file to any where on the pc

cldcp00
Posts: 52
Joined: 20 Jun 2019 15:26

Re: Sorting Audio Files Into Folder Script

Post by cldcp00 »

highend wrote: 30 Aug 2023 08:17 Set your destination folders in the script first, one line for each folder.
Select one file, run the script once. Click outside the menu! but remember where it popped up.
Change the two position variables to better fit your needs.

Code: Select all

    $moveItem = 0; // 0 = Copy item, 1 = Move item
    $menuPosX = 500;
    $menuPosY = 500;

    $destinations = <<<>>>
C:\Temp
D:\Temp
    >>>;

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

    setting "BackgroundFileOps", 0;
    foreach($item, $selected, <crlf>, "e") {
        $base = gpc($item, "file");
        $menu = <<<>>>
            $base||$item|4
            -
            $destinations
        >>>;
        $menu   = regexreplace($menu, "^[ \t]+");
        $target = popupmenu($menu, $menuPosX, $menuPosY, 6:=<crlf>, 7:="|");
        if (!$target) { continue; }

        copyto $target, $item, , 2, 2, 2, 1, 0, 0, 1, 0, 0;
        if ($moveItem == 1) { runret(lax("cmd" /c DEL /F /Q "$item"), "%TEMP%"); }
    }
Thank you very much for this script. I will make a donation when I have the funds thanks again - cldcp00

cldcp00
Posts: 52
Joined: 20 Jun 2019 15:26

Re: Sorting Audio Files Into Folder Script

Post by cldcp00 »

Hi
I decided to add an audit trail to your code but for some reason it is not working, could you point me in the right direction please.

Code: Select all

$moveItem = 0; // 0 = Copy item, 1 = Move item
$menuPosX = 500;
$menuPosY = 500;
$auditTrailFile = "D:\New Music Library\0000 Music Files Library & Information\Xyplorer Music File Audit.txt"; // Set the path to your audit trail file.

$destinations = <<<>>>
D:\New Music Library\New Music Library 2020\Dance
D:\New Music Library\New Music Library 2020\Country
D:\New Music Library\New Music Library 2020\Classical
    >>>;

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

setting "BackgroundFileOps", 0;
foreach($item, $selected, <crlf>, "e") {
    $base = gpc($item, "file");
    $menu = <<<>>>
        $base||$item|4
        -
        $destinations
    >>>;
    $menu = regexreplace($menu, "^[ \t]+");
    $target = popupmenu($menu, $menuPosX, $menuPosY, 6:=<crlf>, 7:="|");
    if (!$target) { continue; }

    copyto $target, $item, , 2, 2, 2, 1, 0, 0, 1, 0, 0;

    // Log the operation to the audit trail file.
    $auditLog = "Copied: " + $item + " to: " + $target + "\n";
    appendfile $auditTrailFile, $auditLog;

    if ($moveItem == 1) { runret(lax("cmd" /c DEL /F /Q "$item"), "%TEMP%"); }
}

admin
Site Admin
Posts: 60622
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

Re: Sorting Audio Files Into Folder Script

Post by admin »

Strings are concatenated with ., not with +.

PeterH
Posts: 2785
Joined: 21 Nov 2005 20:39
Location: Germany

Re: Sorting Audio Files Into Folder Script

Post by PeterH »

...and in double quotes variables are resolved, so you can change:

Code: Select all

// $auditLog = "Copied: " + $item + " to: " + $target + "\n";  // can be changed to:
   $auditLog = "Copied: $item to: $target\n";                 // for me this looks better!
Only thing to look for: the character following the variable name must be non-allowed in names.
Win11 Pro 223H2 Gerrman

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

Re: Sorting Audio Files Into Folder Script

Post by highend »

Replace

Code: Select all

    $auditLog = "Copied: " + $item + " to: " + $target + "\n";
    appendfile $auditTrailFile, $auditLog;
with

Code: Select all

    $auditLog .= "Copied: $item to: $target<crlf>";
and after the loop a

Code: Select all

writefile($auditTrailFile, $auditLog);
One of my scripts helped you out? Please donate via Paypal

cldcp00
Posts: 52
Joined: 20 Jun 2019 15:26

Re: Sorting Audio Files Into Folder Script

Post by cldcp00 »

Hi Highend,

I think I have amended the code as you has suggested (see below) but the script still will not run, Vert sorry if I am doing something silly.

Code: Select all

$moveItem = 0; // 0 = Copy item, 1 = Move item
$menuPosX = 500;
$menuPosY = 500;
$auditTrailFile = "D:\New Music Library\0000 Music Files Library & Information\Xyplorer Music File Audit.txt"; // Set the path to your audit trail file.

$destinations = <<<>>>
D:\New Music Library\New Music Library 2020\Dance
D:\New Music Library\New Music Library 2020\Country
D:\New Music Library\New Music Library 2020\Classical
    >>>;

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

setting "BackgroundFileOps", 0;
foreach($item, $selected, <crlf>, "e") {
    $base = gpc($item, "file");
    $menu = <<<>>>
        $base||$item|4
        -
        $destinations
    >>>;
    $menu = regexreplace($menu, "^[ \t]+");
    $target = popupmenu($menu, $menuPosX, $menuPosY, 6:=<crlf>, 7:="|");
    if (!$target) { continue; }

    copyto $target, $item, , 2, 2, 2, 1, 0, 0, 1, 0, 0;

    // Log the operation to the audit trail file.
    $auditLog .= "Copied: $item to: $target<crlf>";

    if ($moveItem == 1) { runret(lax("cmd" /c DEL /F /Q "$item"), "%TEMP%"); }
}
writefile($auditTrailFile, $auditLog);

jupe
Posts: 2805
Joined: 20 Oct 2017 21:14
Location: Win10 22H2 120dpi

Re: Sorting Audio Files Into Folder Script

Post by jupe »

You have removed the indenting, make it match the original, also you might want to add the "a" flag to writefile, else you will lose your log after each run.

cldcp00
Posts: 52
Joined: 20 Jun 2019 15:26

Re: Sorting Audio Files Into Folder Script

Post by cldcp00 »

Hi Jupe
Thanks for you help, i am still leaning.
Are you saying that indenting the last line will make it work?
line of code
writefile($auditTrailFile, $auditLog);
Also
Not sure what you mean sorry
also you might want to add the "a" flag to writefile, else you will lose your log after each run

jupe
Posts: 2805
Joined: 20 Oct 2017 21:14
Location: Win10 22H2 120dpi

Re: Sorting Audio Files Into Folder Script

Post by jupe »

No not just the last line, I said make the indenting (of the whole script) match the original, also indent the writefile line to match the indent of the curly brace above it. Indenting is important.

Lookup the writefile command in the help file, where you will see you require "a", as the third parameter.

Also since you didn't initialize $auditLog var you will have that text prefixed to each log entry, init the var like this $auditLog = ""; at the start of your script to avoid this.

PeterH
Posts: 2785
Joined: 21 Nov 2005 20:39
Location: Germany

Re: Sorting Audio Files Into Folder Script

Post by PeterH »

But be careful: this regards all *executable* lines...

... but <<<>>> begins a group of text lines (up to >>>) - so it must be:

Code: Select all

   $destinations = <<<>>>
D:\New Music Library\New Music Library 2020\Dance
D:\New Music Library\New Music Library 2020\Country
D:\New Music Library\New Music Library 2020\Classical
    >>>;
Win11 Pro 223H2 Gerrman

jupe
Posts: 2805
Joined: 20 Oct 2017 21:14
Location: Win10 22H2 120dpi

Re: Sorting Audio Files Into Folder Script

Post by jupe »

Yep, that's why I said "match the original", but in this use case it wouldn't matter if that section was indented anyway, because any extraneous spaces would get removed by running the script.

Post Reply