2

I'd like to check the archive bit for each file in a directory using python. So far i've got the following but i can't get it to work properly. The idea of the script is to be able to see all the files that have the archive bit on.

Thanks

# -*- coding: latin-1 -*-
import os , win32file, win32con
from time import *
start = clock()

ext = [ '.txt' , '.doc' ]

def fileattributeisset(filename, fileattr):
    return bool(win32file.GetFileAttributes(filename) & fileattr)

for root, dirs, files in os.walk('d:\\Pruebas'):
    print ("root", root)    
    print ("dirs", dirs)
    print ("files", files)
    for i in files:
        if i[ - 4:] in ext: 
            print('...', root, '\\', i, end=' ')
            fattrs = win32file.GetFileAttributes(i)
            if fattrs & win32con.FILE_ATTRIBUTE_ARCHIVE:
                print('A isSet',fattrs)
        #print( fileattributeisset(i, win32con.FILE_ATTRIBUTE_ARCHIVE)) 
    print ('####')

EDIT: all files appear to have the archive bit on, doing 'attrib' shows that all files have no attribute bits on.

Alan Featherston
  • 1,086
  • 3
  • 14
  • 27

1 Answers1

7

The file list returned from os.walk are not fully qualified paths, so when you call

win32file.GetFileAttributes(i)

it can't find the file and returns an error code; which happens to be -1. So the operation

fattrs & win32con.FILE_ATTRIBUTE_ARCHIVE 

is always true.

You need to join the root to the filename so that GetFileAttributes will succeed:

fattrs = win32file.GetFileAttributes(os.path.join(root, i))

Also, when you are checking the extension, it is probably better to use os.path.splitext(path) to retrieve the extension rather than relying on them be 3 characters long as you do.

zdan
  • 28,667
  • 7
  • 60
  • 71