Last active
December 28, 2015 23:38
-
-
Save hollobon/7579947 to your computer and use it in GitHub Desktop.
Get NTFS hard link count for file(s)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
import tempfile | |
import win32file | |
from glob import glob | |
def get_link_count(filename): | |
if os.path.isdir(filename): | |
dwFlagsAndAttributes = win32file.FILE_FLAG_BACKUP_SEMANTICS | |
else: | |
dwFlagsAndAttributes = 0 | |
handle = win32file.CreateFile(filename, | |
win32file.GENERIC_READ, | |
win32file.FILE_SHARE_READ, | |
None, | |
win32file.OPEN_EXISTING, | |
dwFlagsAndAttributes, | |
None) | |
(attributes, created_at, accessed_at, written_at, volume, file_hi, file_lo, | |
n_links, index_hi, index_lo) = win32file.GetFileInformationByHandle(handle) | |
return n_links | |
def main(): | |
for filename in glob(sys.argv[1]): | |
print(filename, get_link_count(filename)) | |
if __name__ == "__main__": | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Based on http://timgolden.me.uk/python/win32_how_do_i/see_if_two_files_are_the_same_file.html