Search code examples
windowswinapiwindows-shellwindows-explorer

What is the longest file path allowed to be moved to Recycle Bin?


For my application I'm testing the longest available file path to be allowed to be moved into the Recycle Bin and I'm getting interesting results.

On Windows XP the maximum size is 259 characters, which is the MAX_PATH constant minus 1.

But on my Windows 8.1 Pro, that maximum allowed file path size somehow seem to be 215 characters.

So I'm curious are there any official guidelines for this?

EDIT: OK, since posters below requested an API, I'm using SHFileOperation with FO_DELETE and FOF_ALLOWUNDO to place a user's file into the Recycle Bin. Since Windows Explorer uses the same exact API for its Delete operation it's easy to test it by making a long path within Windows Explorer and then by trying to delete it. In my experiments I can see the following:

  • Windows XP, if the total path length is 259 chars (on some editions, it may be 257??), the file/folder will be placed into the Recycle Bin. Otherwise Windows Explorer offers only an option to permanently delete it.

  • Windows Vista, this limit is 217 chars, inclusively.

  • Windows 7 and 8, it is 215 chars, inclusively.

So it seems like this maximum limit is shrinking... Thus I was just curious, if this is documented somewhere in MSDN?


Solution

  • Recycle Bin internals:

    Windows XP

    Every drive has its own drive:\RECYCLER\%USER_SID% directory. This directory contains all deleted files but files have names like DcN.ext where D is fixed part of the name, c is drive letter, N is a index and ext is extension of original file. Besides deleted files there is database file named INFO2.

    INFO2 file starts with header. Header structure:

    Offset Type  Value
    0x0000 DWORD Signature  ; Always 5
    0x0004 DWORD Unknown1
    0x0008 DWORD Unknown2
    0x000C DWORD RecordSize ; Always 0x00000320
    0x0010 DWORD Unknown3
    

    Records are stored successively immediately after header to the end of the INFO2 file. Record structure:

    Offset Type               Value
    0x0000 ANSICHAR[MAX_PATH] OriginalFileNameA ; Ansi string
    0x0104 DWORD              Index             ; Associated with *N* from *DcN.ext*
    0x0108 DWORD              DriveIndex        ; A: = 0; B: = 1; C: = 2; ...
    0x010C FILETIME           DeleteFileTime
    0x0114 DWORD              OriginalFileNamePhysicalSize
    0x0118 WIDECHAR[MAX_PATH] OriginalFileNameW ; Wide string
    

    Windows Vista and above

    Every drive has its own drive:\$Recycle.Bin\%USER_SID% directory. This directory contains all deleted files but now there is no database file. Every deleted file is associated with 2 files inside RB.

    First file has name like $INNNNNN.ext where $I is fixed part of the name, NNNNNN consists of 6 random letters or numbers and ext is extension of original file.

    $I file structure:

    Offset Type               Value
    0x0000 DWORD              Signature         ; Always 1
    0x0004 DWORD              Unknown1
    0x0008 DDWORD             OriginalFileSize
    0x0010 FILETIME           DeleteFileTime
    0x0018 WIDECHAR[MAX_PATH] OriginalFileNameW ; Wide string
    

    Second file has name like $RNNNNNN.ext where $R is fixed part of the name, NNNNNN is the same as in $I file and ext is extension of original file. $R file is deleted file itself.

    As you see in all cases Windows stores filename in array which has size of MAX_PATH chars. That why the limit of length of filename is MAX_PATH - 1 chars.

    Windows 10

    Windows 10 has a new version of $I file structure (don`t know what update changed it):

    Offset Type                          Value
    0x0000 DWORD                         Signature         ; Always 2
    0x0004 DWORD                         Unknown1
    0x0008 DDWORD                        OriginalFileSize
    0x0010 FILETIME                      DeleteFileTime
    0x0018 DWORD                         OriginalFileNameLen
    0x001C WIDECHAR[OriginalFileNameLen] OriginalFileNameW ; Wide string
    

    And it looks like now Windows can store any files with any paths in the Recycle Bin.