# Sorting in different folders by file type

**URL:** <https://community.metabrainz.org/t/sorting-in-different-folders-by-file-type/498269>\
**Category:** MusicBrainz Picard\
**Tags:** scripting\
**Created:** [October 8, 2020, 1:33pm UTC](https://community.metabrainz.org/t/sorting-in-different-folders-by-file-type/498269 "2020-10-08T13:33:24Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![tip32a](https://community.metabrainz.org/letter_avatar_proxy/v4/letter/t/bbce88/32.png) [@tip32a](https://community.metabrainz.org/u/tip32a)\
**Post date:** [October 8, 2020, 1:33pm UTC](https://community.metabrainz.org/t/sorting-in-different-folders-by-file-type/498269/1 "2020-10-08T13:33:24Z")

</div>

I would like to sort my music by Extension, Artist, Album, Track. So I have a MP3, Wav, M4A and so on root folders then Artist, Album, Track.

I would like each file to be renamed, Artist, Album, track when it is moved.

Would someone mind writing a script for this I would greatly appreciate it.  
Thanks in advance  
Tom

---

<div class="post-metadata">

**Author:** ![outsidecontext](https://community.metabrainz.org/user_avatar/community.metabrainz.org/outsidecontext/32/90_2.png) [@outsidecontext](https://community.metabrainz.org/u/outsidecontext)\
**Post date:** [October 8, 2020, 2:02pm UTC](https://community.metabrainz.org/t/sorting-in-different-folders-by-file-type/498269/2 "2020-10-08T14:02:13Z")

</div>

That’s pretty much the default script. So the default will give you albumartist/album/tracknumber tracktitle

```auto
$if2(%albumartist%,%artist%)/
$if(%albumartist%,%album%/,)
$if($gt(%totaldiscs%,1),%discnumber%-,)$if($and(%albumartist%,%tracknumber%),$num(%tracknumber%,2) ,)$if(%_multiartist%,%artist% - ,)%title%

```

It will also add the discnumber if it is more than one. It has some other specialities, such as creating the album folder only if there is an album artist, and setting the artist name in front of the track title if the album contains tracks from different artists. If you don’t want this all and really just albumartist/album/tracktitle then you could reduce it to:

```auto
$if2(%albumartist%,%artist%)/%album%/%title%

```

Nice and simple. Now for the file extension there is a variable `%_extension%`. To have a folder with the extension name just add it to the beginning of the script separated by a slash for the folder separator:

```auto
%_extension%/$if2(%albumartist%,%artist%)/%album%/%title%

```

---

<div class="post-metadata">

**Author:** ![rdswift](https://community.metabrainz.org/user_avatar/community.metabrainz.org/rdswift/32/7032_2.png) [@rdswift](https://community.metabrainz.org/u/rdswift)\
**Post date:** [October 8, 2020, 3:38pm UTC](https://community.metabrainz.org/t/sorting-in-different-folders-by-file-type/498269/3 "2020-10-08T15:38:11Z")

</div>

> [@outsidecontext](#):
>
> `%_extension%/$if2(%albumartist%,%artist%)/%album%/%title%`

For consistency, I would probably write this as:

`$upper(%_extension%)/$if2(%albumartist%,%artist%)/%album%/%title%`

to avoid potentially creating multiple directories on case-sensitive file systems.

---

<div class="post-metadata">

**Author:** ![tip32a](https://community.metabrainz.org/letter_avatar_proxy/v4/letter/t/bbce88/32.png) [@tip32a](https://community.metabrainz.org/u/tip32a)\
**Post date:** [October 9, 2020, 10:14am UTC](https://community.metabrainz.org/t/sorting-in-different-folders-by-file-type/498269/4 "2020-10-09T10:14:47Z")

</div>

Thank you both. At the moment I have a greater problem. The program crashes often. With the default script.

---

<div class="post-metadata">

**Author:** ![outsidecontext](https://community.metabrainz.org/user_avatar/community.metabrainz.org/outsidecontext/32/90_2.png) [@outsidecontext](https://community.metabrainz.org/u/outsidecontext)\
**Post date:** [October 11, 2020, 3:45pm UTC](https://community.metabrainz.org/t/sorting-in-different-folders-by-file-type/498269/5 "2020-10-11T15:45:16Z")

</div>

Can you be more specific when exactly it is crashing?

We are also preparing the next release of Picard and have fixed a couple of potential crashes there. I have uploaded a current development build on:

[https://s3.eu-central-1.amazonaws.com/artifacts.picard.uploadedlobster.com/picard-setup-2.5.0.dev2.exe](https://s3.eu-central-1.amazonaws.com/artifacts.picard.uploadedlobster.com/picard-setup-2.5.0.dev2.exe)

Could you check whether this solved your issue?

If this does not solve you issue, could you report details on a crash by running Picard from command line with the “-d” command line option, make the application crash and post the output on the command line here?

If you are on Windows, make sure you have installed Picard using the installer. Then open a Windows command prompt and start Picard with:

```
"C:\Program Files\MusicBrainz Picard\picard.exe" -d

```

It would be great if we could address the crash your experience in time for the next release.

---

<div class="post-metadata">

**Author:** ![tip32a](https://community.metabrainz.org/letter_avatar_proxy/v4/letter/t/bbce88/32.png) [@tip32a](https://community.metabrainz.org/u/tip32a)\
**Post date:** [October 11, 2020, 4:06pm UTC](https://community.metabrainz.org/t/sorting-in-different-folders-by-file-type/498269/6 "2020-10-11T16:06:40Z")

</div>

I will gladly. However my personal time is very limited. It is why I didn’t sit learn the script language. It may be a few days before I can post again.

Tom

---

<div class="post-metadata">

**Author:** ![BlakeMulligan](https://community.metabrainz.org/letter_avatar_proxy/v4/letter/b/839c29/32.png) [@BlakeMulligan](https://community.metabrainz.org/u/BlakeMulligan)\
**Post date:** [December 1, 2022, 11:17am UTC](https://community.metabrainz.org/t/sorting-in-different-folders-by-file-type/498269/7 "2022-12-01T11:17:13Z")

</div>

I apologize if this is covered somewhere and I missed it. I’m using your script and would like to have it sort output so for an album with multiple file types they sort into separate folders. When I run your script it lumps all the tracks into one folder by album name.

my desired output would be

artist/album/flac/ (all flac files for that album)  
artist/album/mp3/ (all mp3 files for that album)

This is primarily to easily separate the mp3. I would also be okay with a sort that split collection to:

Flac/artist/album/tracks  
MP3/artist/album/tracks

MP3 files are used for mobile devices only. Which allows separate collection. Thanks

---

<div class="post-metadata">

**Author:** ![outsidecontext](https://community.metabrainz.org/user_avatar/community.metabrainz.org/outsidecontext/32/90_2.png) [@outsidecontext](https://community.metabrainz.org/u/outsidecontext)\
**Post date:** [December 1, 2022, 2:30pm UTC](https://community.metabrainz.org/t/sorting-in-different-folders-by-file-type/498269/8 "2022-12-01T14:30:22Z")

</div>

The `%_extension%` variable used in the script is supposed to hold the file extension and is used in the above scripts to create separate folders. Can you maybe share the exact script you are using?

---

<div class="post-metadata">

**Author:** ![BlakeMulligan](https://community.metabrainz.org/letter_avatar_proxy/v4/letter/b/839c29/32.png) [@BlakeMulligan](https://community.metabrainz.org/u/BlakeMulligan)\
**Post date:** [December 1, 2022, 4:40pm UTC](https://community.metabrainz.org/t/sorting-in-different-folders-by-file-type/498269/9 "2022-12-01T16:40:34Z")

</div>

I’m using RDSwift’s script. I ran it on a test folder that has both the flac and mp3 together. It saved to folder for album name. I imagine an if statement will allow the output if the `%_extension%` variable can properly detect the file type. I looked through the script briefly and can look closer later on to see if there is a place to logically put such an “if” … Thanks

---

<div class="post-metadata">

**Author:** ![rdswift](https://community.metabrainz.org/user_avatar/community.metabrainz.org/rdswift/32/7032_2.png) [@rdswift](https://community.metabrainz.org/u/rdswift)\
**Post date:** [December 1, 2022, 4:54pm UTC](https://community.metabrainz.org/t/sorting-in-different-folders-by-file-type/498269/10 "2022-12-01T16:54:27Z")

</div>

If you’re talking about the script in my [GitHub repo](https://github.com/rdswift/picard-scripts/blob/master/naming_scripts/Bob_Swift/Bob%20Swift's%20Naming%20Script.pts), then you should be able to maintain separate paths for each format by changing Line 446 to:

```taggerscript
$rreplace($upper(%_extension%)/%_nFilePath%/%_nFileName%,[?*:\\_]+,_)

```

Note that I haven’t tested this, but it should work (or at least point you in the right direction).

---

<div class="post-metadata">

**Author:** ![BlakeMulligan](https://community.metabrainz.org/letter_avatar_proxy/v4/letter/b/839c29/32.png) [@BlakeMulligan](https://community.metabrainz.org/u/BlakeMulligan)\
**Post date:** [December 1, 2022, 5:07pm UTC](https://community.metabrainz.org/t/sorting-in-different-folders-by-file-type/498269/11 "2022-12-01T17:07:27Z")

</div>

yes that is the script. I will give this a try. Thanks!

---

<div class="post-metadata">

**Author:** ![BlakeMulligan](https://community.metabrainz.org/letter_avatar_proxy/v4/letter/b/839c29/32.png) [@BlakeMulligan](https://community.metabrainz.org/u/BlakeMulligan)\
**Post date:** [December 1, 2022, 5:18pm UTC](https://community.metabrainz.org/t/sorting-in-different-folders-by-file-type/498269/12 "2022-12-01T17:18:24Z")

</div>

> [@rdswift](#):
>
> `$rreplace($upper(%_extension%)/%_nFilePath%/%_nFileName%,[?*:\\_]+,_)`

Unfortunately same result dropped it into one folder for the album. I will dig into this later on and let you know if I get it figured out.

---

<div class="post-metadata">

**Author:** ![rdswift](https://community.metabrainz.org/user_avatar/community.metabrainz.org/rdswift/32/7032_2.png) [@rdswift](https://community.metabrainz.org/u/rdswift)\
**Post date:** [December 1, 2022, 5:30pm UTC](https://community.metabrainz.org/t/sorting-in-different-folders-by-file-type/498269/13 "2022-12-01T17:30:17Z")

</div>

That’s odd because it should have created a new full path beginning with the file extension in upper case (with the initial and other stuff below that).

Alternately, you could try adding a new line after Line 411 containing:

```taggerscript
$set(_nFilePath,%_nFilePath%/$upper(%_extension%))

```

This should create the different extension directories under the album directory.

---

<div class="post-metadata">

**Author:** ![BlakeMulligan](https://community.metabrainz.org/letter_avatar_proxy/v4/letter/b/839c29/32.png) [@BlakeMulligan](https://community.metabrainz.org/u/BlakeMulligan)\
**Post date:** [December 1, 2022, 5:51pm UTC](https://community.metabrainz.org/t/sorting-in-different-folders-by-file-type/498269/14 "2022-12-01T17:51:21Z")

</div>

> [@rdswift](#):
>
> `$set(_nFilePath,%_nFilePath%/$upper(%_extension%))`

Unfortunately it still dropped into one folder not setting up a sub folder or different folder for file type. I will look at this tonight. I really appreciate your response. It may well be error on my end.

---

<div class="post-metadata">

**Author:** ![rdswift](https://community.metabrainz.org/user_avatar/community.metabrainz.org/rdswift/32/7032_2.png) [@rdswift](https://community.metabrainz.org/u/rdswift)\
**Post date:** [December 1, 2022, 7:15pm UTC](https://community.metabrainz.org/t/sorting-in-different-folders-by-file-type/498269/15 "2022-12-01T19:15:27Z")

</div>

I updated the script on my repo to include an option to put the files in extension-specific subdirectories under the album directory. It definitely works because I tested it here. To enable this option, you need to change Line 87 of the updated script from:

```taggerscript
$set(_aExtension,)

```

to:

```taggerscript
$set(_aExtension,1)

```

---

<div class="post-metadata">

**Author:** ![outsidecontext](https://community.metabrainz.org/user_avatar/community.metabrainz.org/outsidecontext/32/90_2.png) [@outsidecontext](https://community.metabrainz.org/u/outsidecontext)\
**Post date:** [December 1, 2022, 9:36pm UTC](https://community.metabrainz.org/t/sorting-in-different-folders-by-file-type/498269/16 "2022-12-01T21:36:45Z")

</div>

Do you have “move files” enabled?

---

<div class="post-metadata">

**Author:** ![BlakeMulligan](https://community.metabrainz.org/letter_avatar_proxy/v4/letter/b/839c29/32.png) [@BlakeMulligan](https://community.metabrainz.org/u/BlakeMulligan)\
**Post date:** [December 2, 2022, 2:35am UTC](https://community.metabrainz.org/t/sorting-in-different-folders-by-file-type/498269/17 "2022-12-02T02:35:43Z")

</div>

Perfect, amazing that you did the change to your script.

---

<div class="post-metadata">

**Author:** ![rdswift](https://community.metabrainz.org/user_avatar/community.metabrainz.org/rdswift/32/7032_2.png) [@rdswift](https://community.metabrainz.org/u/rdswift)\
**Post date:** [December 2, 2022, 3:52am UTC](https://community.metabrainz.org/t/sorting-in-different-folders-by-file-type/498269/18 "2022-12-02T03:52:22Z")

</div>

Thanks for letting us know that it worked for you. As for the change, it was pretty minor, and perhaps there are others that can use it.

---

<div class="post-metadata">

**Author:** ![BlakeMulligan](https://community.metabrainz.org/letter_avatar_proxy/v4/letter/b/839c29/32.png) [@BlakeMulligan](https://community.metabrainz.org/u/BlakeMulligan)\
**Post date:** [December 2, 2022, 9:56am UTC](https://community.metabrainz.org/t/sorting-in-different-folders-by-file-type/498269/19 "2022-12-02T09:56:41Z")

</div>

I hate to be a pain one last question related to the output as it moves the files. Is it possible to have the artist listed by first name?

Example: ~ R ~\Reba McEntire {date} album title\ …  
I tried getting it to sort artist by first but I was either in the wrong part of your script or just didn’t get the parameters correct. It would also be helpful to have option for “the” (“The Rolling Stones” … “Rolling Stones”, The … ignore “the” - “Rolling Stones”. I know that dropping “the” is not technically correct.

---

<div class="post-metadata">

**Author:** ![BlakeMulligan](https://community.metabrainz.org/letter_avatar_proxy/v4/letter/b/839c29/32.png) [@BlakeMulligan](https://community.metabrainz.org/u/BlakeMulligan)\
**Post date:** [December 2, 2022, 9:59am UTC](https://community.metabrainz.org/t/sorting-in-different-folders-by-file-type/498269/20 "2022-12-02T09:59:45Z")

</div>

I did but I realized later I made an error and probably didn’t save the change - very difficult day with medical news for my sister and I shouldn’t have been trying to sort this at same time. I suspect his original answer would have worked without need to amend the script as he did.

[Next page](https://community.metabrainz.org/t/sorting-in-different-folders-by-file-type/498269.md?page=2)
