Add Space in Directory Name if it excedes more than 8 characters( FreeRTOS+FAT)

Using an FreeRTOS + FAT library, following the 8.3 format if DirectoryName/FolderName exceeds 8 character then it is truncating the space.
example: fold/Dir Name: Test Folder–> it is showing as "TestFo~"1
but expecting it as “Test F~1”

reading this from SD card

As far as I know, there is no “official” defined method for generating the short (8.3) name for a long file name. Using the space in the short name is problematic, as it says the name must be quoted to be used on command lines, so some algorithms will drop them when shortening the name.

Running in a DOS box:

mkdir "Test Folder"
dir /x
Thu, 05-Sep-24  14:02 <DIR> TESTFO~1  Test Folder

When creating a 8.3 filename that already exists, a number may be added, which can range from “~1” to “~999999” in order to find a unique 8.3 filename.

The space in “Test Folder” has been removed in “TESTFO”.

So the latest possible attempt to create an 8.3 filename would be “T~999999”.

This function determines which characters are valid and which aren’t:

static BaseType_t FF_ValidShortChar( char cChar )
{
    return ( cChar >= 'A' && cChar <= 'Z' ) ||
           ( cChar >= 'a' && cChar <= 'z' ) || /* lower-case can be stored using NT/XP attribute. */
           ( cChar >= '0' && cChar <= '9' ) ||
           strchr( "$%-_@~`!(){}^#&", cChar ) != NULL;
} /* FF_ValidShortChar() */
/*----------------------------------------------*/

Characters that are not valid for 8.3 will be removed.

Thanks for the Reply @htibosch.
Expecting space in dir/fol name should like ex: Test Folder → Test Fo~1.
if the dir name is less than <8 characters including space,
it will display like : “Test Fol” → “Test Fol”.

Made changes in FF_ValidShortChar(), space is valid character.

static BaseType_t FF_ValidShortChar( char cChar )
{
return ( cChar >= ‘A’ && cChar <= ‘Z’ ) ||
( cChar >= ‘a’ && cChar <= ‘z’ ) || /* lower-case can be stored using NT/XP attribute. /
( cChar >= ‘0’ && cChar <= ‘9’ ) ||
( cChar >= ’ ’ ) || /space is added/
strchr ( “$%-_@~`!(){}^#&”, cChar ) != NULL;
} /
FF_ValidShortChar() */

finally expecting the folder name like this:: Test folder ----> Test fo~1
THank you,

I read the following here:

  • An 8.3 filename MUST only contain characters that can be represented in ASCII, in the range below 0x80.
  • An 8.3 filename MUST NOT contain the " " space character.
  • An 8.3 filename MUST NOT contain more than one “.” period character.

But if you like to keep the spaces, be my guest. I hope it works under all circumstances, and also that the created volume can be read in another OS.

The space in a file name can be very annoying because of the parsing:

    filename="Last episode.doc"
    gedit $filename

My editor gedit will open 2 files called “Last” and “episode.doc”. Quotes are needed.

Cheers!

Got it, But I have requirement to enable the space in folder name. as attached image expecting folder name “NEW FO~1”

if it is 8.3 format, dir name is less than 8character with space. then space will not be truncated.
This will be checking SD card. exa: “Test Fol”—> "Test Fol "

Thanks :slightly_smiling_face:

I think I am missing something here. The link Hein shared above, says that 8.3 filename cannot contain space. You say that you have a requirement to have space in the name. Are you saying that you have a requirement to be non-complaint? What is the reasoning?

My guess is this is a result of 8.3 being just a “de-facto” standard that wasn’t actually formally defined as to its rules, only the example implementations of it by Microsoft (and other code writers) that were not consistent. From what I remember, it was quite possible in the DOS days to create filenames in programs that were non-complaint with there ability to be used in other parts of the system. File/Directory names with spaces was one such feature, which might need quotes around them in some contexts.

This is a bit like the fact that in Unix, a file name is actually built of ANY arbitray sequence of bytes, with just the values of 0 and 47 decimal (the character ‘/’) being special, 0 being the end of the name and 47 decimal separating the name into path levels. Parts of DOS just took the 11 bytes as is, but other parts put restrictions on them, allowing the creation of “impossible” file names.

Any program that requires using an 8.3 file name that doesn’t meet the simplest definition (being just an 8.3 name built of the basic allowed characters, which don’t include space) is asking to have problems. It especially shouldn’t assume a given conversion from a long file name, that makes me think the program really was expecting a file system with long file names, and then hacked to work on an 8.3 system.