8

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?

c00000fd
  • 20,994
  • 29
  • 177
  • 400
  • This question belongs to http://superuser.com/ – Spook May 29 '14 at 05:21
  • 1
    @Spook: Why? It's a programming question. – c00000fd May 29 '14 at 05:25
  • 2
    Aye, for the most part it could belong to both, although I'm not sure why would a user really care about this. Definitely seems like a programming question (that belongs here) to me. And a good one too. +1 – MasterMastic May 29 '14 at 05:32
  • IIRC the "longest [total] path allowed" is larger than MAX_PATH: ref http://stackoverflow.com/questions/265769/maximum-filename-length-in-ntfs-windows-xp-and-windows-vista and http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx (see extended-length path: "The Windows API has many functions that also have Unicode versions to permit an extended-length path for a maximum total path length of 32,767 characters.") – user2864740 May 29 '14 at 05:36
  • @c00000fd No, it isn't. This is a question about Windows filesystem, NTFS in particular. http://superuser.com/search?q=longest+file+path+windows – Spook May 29 '14 at 05:48
  • What API are you using? – Harry Johnston May 29 '14 at 05:50
  • @HarryJohnston: `SHFileOperation` with `FO_DELETE` and `FOF_ALLOWUNDO`. As for larger file paths, then yes, if they are prepended with `\\.\` but Windows Explorer and `SHFileOperation` do not support it. – c00000fd May 29 '14 at 05:56
  • @Spook: the question isn't "what is the longest path Windows can handle" but "what is the longest path SHFileOperation can handle". I think it's on-topic. – Harry Johnston May 29 '14 at 05:59
  • @HarryJohnston: OK, it's not only SHFileOperation. It's just one of them. This limit is throughout most Shell APIs. – c00000fd May 29 '14 at 06:01
  • @c00000fd: I suspect that the exact limit will vary from API to API. – Harry Johnston May 29 '14 at 06:03
  • @c00000fd Update the question with the particular API (and test-case code and what "interesting results" mean). – user2864740 May 29 '14 at 06:19
  • Cannot confirm that Windows 7 has limit of 217 chars. In my test limit the same - 259 chars. – Denis Anisimov May 29 '14 at 13:39

2 Answers2

8

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.

Denis Anisimov
  • 3,297
  • 1
  • 10
  • 18
  • Thanks for the info. Having your info I was able to come up with a formula. I described it here: http://forums.codeguru.com/showthread.php?545395-How-can-I-get-path-to-the-recycle-bin-folder-for-a-specific-drive&p=2155927#post2155927 – c00000fd May 31 '14 at 22:36
  • One question though -- is recycle bin folder (this part `C:\$Recycle.Bin`) always placed in the root of a drive? In other words, it can't be `C:\Something\$Recycle.Bin`, can it? – c00000fd May 31 '14 at 22:38
  • As I know - always in the root of drive. – Denis Anisimov May 31 '14 at 23:16
  • 2
    There is one special case, which may or may not be relevant: if the user's Documents folder is redirected to a network (UNC) path, Windows creates a Recycle Bin folder as a subfolder of Documents. – Harry Johnston Jun 02 '14 at 07:06
  • @HarryJohnston: Good point. Thanks. I just tested it. What happens is that any redirected folder (not only Documents) can have its own Recycle Bin. It is created on the Server as a subfolder as soon as something is deleted from that folder in the client user account. A path on the server can be something like this: `C:\Home\UserName\Documents\$RECYCLE.BIN`. Note that deletion from a folder that is not redirected goes into a general recycle bin, or the one in the root folder on the client machine. Hmm. That complicates things because one cannot access the `$RECYCLE.BIN` on the server itself... – c00000fd Jun 03 '14 at 08:11
  • The question now is how to find out if a certain folder is being redirected? – c00000fd Jun 03 '14 at 08:12
0

Simple answer. The recycle bin is a (well) hidden folder on disk. All the files in it keep their original names, plus the extra length for the name of the recycled bin folder.

In Windows XP the path looked like this:

C:\RECYCLER\S-1-5-21-1089551744-1120685985-1162132538-1003\

I'm sure it's similar in later versions, but perhaps getting longer. Your filename has to go on on the end of that, and still stay inside the 260 limit.


So how do you find that name for a specific system? Try GetFinalPathnameByHandle, or this blog.

http://msdn.microsoft.com/en-us/library/windows/desktop/aa364962.aspx

http://pdh11.blogspot.com.au/2009/05/pathcanonicalize-versus-what-it-says-on.html


More useful places to look.

IKnownFolder: http://msdn.microsoft.com/en-us/library/windows/desktop/bb776912.aspx

SHGetKnownFolderPath: http://msdn.microsoft.com/en-us/library/windows/desktop/bb762188%28v=vs.85%29.aspx

SHGetFolderPath using CSIDL: http://msdn.microsoft.com/en-us/library/windows/desktop/bb762181%28v=vs.85%29.aspx

david.pfx
  • 10,520
  • 3
  • 30
  • 63
  • Ha. Interesting. Thanks for the input. It makes sense now. Just curious if there's any API to get that path that you quoted? – c00000fd May 29 '14 at 16:05
  • @c00000fd The *Recycle Bin* virtual folder goes by the [`KNOWNFOLDERID` FOLDERID_RecycleBinFolder](http://msdn.microsoft.com/en-us/library/windows/desktop/dd378457.aspx). Using `KNOWNFOLDERID`s is explained under [Working with Known Folders in Applications](http://msdn.microsoft.com/en-us/library/windows/desktop/bb776912.aspx). – IInspectable May 29 '14 at 16:26
  • @IInspectable: Yes, I'm aware of that. The question is how to get a recycle bin path for a specific drive, like `C:` for instance? – c00000fd May 29 '14 at 16:53
  • @ david.pfx. I'm sorry, I'm not following your logic. Can you show an example? Obviously one needs a handle to the recycle bin file to use `GetFinalPathnameByHandle`, right? – c00000fd May 31 '14 at 23:49
  • You can use the known folder ID to open the file or folder which gives you a handle. Then you can get the path from the handle. – david.pfx Jun 01 '14 at 01:25
  • @david.pfx: I'm still not following you. Sorry, buddy. What API will I use for this: `You can use the known folder ID to open the file or folder which gives you a handle`? – c00000fd Jun 02 '14 at 01:36
  • I could easily be wrong, but as `FOLDERID_RecycleBinFolder` is a virtual folder, I don't think you can actually open a handle to it. – Harry Johnston Jun 02 '14 at 21:28
  • @HarryJohnston: You'll just have to try it. I would expect it does, and that each of the files it contains has a path, and they are all in the same folder (same prefix). – david.pfx Jun 02 '14 at 23:28
  • Try it how? None of the shell interfaces you mention provide any methods that return a handle. Also, you do realize that in the general case there's more than one physical directory involved? – Harry Johnston Jun 03 '14 at 01:53
  • `FOLDERID_RecycleBinFolder` represents more than one physical folder. One simple case is that the recycle bin is specific for each Windows user. So there's absolutely no association between a file system `HANDLE` and the Shell `CSIDL` for recycle bin. – c00000fd Jun 03 '14 at 06:54