Thursday, November 04, 2004

File Length / Path Length restrictions under Windows

To check the file path length limitation on Windows Server 2003, I created a small bat file with the following two lines
md a
cd a

This creates a folder a and changes dir. to that folder. I kept on running this bat file, till it gave me an error "The filename or extension is too long."

Screen Capture showing the bat file being run multiple times

Once the folders were created, they looked like this in Explorer
Screen cap showing explorer view of the folder hierarchy

The full path was
"C:\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\aa\a\a\a\a
\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\aa\a\a\a\a\a\a\a\a\a
\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\aa\a\a\a\a\a\a\a\a\a\a\a\a\a\a
\a\a\a\a\a\a\a\a\a\a\a\a\a\aa\a\a\a\a\a\a\a"

That's a total of 121 levels! But seriously, that's not important, what's important here is the total path length. 122 'a's and 122 '\'s makes it 244. 244 and the two letters for the drive and colon, make it 246. Finally, if we were to add any files to this folder, there would be trailing slash added to the path after the last folder name, which adds one more character making the total 247!

Once we have a folder who's total path length is 247, then we are allowed only short-file names. That is, 8.3 format file names. So, adding the 8 characters from the name take the total to 255! Now, based on this finding I am led to believe that the extension is a separate entry in the structure for the file system.

To confirm this, I tried creating files, and I was allowed only 8 characters in the filename. What about the extension? The ren command refused to accept extensions beyond 3 characters length, the error message being
"The system cannot accept the path or file name requested."

So, does this confirm that the length of the file-path must be less that 255?
To test that out, I did the following

C:\>copy con A0123456789B0123456789C0123456789D0123456789
E0123456789F0123456789G0123456789H0123456789I0123456789
J0123456789K0123456789L0123456789M0123456789N0123456789
O0123456789P0123456789Q0123456789R0123456789S0123456789
T0123456789U0123456789V0123456789W0123456789X0123456789
Y0123456789Z0123456789.txt
The file name is too long.
0 file(s) copied.

Hmm, 2nd try

C:\>copy con A0123456789B0123456789C0123456789D0123456789
E0123456789F0123456789G0123456789H0123456789I0123456789
J0123456789K0123456789L0123456789M0123456789N0123456789
O0123456789P0123456789Q0123456789R0123456789S0123456789
T0123456789U0123456789V0123456789W0123456789X0.txt
The file name is too long.
0 file(s) copied.


So, I progressively reduced the length till:

C:\>copy con A0123456789B0123456789C0123456789D0123456789
E0123456789F0123456789G0123456789H0123456789I0123456789
J0123456789K0123456789L0123456789M0123456789N0123456789
O0123456789P0123456789Q0123456789R0123456789S0123456789
T0123456789U0123456789V0123456789W012345678.txt
File Length Test
The filename, directory name, or volume label syntax is incorrect.
0 file(s) copied.

Hmm, still no luck, lets continue to remove one character from the name.

C:\>copy con A0123456789B0123456789C0123456789D0123456789
E0123456789F0123456789G0123456789H0123456789I0123456789
J0123456789K0123456789L0123456789M0123456789N0123456789
O0123456789P0123456789Q0123456789R0123456789S0123456789
T0123456789U0123456789V0123456789W01234567.txt
File Length Test
^Z
1 file(s) copied.


Ok, Great. This time the file name length was 251 characters plus "C:\" makes it 254.
So, actually the last but one attempt should've worked correctly (i.e. with 252 characters in file name). I think that's because 1 character is still reserved for the trailing "\" just in case! [wink]

Now, what about the length of the extension?
Continuing on the same track,

C:\>copy con .A0123456789B0123456789C0123456789D0123456789
E0123456789F0123456789G0123456789H0123456789I0123456789
J0123456789K0123456789L0123456789M0123456789N0123456789
O0123456789P0123456789Q0123456789R0123456789S0123456789
T0123456789U0123456789V0123456789W0123456789X0123456789
Y0123456789Z0123456789
The file name is too long.
0 file(s) copied.

Same logic and finally,

C:\>copy con .A0123456789B0123456789C0123456789D0123456789
E0123456789F0123456789G0123456789H0123456789I0123456789
J0123456789K0123456789L0123456789M0123456789N0123456789
O0123456789P0123456789Q0123456789R0123456789S0123456789
T0123456789U0123456789V0123456789W0123456789X0
File Length Test
The filename, directory name, or volume label syntax is incorrect.
0 file(s) copied.

C:\>copy con .A0123456789B0123456789C0123456789D0123456789
E0123456789F0123456789G0123456789H0123456789I0123456789
J0123456789K0123456789L0123456789M0123456789N0123456789
O0123456789P0123456789Q0123456789R0123456789S0123456789
T0123456789U0123456789V0123456789W0123456789X
File Length test
^Z
1 file(s) copied.


Length again: 254. The '.' is now considered part of the file name.

So, there we have it! The total path length has to be less that 254 characters including the filename.

Finally, I was able to get a link to the MSDN Article which mentions this:
Making room for long file names
From the article
Protected-Mode FAT File System (VFAT)
The VFAT file system organizes data on fixed and floppy disks just like the FAT file system. It is also compatible with the FAT file system, using file allocation tables and directory entries to store information about the contents of a disk. VFAT supports long filenames by storing these names and other information, such as the date and time the file was last accessed, in the FAT structures. It allows filenames of up to 255 characters, including the terminating null character. This is similar to NTFS, which allows filenames of up to 256 characters. VFAT allows paths of up to 260 characters, including the terminating null character. Do remember, though, that the path includes the full filename, so if you happen to have a filename that is 255 characters long, you will only have 4 characters left for the path (the last character will be the NULL terminator).

4 comments:

  1. Anonymous3:09 AM

    gidThat was an interesting experiment.

    ReplyDelete
  2. Anonymous2:05 AM

    this is a nice article.

    consider this:


    you have N number of files with big filenames that exceeds the limit and you'ld like to move or delete them so del *.* or move *.* returns with the path error you mentioned. Then whats available in command line to deal with these files.

    Assume you have hundreds of thousands of these in a folder and all batch operations (del/move) are terminated with that error. And windows explorer simply crashes when you try to view that folder, so the only option is to use command line and only way to see these files is through dir command.

    This happened to me just now and i was googling aroung to find a tool or ways to deal with such folders, with no luck.

    Perhaps you can shed some ligt onto it.

    ReplyDelete
  3. Plain curious: how did so many files get created in the first place?

    Anyway,
    1. First, try using an explorer replacement like x2 explorer lite. Available at http://www.download.com/Xplorer2-Lite/3000-2248-10407731.html?part=dl-Xplorer2L&subj=uo&tag=button

    If even x2 is crashing, we might need to do some scripting, one option is Windows Powershell (http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx)

    You could contact me via email: vinilm@gmail.com

    ReplyDelete
  4. Anonymous5:58 PM

    There is a library providing possibility to access and handle files with long filename/path. I have encountered the restriction myself and used the library to successfully overcome the problem. The library permanent link: http://www.abtollc.com/Products.aspx

    ReplyDelete