Skip to content

Instantly share code, notes, and snippets.

@hollobon
Last active December 28, 2015 23:38
Show Gist options
  • Save hollobon/7579947 to your computer and use it in GitHub Desktop.
Save hollobon/7579947 to your computer and use it in GitHub Desktop.
Get NTFS hard link count for file(s)
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