Click here to Skip to main content
Click here to Skip to main content

FAT-32 Sorter

By , 21 Jul 2010
 

Contents

Introduction

In this article, I'll present a small utility that I wrote to sort the files' table for the FAT32 file system. This utility solves the problem that most FAT32-based music players have - Undefined songs sorting. This problem occured in my car's stereo system, and in Amazon's Kindle.

Seeking and resolving the problem was a nice journey, that involved researching and programming down to the low level of the flash drive's file system. I've supplied most of the background knowledge needed to understand this article, but you need some prior knowledge in C++ programming in order to understand the code.

A Random Stranger

So this is how this whole thing started:
Not a long time ago, I bought a stereo system for my car - Pioneer DEH-P4050UB. The main advantage in this product, was the ability to read music from a USB flash drive. So far so good, but I like listening to my music in a random order, and I've noticed something disturbing — on random mode, if the song ends, then the next one will be randomly picked. HOWEVER, if I switch to the next song by myself (Using the Fast-Forward button) then the next song will be the SEQUENTIAL song after the current one.

After thinking about it for a while, I remembered that there's a "Next Directory" button, so I can just switch to the next album anytime. That way, I can put some very short audio file, which will be the first in the album in an alphabetical order (i.e. 11111udi.mp3), and after playing this file (for a very short time), the next song will be randomly picked! That way I can get a random song with only one click!

Unfortunately, the gods of Pioneer had different plans for me. I've noticed that, for some reason, the new short audio file was not the first one to be played in the directory. After that, I've also noticed that lots of my songs in the albums are not played in an alphabetical order, which tells me that there's some mysterious order that needs to be revealed.

FAT32 in a Nutshell

Before getting our hands dirty, let's get to know a little about the file system that my USB was formatted to (as required by the stereo system). Learning all the aspects of FAT32, can take a long time. I'll hereby give some key features of this file system, that'll help you understand the solution to my problem.

Structure

Not getting into specifics, the file system begins with the boot sector, which holds all the parameters' values for the file system (i.e sectors' size, as explained later). The FAT32 file system layout is described in this picture:

fat32_layout.png

Taken from the article "Paul's 8051's code library: Understanding FAT32 Filesystems."

