views:

131

answers:

4

I'm wondering how to get globally unique IDs for files and folders in Windows (XP, Vista and 7), and also be able to get the full path of the file or folder just by having the ID, something like getFileByGUID. I'm trying to do this in C++, C# and PHP.

The globally unique IDs should stay the same even if the file is moved, so using the full path of the file or folder wouldn't work.

Any help would be much appreciated, thanks!

+2  A: 

You could hash together information about the file, such as its metadata and/or contents. It would be difficult to do this on an entire file system without collisions, but I assume you're not trying to index the whole file system. This wouldn't work if you need files to retain their IDs if they're modified, though.

Jon Purdy
Not to mention the resulting database would be HUGE and take several minutes to build from API overhead alone.
Billy ONeal
+2  A: 

You may consider using the Distributed Link Tracking Service.

Matteo Italia
Sounds indeed like the solution to the problem, even if it's not an answer to the question.
MSalters
+1  A: 

Subject to the caveats mentioned in the page for BY_HANDLE_INFORMATION, GetFileInformationByHandle might be helpful, depending on what the goal is.

This won't let one retrieve the file's name, though. Due to NTFS hard links there may be more than one path to the same file contents anyway...

brone
A: 

I would suggest maintaining your own metadata text file/sqlitedb index using the MD5 or SHA1 hash of the file as the id, and all the metadata you want to be able to lookup.

As its a hash of the file and not a random GUID it can easily be rebuilt if the index gets lost or corrupted.

mythz
Um...this is the same as my answer? O_o
Jon Purdy