Page 1 of 2

Sorting Audio Files Into Folder Script

Posted: 29 Aug 2023 23:17
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.

Re: Sorting Audio Files Into Folder Script

Posted: 30 Aug 2023 08:17
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%"); }
    }

Re: Sorting Audio Files Into Folder Script

Posted: 30 Aug 2023 09:37
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?

Re: Sorting Audio Files Into Folder Script

Posted: 30 Aug 2023 22:11
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

Re: Sorting Audio Files Into Folder Script

Posted: 01 Sep 2023 22:01
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

Re: Sorting Audio Files Into Folder Script

Posted: 02 Sep 2023 00:13
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%"); }
}

Re: Sorting Audio Files Into Folder Script

Posted: 02 Sep 2023 09:22
by admin
Strings are concatenated with ., not with +.

Re: Sorting Audio Files Into Folder Script

Posted: 02 Sep 2023 12:15
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.

Re: Sorting Audio Files Into Folder Script

Posted: 02 Sep 2023 13:57
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);

Re: Sorting Audio Files Into Folder Script

Posted: 02 Sep 2023 21:48
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);

Re: Sorting Audio Files Into Folder Script

Posted: 02 Sep 2023 21:55
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.

Re: Sorting Audio Files Into Folder Script

Posted: 02 Sep 2023 22:28
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

Re: Sorting Audio Files Into Folder Script

Posted: 02 Sep 2023 22:34
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.

Re: Sorting Audio Files Into Folder Script

Posted: 02 Sep 2023 23:11
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
    >>>;

Re: Sorting Audio Files Into Folder Script

Posted: 02 Sep 2023 23:22
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.