Let's discuss the important things:

  • Volume ID (Boot Sector) - Holds some vital information about the current partition, like the size of a cluster and the size of the FAT Tables in the file system.
  • Clusters - In order to navigate conveniently through the file system, all the bits are divided into larger groups of data called "Sectors" and "Clusters." Sectors are the smallest group, their size is fixed and stated in the Boot Sector (Currently - it's 512 Bytes). Since Sectors are big, but not big enough — we use the Clusters, which are nothing more than just a collection of sectors. The official number of sectors per cluster is also defined in the boot sector (Usually, Cluster = 8 Sectors). From now on — we'll just work with the clusters as our smallest data unit.
  • FAT Tables - One cluster cannot always be enough to hold the file's data. A single cluster can hold 4096KB of data (8 Sectors * 512 Bytes Each). So in order to spread the data in different clusters, we need a way to track them down later on. That's why we have the FAT Tables! Given some cluster number, we can go to one of the FAT tables and calculate the next cluster in the chain, and then the next one, and the next one...up until reaching some flag that stops this chain. There are TWO tables, that backup each other in case one table gets corrupted for some reason.

Dir Entries

Every file and folder in the file system, is represented in one or more entries called "Dir Entries". The Dir Entry holds all the information about the file/folder, as can be seen in the next figure:

Dir_entry.png

  • The "First Cluster Number" (DIR_FstClusLO and DIR_FstClustHI) is the cluster where we can find the DATA itself for the File/Folder.
  • File's data can vary from Text to Binary file of some image or even executable.
  • Folder's data contains a list of all its files and sub-folders. Each entry in that list is a "Dir Entry", that leads to its own data cluster

The "DIR_Name" holds the file/folder's name. You might ask yourself: "Only 11 bytes for a "filename" field?! But I can use longer file names in FAT32!" Well, You're right. There's a little "trick" to write longer file names, using other type of enrtries called "Long File Name Entries" (LFN Entries):

LDir_entry.png

Every file that has a long file name (more then 11 bytes) or a name in some different language, that requires UNICODE, is using 1 or more LFN entries. Each entry has a few bytes to hold the file's name. Combining all the LFN entries we get the long file name!

Next to all the file's LFN entries, there's a short dir entry, which holds some vital information about the file, and helps support older operating systems, which are not familier with the LFN entries. So actually, a file that has a long name uses more then one Dir Entry. It uses LFN Entries as much as it need, and one short Dir Entry for backward compatibility.

In the next figure, we can see entries for 2 files: "udini01.txt" and "long name for a file.txt". The first one has a short name, therefore it uses only one short dir entry. But, the second file needs more entries, so it uses 2 LFN entries (Marked in #1 and #2), and after that — a backwards compatibility short entry, with a truncated file name.

entries_types.png

This screen shot is from the program "DiskExplorer for FAT," which gives you a great look into your file system to see how everything is stored in there. You can download it here

If you look closely, you can even see some other attributes of the files. For instance, the data of these files is stored in cluster number 3 and 4 for "udini.txt" and "long name for a file.txt" respectively.

Seeking the Problem

After getting some background knowledge, let's get back to our issue: My songs were ordered in some mysterious way, while playing in my car stereo. After playing with some OS commands to read directories, I've found that the FindFirstFile and FindNextFile functions, return the same files' order as my car stereo. So what does dictate their order?

Regarding the way these functions order the files, the MSDN says: "The order in which the search returns the files, such as alphabetical order, is not guaranteed, and is dependent on the file system."
Meaning — I need to find the exact cause in the file system for this order, and sort it in the device itself. The alternative is to change the firmware of my car stereo, to sort the files in each directory it reads, and that's just a lot of work.

Let's see some files, and the way they're ordered in the Windows Explorer, and inside the FAT32 file system.

The files in the windows explorer (Sorted):

folder_files.png

The files in the File System's files table (Unsorted):

Unsorted_Files.png

Using the excellent "DiskExplorer for FAT," I could see the order of the files in the Files Table inside the file system.

Let's concentrate on the order of the numbers (marked in circles). We can see that the order in the file system is different from the order in the Windows Explorer's window. I wrote a small C program that uses the FindFirstFile/FindNextFile functions (presented above), and it gave me the order:

  • Riverside - 04 - left out
  • Riverside - 02 - driven to destruction
  • Riverside - 05 - hybrid times
  • Riverside - 03 - egoist hedonist
  • Riverside - 01 - hyperactive.mp3

Which is exactly the same order as in the Files Table, concluding that the functions return the files in the order they are stored in the Files Table! This order is caused by the OS that copies the files to the device, which cannot be controlled easily. If I'll try to copy the files one at a time in the correct order, then the Files Table will be sorted in the same way. Of course that's hardly a solution for a USB flash drive with a few hundreds of songs in it... What should we do?! Let's see...

Sorting Things Out

In order to get the music played in the right order (Alphabetically and NOT the order you copy them!), I had to write a utility that sorts the Dir Entries in an alphabetical order.

The Entries Entities

There's a lot of information when you read the file system. Every file or folder can have more than 10 dir entries (Thanks to the LFN support), and we need to organize the data properly. Here's a simplified class diagram of the classes I used to maintain the data:

CEntry.png

  • CEntry - That's the abstract father of all the entry types in the system. Each file or folder in the file system is stored in one of its sub-classes. The m_data data member holds the 32 bytes of the short entry (And not the LFN entry, if there is one). The m_chainedLFNEntry is a dynamic array with all the LFN entries for this file/folder. The getName() returns the name of the file/folder. If there are LFN entries, then the file name's bytes are collected from all the LFN entries in the m_chainedLFNEntries, and combined to one string that is the long file name itself.
  • CFileEntry - Represents a single file in the system.
  • CSpecialEntry - This is a special entry that represents some entries that are not files or folders. Such entries are the volume label entry (containing the device's label name). Other special entries are the "." and the ".." entries, which are just "pointers" to the current folder and to the father folder of the current folder, respectively.
  • CFolderEntry - This entry represents a folder, which contains all of its files inside it (in the m_files), and all of its sub-folders (in the m_folders). It also has the sorting functionality, which is the main feature of this utility.

CFileSystem

CFileSystem.png

This class is a high level API to the file system. It uses the CVolumeAccess class, which will be described later, for the low level access to the device. It gives a more simplified API to perform the main function on the file system. The class holds the m_rootDir data member, which is the root entity (CRootEntry) that holds the files and folders' tree, representing the entire file system. The function initFDT() is in charge of populating the data. The sort() function is used to sort all the files and folders in the file system.

CVolumeAccess

All the low level access for the file system is done by this utility class.

CVolumeAccess.png

This class is a singleton. When initialized, it sends a command to the device to lock and dismount it (dismounting the device, when a low level access is needed, is imperative since Windows Vista). Also in the initialization stage, it loads the FAT tables to the memory (the m_FAT1Data\m_FAT2Data data members), so we can use it later on to find all the clusters chained one after another. In the next code snippet, we see how to read the data from a random cluster chain:

DWORD dwNextClusterNum = aStartClusterNum;
DWORD dwNumClustersRead = 0;

// As long we didn't reach the end of the chain
while (dwNextClusterNum != FAT_END_CHAIN)
{
    // Reads from the device the whole cluster, and puts it in the next free
    // position in the buffer 
	if (!readBytesFromDeviceCluster(aoChainedClustersData+(
             dwNumClustersRead*m_clusterSizeBytes), 
	    m_clusterSizeBytes,
	    dwNextClusterNum))
	    {
		    return false;
		}
	dwNextClusterNum = m_FAT1Data[dwNextClusterNum];
	++dwNumClustersRead;
}

The above code is taken from the readChainedClusters() function. The aoChainedClustersData buffer is filled with the data from the clusters's chain.

The clusters, in a chain, are not necessarily sequential (actually, most likely they aren't). For example, a clusters' chain could be the series of the clusters: 4, 5, 8, 23, 87, 432. The aStartClusterNum holds the number of the first cluster in the chain. That number is stated in the file\folders's Dir Entry (See the "FAT32 Byte Directory Entry Structure" figure above).

The dwNextClusterNum variable, is used to store the next number. If we'll mark the current cluster's number with X, The next clusters number is the number located in the Xth index on the FAT table. That explains the line dwNextClusterNum = m_FAT1Data[dwNextClusterNum];

The first FAT table, m_FAT1Data, was arbitrary chosen over m_FAT2Data, since both tables are the same. This loop keeps on going until the next cluster has reached the FAT_END_CHAIN constant, which marks the end of the chain.

Using the readChainedClusters(), we can get all the folders' files/sub-folders list, and so we can navigate through the file system easily, populating the data into appropriate CEntry objects. The Root folder will be saved in the CFileSystem object, and all of it's sub-folders will be sorted, recursively, by the sort() function.

Working with the Utility

What can it do?

menu.png

The Utility that I wrote, provides the following features:

  • Dump Files table (Backup) - Saving the files table to a file, from which we can recover later in case there's some error. The backup file name will always be "dirs.dat".
  • Load Files table (Recover) - Load a files table from a file previously saved. The data will be loaded from a file named "dirs.dat" ONLY, located in the same directory as the utility.
  • Export Folders List - Stores a list of all the files and folders on the device, as they're ordered in the files' table, in a file called "Files List.txt". The data will be printed in a tree form, and each directory will be in the format: [Serial Num|Name]. The serial num is just a counter of all the folders in the device. It's useful for me, because my car stereo shows me only the folders' serial numbers. With a list like that, I can easily search for some particular album. I hope that other people will also find it usefull for their purposes.

Here's an example for such list

FilesList.png

Sort the FAT Table - This is the main function. This command sorts the files table, and flushes the sorted table back to the device. NOTE: This action automatically saves a backup file, so there's no need to use the backup prior to that. If there's a problem with the sorted table, you can always recover by renaming the created backup file to "dirs.dat", and use the "Recover" option in the menu.

How to begin?

In order to sort the files table, you need to choose the drive on which the device is mounted to. Use option 1 in the main menu, and enter the correct drive letter.

option1.png

Note that the device needs to be connected and mounted to this drive. It also needs to be formatted to FAT32 file system. Otherwise - Any operation on the device will fail.

After that, you can use the sort option (Number 5 in the menu), which will cause the utility to load the files' table, sort it, and flush it back to the device.

sorting.png

Note: The "???" are just some folders in Hebrew characters that this DOS window can't display, but the sorting process won't get affected nor harmed by this.

This process takes about 1-2 minutes for a 16GB USB Drive. It's pretty safe, because you can always recover the table from the backup file. To see the changes, you can print the FilesList before and after the sorting process (don't forget to rename it after each action, or else it'll get overwritten!)

With this utility you can easily order your songs in the right order as it should be! This solution is also applicable to the Amazon's Kindle (Also plays all the songs in the order they were loaded, as described Here)- Just use this utility to sort the kindle's files table (Which is FAT32) and Voila! All the songs are alphabetically ordered..

Resources

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Udi Cohen
Software Developer Any.DO
Israel Israel
Member
Over 10 years of programming experience, Mainly in C++ and Java.
Lead developer of several web sites and developed applications for Windows, Linux and Mobile devices.
Currently working as an Android developer for Any.DO.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralExcellent!memberMember 1005724416 May '13 - 5:14 
Great work! Where linux fatsort utility failed, your program just did what I needed. Think they should throw out that fatsort thing, and port in your instead   Big Grin | :-D
GeneralMy vote of 5memberMatt Fichtenbaum5 Apr '13 - 14:28 
Performs a useful function well.
 
It would be nice to be able to sort folders but leave the file order unchanged.
GeneralMy vote of 5memberPaul Tew4 Dec '12 - 3:54 
Thanks for the article/code - I tried messing around with folder created dates etc with no luck previously
GeneralMy vote of 5membergndnet7 Nov '12 - 14:06 
awesome, thanks
Questiondoesn't seem to rename SFN (shortnames) alphabetically [modified]membersubatomicglue21 Sep '12 - 17:52 
in windows7 SP1...
I ran this on my USB stick in drive "J:"... it showed every folder, scrolling by, hundreds of them... it seemed to mount/lock just fine. but viewing the directories with dir /x after the sort, none of the shortnames were renamed, still out of order, and in my car the USB stick files are still out of order by long name (file - 09 - whatever.mp3 is first while 01 is later...) in the same ordering that the shortnames are named in.
 
ideas?
 
UPDATE: looks like the order in the FAT table is actually coming out alphebetical, using the "4. Export Folders" function, however, the shortnames are still the same (unchanged). Maybe your pioneer uses the FAT ordering, but my Kenwood is actually looking at the SFN alphebetical order, (my Kenwood doesn't care how files are ordered in the FAT)... Looking like I need SFN ordering/renaming capability...
http://www.subatomicglue.com


modified 22 Sep '12 - 0:52.

AnswerRe: doesn't seem to rename SFN (shortnames) alphabeticallymembersubatomicglue22 Sep '12 - 3:44 
I started looking into the code, to implement ShortName renaming in an incrementing/alphebetical way.
 
Trying this code:
void CFolderEntry::sortEntries()
{
    // Sort the files and folders entries
    // Note - When there're 2 identical entries in the vector - the sort function throws an assert error
    // In our case it's OK, cause the file\folder name is unique inside some folder
    sort(m_files.begin(), m_files.end(), compareEntries);
    sort(m_folders.begin(), m_folders.end(), compareEntries);
 
    // Iterate all the non-deleted sub-folders, and sort them as well
    for (vector<CFolderEntry*>::iterator foldersIter = m_folders.begin();
        foldersIter != m_folders.end();
        foldersIter++)
    {
        (*foldersIter)->sortEntries();
    }
 
    int inc = 1;
    for (vector<CFileEntry*>::iterator filesIter = m_files.begin();
        filesIter != m_files.end();
        filesIter++)
    {
        char newsfn[12] = "";
        sprintf_s( newsfn, "%011d", inc );
        ++inc;
        (*filesIter)->setShortName( newsfn );
    }
}
 
void CEntry::setShortName( const char* n )
{
    if (m_numOfEntryElements)
    {
        int sizetocopy = (sizeof(m_data.DIR_Name)) < (strlen(n)+1) ? (sizeof(m_data.DIR_Name)) : (strlen(n)+1);
        memcpy( m_data.DIR_Name, n, sizetocopy );
    }
}
 
Problem is that it actually renames the files on the USB stick to those short names, 00000000.001, 00000000.002, 00000000.003, etc...., somehow the LFN names are going away...
 
And for some reason, one (just one) of the filename LFN's are staying:
 
J:\00000000.001>dir /x
 Volume in drive J is CAR16GB
 Volume Serial Number is BEB2-A9C0
 
 Directory of J:\00000000.001
 
09/22/2012  08:33 AM    <DIR>                       .
09/22/2012  08:33 AM    <DIR>                       ..
03/11/2012  11:55 AM               621              00000000.001
02/24/2007  04:02 AM           168,097              00000000.002
03/12/2012  08:04 PM            34,212              00000000.003
06/02/2012  10:38 AM         2,608,170              00000000.004
06/02/2012  10:38 AM         7,875,109              00000000.005
06/02/2012  10:38 AM         3,174,972              00000000.006
06/02/2012  10:38 AM         5,113,400 00000000.007 aphex twin - donkeyrhubarb.mp3
06/02/2012  10:38 AM         3,339,988              00000000.008
06/02/2012  10:38 AM         7,572,321              00000000.009
06/02/2012  10:38 AM         3,148,933              00000000.010
06/02/2012  10:38 AM         6,145,672              00000000.011
06/02/2012  10:38 AM         9,900,729              00000000.012
06/02/2012  10:38 AM         3,471,228              00000000.013
06/02/2012  10:38 AM         7,662,983              00000000.014
02/24/2007  04:02 AM           168,097              00000000.015
02/24/2007  04:01 AM           229,629              00000000.016
02/24/2007  03:56 AM           333,765              00000000.017
02/24/2007  03:57 AM           127,002              00000000.018
05/24/2007  10:32 PM           228,941              00000000.019
02/24/2007  03:34 AM           152,966              00000000.020
05/24/2007  03:40 AM             1,131              00000000.021
              21 File(s)     61,457,966 bytes
               2 Dir(s)  15,959,343,104 bytes free
 

I'm guessing it isn't as simple as just setting the m_data.DIR_Name... But looking at the code, it isn't obvious to me why the LFN would go away just by changing DIR_Name.
 
Ideas?
http://www.subatomicglue.com

QuestionFolder Numbering only when audio files are in itmemberkubax835 Jun '12 - 4:09 
Hi,
 
First of all, this programm realy rocks. I've searched something like this for month.
 
I was very excited when i noticed that you allready did an export to text file, so i doesn't have to.
 
What i wanted to have (besides the sorting) was a list of the content of my SD card with numbering like my car radio would number it.
 
Let me explain this in detail:
 
I've got a 32 Gb SD card with all my music in it. So it's realy hard to search for a special Artist. Let's say i'd like to hear the album "Road to Riun" from "The Ramones". Without the Album number from my car radio it's realy hard to search.
 
As of my SD card is sorted like this:
 
ROOT
|--- [1|7th Child]
|------ [2|7th Child-Bleed to Death]
|--------- {1} 01.7th Child-Bleed to Death.mp3
|--------- {2} 02.7th Child-Bleed to Death.mp3
|--------- {3} 03.7th Child-Bleed to Death.mp3
|--------- {4} 04.7th Child-Bleed to Death.mp3
|------ {5} TEST2   MP3
|--- [3|All Shall Perish]
|------ [4|Hate . Malice . Revenge]
|--------- {6} 01 deconstruction.mp3
|--------- {7} 02 laid to rest.mp3
|--------- {8} 03 our own grave.mp3
|--------- {9} 04 the spreading disease.mp3
|--------- {10} 05 sever the memory.mp3
|--------- {11} 06 for far too long....mp3
|--------- {12} 07 never ending war.mp3
|--------- {13} 08 herding the brainwashed.mp3
|------ [5|the Price Of Existence]
|--------- {14} 01 eradication.mp3
|--------- {15} 02 wage slaves.mp3
|--------- {16} 03 the day of justice.mp3
|--------- {17} 04 there is no business to be done on a dead planet.mp3
|--------- {18} 05 better living through catastrophe.mp3
|--------- {19} 06 prisoner of war.mp3
|--------- {20} 07 greyson.mp3
|--------- {21} 08 we hold these truths....mp3
|--------- {22} 09 the true beast.mp3
|--------- {23} 10 promises.mp3
|--------- {24} 11 the last relapse.mp3
 
As you can see i allready did a numbering for the mp3 files itself (only counting mp3 and wav files) so it's allready possible to say "Let's hear The Bard's Song" from "Blind Guardian" in this case it would be Song Number 199.
 
But (and finaly here is my problem) if i'd like to listen to the Album mentioned above the exported list tells it's album number 285 but indeed it is not, because of the artist folders counting to in the export (wich my car radio doesn't count because there are no mp3 files inside)
 
I've allready tried to do it on my own, but it looks like i'm to unexpired in programming :/
 
Could you please help me, or even give me a hint how to accomplish it?
 
Have a nice day,
Kubax
AnswerRe: Folder Numbering only when audio files are in itmemberUdi Cohen5 Jun '12 - 4:20 
so you're basically saying that the folder w/o mp3 files are ruining the
count?
if so - just put an mp3 file that plays silence for a second, that way the
folder will be counted.
 
did i understand you correctly?
GeneralRe: Folder Numbering only when audio files are in itmemberkubax835 Jun '12 - 5:07 
Ok, this works for the albums, but now my tracks are all increased by 1 (i think because of the fact the programm sorts files like this:
 
ROOT
|--- [1|7th Child]
|------ [2|7th Child-Bleed to Death]
|--------- {1} 01.7th Child-Bleed to Death.mp3
|--------- {2} 02.7th Child-Bleed to Death.mp3
|--------- {3} 03.7th Child-Bleed to Death.mp3
|--------- {4} 04.7th Child-Bleed to Death.mp3
|------ {5} NO_AUDIOMP3
|--- [3|All Shall Perish]
|------ [4|Hate . Malice . Revenge]
|--------- {6} 01 deconstruction.mp3
|--------- {7} 02 laid to rest.mp3
|--------- {8} 03 our own grave.mp3
|--------- {9} 04 the spreading disease.mp3
|--------- {10} 05 sever the memory.mp3
|--------- {11} 06 for far too long....mp3
|--------- {12} 07 never ending war.mp3
|--------- {13} 08 herding the brainwashed.mp3
|------ [5|the Price Of Existence]
|--------- {14} 01 eradication.mp3
|--------- {15} 02 wage slaves.mp3
|--------- {16} 03 the day of justice.mp3
|--------- {17} 04 there is no business to be done on a dead planet.mp3
|--------- {18} 05 better living through catastrophe.mp3
|--------- {19} 06 prisoner of war.mp3
|--------- {20} 07 greyson.mp3
|--------- {21} 08 we hold these truths....mp3
|--------- {22} 09 the true beast.mp3
|--------- {23} 10 promises.mp3
|--------- {24} 11 the last relapse.mp3
|------ {25} NO_AUDIOMP3
 
but my car radio sorts like this.
 
ROOT
|--- [1|7th Child]
|------ {1} NO_AUDIOMP3
|------ [2|7th Child-Bleed to Death]
|--------- {2} 01.7th Child-Bleed to Death.mp3
|--------- {3} 02.7th Child-Bleed to Death.mp3
|--------- {4} 03.7th Child-Bleed to Death.mp3
|--------- {5} 04.7th Child-Bleed to Death.mp3
|--- [3|All Shall Perish]
|------ {6} NO_AUDIOMP3
|------ [4|Hate . Malice . Revenge]
|--------- {7} 01 deconstruction.mp3
|--------- {8} 02 laid to rest.mp3
|--------- {9} 03 our own grave.mp3
|--------- {10} 04 the spreading disease.mp3
|--------- {11} 05 sever the memory.mp3
|--------- {12} 06 for far too long....mp3
|--------- {13} 07 never ending war.mp3
|--------- {14} 08 herding the brainwashed.mp3
|------ [5|the Price Of Existence]
|--------- {15} 01 eradication.mp3
|--------- {16} 02 wage slaves.mp3
|--------- {17} 03 the day of justice.mp3
|--------- {18} 04 there is no business to be done on a dead planet.mp3
|--------- {19} 05 better living through catastrophe.mp3
|--------- {20} 06 prisoner of war.mp3
|--------- {21} 07 greyson.mp3
|--------- {22} 08 we hold these truths....mp3
|--------- {23} 09 the true beast.mp3
|--------- {24} 10 promises.mp3
|--------- {25} 11 the last relapse.mp3
 
any chance to get rid of this?
QuestionRandomize instead of sortmemberrramirezg30 Mar '12 - 6:35 
I've a similar problem with my car stereo (an obscure chinese brand) but no C++ programming skills to modify your code. I like to play songs in random order but the random algorithm seems to be broken because it plays the songs in the same "not-so-random" order every time, so I would like to ask you to include an option to randomize the order of the files. Any chance you could implement it ? thanks
QuestionThank YoumemberAndySuk12 Mar '12 - 1:10 
Not just for the utility but also for the explanations.
 
Andy
GeneralFAT hard linksmemberkolobok_3812 Oct '11 - 20:51 
Hi!
I'm just finished project based on this source code.
It is mainly intended to randomize playing files in car audio within whole drive or folders including sub-folders.
Maybe it would be useful in other stuff.
So, with additional function you can make hard links to files in hard-named folders, containing links to all files in subfolders.
 
If author still reading this thread: I would like to send you my sources. If you deems it necessary, may be you post it here.
 
Sorry for my bad bad English )
GeneralRe: FAT hard linksmemberUdi Cohen23 Oct '11 - 10:45 
Hey,
 
I'm glad that you found my code useful, and I would be glad to see what you did there to implement that feature. In my car stereo I can actually randomly play all the songs in the whole drive, but not in a single folder with it's sub folders.
 
Udi.
GeneralRe: FAT hard linksmemberkolobok_3830 Oct '11 - 15:17 
I can't find your e-mail...
Here it is (source + binaries):
http://depositfiles.com/files/5afxx0bl4
pass: FAT
QuestionThanks for sharing... Great tool, good code :)memberrbid16 Sep '11 - 3:11 
It was easy to understand what you do, thanks for sharing.
--- Ricky Marek (From Atzmon, Israel)
-- Ricky Marek (AKA: rbid)
-- "Things are only impossible until they are not" --- Jean-Luc Picard
 
My articles
 

QuestionThank youmemberMember 823958015 Sep '11 - 1:18 
Tried FATsorter and YAFS. Neither worked for me. Then found this. Simple and does just what I need... puts all the episodes of an MP3 audio book into order.
 
Cheers!
GeneralGreat WorkmemberMatthias Günther5 Jun '11 - 5:03 
First of all please excuse my bad English.
 
I've been look for such a feature for a long time and I'm glad, that I found a begin for my problem now, to place Dirs and Files in any sort order on a USB drive.
 
I'm not a friend of leading 01 - MP3File.mp3, 02 - MP3File.mp3 a.s.o and I did write a little App for me, which copies the Dirs and MP3s in my sorted order on the stick. But this requieres formatting the stick and copying all unchanged MP3s again.
 
Unfortunately I'm only a VB Hobby developer without any knowledge in C++ and without Visual Studio .Net and so I would like to ask you, if you can provide a Dll, with which the dirs.dat can be generated with the Dirs and Files in any sort order and then this order can be restored on USB Drive.
 
Thanks in advance
 
Matthias
GeneralRe: Great WorkmemberUdi Cohen9 Jul '11 - 13:56 
Hey MAtthias,
 
Glad you liked the article and the demo.
 
What kind of sorting method are you looking for?
GeneralRe: Great WorkmemberMatthias Günther11 Jul '11 - 5:30 
Hi Udi,
 
thanks for your reply.
 
I mean every imaginable sort order. In my special case I add the songs, which should be on stick, from song source TV1 to TV2. In TV2 all song folders and the songs inside this folders can be sorted with Drag'n Drop by user choice.
If the songs are sorted, the new(er) songs were copied to the stick and the odd songs on the stick were deleted. Then I create a TEMP folder on the stick, move all songs of the stick in this TEMP folder and move them back to stick Root in the order like sorted in TV2.
Works fine with my Kenwood, but my Alpine (has no banking mode) has it's own rules, because the the song order inside a folder is ok but the order of the folders is different.
 

It's difficult for me to describe this, so I try it this way
 

I thought, if it would be possible to create a own "Dirs.dat" like
 
Set oFileTable = New FileTable
 
oFileTable.AddSong("RootSong1.mp3")
oFileTable.AddSong("RootSong2.mp3")
 
oFileTable.AddFolder("XYZ")
oFileTable.AddSong("XYZSong1.mp3")
oFileTable.AddSong("XYZSong2.aac")
 
oFileTable.AddFolder("ABC")
oFileTable.AddSong("ABCSong1.aac")
oFileTable.AddSong("ABCSong2.wma")
 
...
...
 
oFileTable.Create("C:\Dirs.dat")
oFileTable.Recover("USBDrive:", "C:\Dirs.dat")
 

it should be also possible, to sort the songs on stick in any imaginable order.
 

Regards
 
Matthias
GeneralMy vote of 1memberMember 79054677 May '11 - 21:12 
maybe it is a good article but the app just crashes and does nothing?
GeneralRe: My vote of 1memberUdi Cohen9 Jul '11 - 13:50 
Hey,
 
Sorry about that.
I tried to play with it a little bit, to find the problem, but I had no luck with that :/
 
Could you please specify the type of error you're getting?
Are you sure you're using a FAT32 formatted stick? And it's not read only?
Also, please make sure you do choose the correct drive in the main screen.
 
If you can - an output of the problem could be great!
 
Thanks,
 
Udi
GeneralPioneermemberhfrmobile3 Apr '11 - 14:35 
Also having a Pioneer device (DVD Player Pioneer DV-310-K DVD-Player with USB) which does not display files/directories sorted on USB sticks (FAT32) and so your utility seems exactly what I am looking for.
 
See above:
 
* Download demo - 15.54 KB
* Download source - 28.71 KB
 

What does "Demo" mean? Demo version? Do I need to compile the project by myself to run the full functionality or does the source code contains "limitations" so that only limited functionality is available (e.g. number of files/dirs etc.)? I am confused ...
MCP, MCTS, MCPD
http://www.hfrmobile.com

GeneralRe: PioneermemberUdi Cohen3 Apr '11 - 21:03 
Hey
The "Demo" means that this is a demonstration to the subject I presented in this article, but the demo utility is fully functional w/o any limitation.
 
I'm glad you've found it useful!
Have fun
 
Udi
GeneralRe: Pioneermemberhfrmobile4 Apr '11 - 0:26 
Hi,
 
thanks for your prompt reply.
 
Just tried it but it crashed:
Screen-shot
 
Here is the output:
Are you sure you want to sort the entire files' table (backup will be saved) [y/n] ? y
Dismounting and Locking the volume...Done!
Boot sector size = 90 Bytes
Error reading from the file 0x57
Reading FATs Data...DONE!
The folder "ROOT" is probably corrupted, because no data was found on this folder
The folder "ROOT" is probably corrupted, because no data was found on this folder
Saving backup of the files table..
The folder "ROOT" is probably corrupted, because no data was found on this folder
Backup Saved to file: 20110404_121936.dat!..
Sorting the files table...
DONE!
Flushing the data for folder ROOT
 
Maybe read-only and/or hidden files are a problem? The USB-Stick uses FAT32 and works fine.
 
Btw, 20110404_121936.dat is 0 Byte in size ...
 
Have a nice day,
Harald-René
MCP, MCTS, MCPD
http://www.hfrmobile.com

GeneralRe: PioneermemberUdi Cohen9 Jul '11 - 13:53 
Hey There,
 
Sorry for the late response.
 
A read-only drive can sure be a problem here Smile | :) please make sure it isn't.
About hidden files - there shouldn't be any problem with that, but if you ruled out the read-only issue, please try to remove all hidden files and try again, let me know if that was the problem.
 
Also, please check that the chosen drive if the drive that you want to sort.
 
Thanks,
 
Udi
GeneralRe: Pioneermemberhfrmobile10 Jul '11 - 0:56 
I get an access denied error now.
 
- using Command Prompt with admin privileges
- drive is accessible and works but:
Are you sure you want to sort the entire files' table (backup will be saved) [y/n] ? y
Dismounting and Locking the volume...Error locking the volume (Error=0x5)
The device is not ready..
 
Using Vista Ultimate (32bit) operating system.
 
--hfrmobile
MCP, MCTS, MCPD
http://www.hfrmobile.com

QuestionCan it work on 2T Disk?membernhchmg15 Dec '10 - 22:42 
I test on 2T Disk Simulator,it simulate a 2TB disk,1024,2048,4096 bytes/secotr,this app can not work.I found it on http://www.2tdisk.com
GeneralAwesomememberNayan Choudhary14 Oct '10 - 8:06 
Thanks man for this great tool! I was facing the same issue with my Pioneer music system in my car.
 
Not only I get to play the music in sorted fashion, but also I gained knowledge of FAT.
 
One question: do I need to sort the USB pen drive every time I add new files?
GeneralRe: AwesomememberUdi Cohen14 Oct '10 - 12:59 
Hey,
 
I'm glad that you enjoyed the article and found the tool useful.
 
Regarding your question - the answer is yes. That's because when you add more songs to the USB device, the OS puts them in the end of the files table, which is not alphabetically sorted.
 
Actually, I just created a command line version of this tool, which lets you send arguments instead of working with the menu. If you'd like - I can post it here for more convenient use.
 
Udi.
GeneralRe: AwesomememberNayan Choudhary18 Oct '10 - 9:02 
Thanks for the answer!
 
I actually also figured it out myself after posting this question. Obviously, the OS doesn't care about the order of the files. Hmm, this could be pain considering I change songs/files every 4-5 days. Smile | :)
 
Regarding params passed to the application, it is very easy and would not like to consume your time as it can be done in very few steps by modifying your code.
 
One thing I would like to state is that: I used latest DiskExplorer and found that while it could read and show the FAT structure and table perfectly before, after sorting, it showed warning/error messages. Seems like sorting renders table in a little inconsistent state. However, OS did not complain (I use Windows 7.) Well, as long as OS doesn't complain, I'm fine. But it was strange to see DiskExplorer throwing messages.
 
Thanks once again for this wonderful tool!
 
Regards
Nayan
GeneralRe: AwesomememberAxelC27 Dec '12 - 11:02 
Hi,
I'm very interested in the command line version, is there a way to get it ?
 
Thanks in advance,
Axel.
GeneralMy vote of 5memberNayan Choudhary14 Oct '10 - 7:48 
I have same problem with my Pioneer car USB player. Thanks to this article, I get better knowledge of the behaviour of my player.
GeneralA great article... just one minor point...memberVB Beast27 Jul '10 - 8:25 
A great article, it explains the FAT32 system very well. I have bookmarked it for reference purposes.
 
I think it might be overkill for sorting the files (unless preserving datestamps is a requirement.)
 
If you write a program to rename the folder, create a new folder with the old folder name and then move the files to the newly created folder then they are sorted into the order that they are moved in. Admittedly this relies on the implementation of the file system, but I see no reason for it not to work.
 
This seem to work on the test application that I wrote in C#, but it will be restricted to what Windows allows you to do with the files.
 
Thank you for the article, I like it.
 
Mark
GeneralRe: A great article... just one minor point...member0liviere30 Aug '10 - 11:26 
Great article; just looked at the C++ source code, well structured! but it is a Win application and I guess it won't compile & run under Linux.
 
Mark wrote> If you write a program to rename the folder, create a new folder with the old folder name and then move the files to the newly created folder then they are sorted into the order that they are moved in.
 
does that mean, if I wanted to sort the files in a fat32 directory under Linux, I'd just use the file manager to create a destination directory, select all files (sort file list in alphabetically order) in the source dir, select all & cut, and paste into the new folder?
GeneralRe: A great article... just one minor point...memberVB Beast31 Aug '10 - 10:06 
That might not work, as it would depend on what order the files were copied in. To have the best chance of working the files would need to be copied one at a time (and even then it has a slight chance of not working).
 
I am not very good at linux, so this may well not work, but if you are feeling rather brave (be carefull with the cp (copy) command at the end!) or know linux better then I do you could try:
 
ls -p source | grep -v "/" | sort -z -f -n | xargs -0 -i cp {} destination
 
This should do a listing of the 'source' directory, pipe that through a grep which removes the subdirectories and pipes that through a sort which is then sent to the cp command to copy the files to the 'destination' directory.
 
I am not sure that the sort is necessary, as the ls might be able to do the sort.
GeneralRe: A great article... just one minor point...memberUdi Cohen1 Sep '10 - 15:16 
Hey Mark,
 
I'm happy that you liked my article, and hope that you'll find it useful in the future.
 
The main reason, at least for me, that you're idea is not good (besides what the other people already said) is that it takes a long time to do that. Think about why I needed this utility (The whole story is right here! in the beginning of the article), so you'll realize that copying 16GB of data every time I'm adding some songs to my USB flash drive - is not a good idea. It'll take a long time and make this whole utility not worthwhile.
 
Thanks for your reply,
 
Udi.
GeneralQuestionmemberMember 213318726 Jul '10 - 11:59 
I just read your article, I think it's great. I was wondering if a little modification to the source code would help me solve a problem I have with a USB Memory stick. I can´t write/delete on the device, I just keep getting a Read Only error and just found out in your article that there is an entry in the directory entry structure that has on its Description "File attributes (like Read-Only)". I'm not familiar with C++ so it's not easy for me to make the modification myself, I would really appreciate some guidance in this...
GeneralRe: QuestionmemberVB Beast27 Jul '10 - 8:29 
Hmm, I would guess that the read only flag is not the problem.
 
Does the memory stick have a switch on it? A lot of them have hardware write protection.
 
To remove the read only attribute in Windows you can right click on the file in Windows Explorer, select Properties from the drop down menu and the read only attribute is near the bottom of the dialog.
GeneralRe: QuestionmemberMember 213318727 Jul '10 - 10:31 
Thanks for your response. No, there is no switch, and the read only attibute isn't the problem either, I just keep getting the error "media is write protected", I've tried many posible solutions I found on the web, none have worked Frown | :( , I even tried to drop the partition in Ubuntu. So my only option by now is try to solve this programatically... or burn the memory stick Laugh | :laugh: , I think this last one is the best option...
GeneralGreat ArticlememberSilvio Reis Jr21 Jul '10 - 9:04 
My vote of 5!

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 21 Jul 2010
Article Copyright 2010 by Udi Cohen
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid