Getting Windows File Attributes

The GetFileAttributes() function retrieves the attribute settings for a file. It returns a DWORD value that can be AND-ed using the << operator to extract information.

Note that the GetFileAttributes() function takes a string filename and not a handle.

int _tmain(int argc, _TCHAR* argv[])
{
	char *file = "C:";
	DWORD dwAttrib;

	dwAttrib = GetFileAttributes(file);

	if(dwAttrib == 0xFFFFFFFF){
		printf("Error getting attributes.\n");
		exit(EXIT_FAILURE);
	}

	if(dwAttrib & FILE_ATTRIBUTE_NORMAL){
		printf("File is a normal file.\n");
	} 
	if(dwAttrib & FILE_ATTRIBUTE_DIRECTORY){
		printf("File is a directory.\n") ;
	} 
	if (dwAttrib & FILE_ATTRIBUTE_DEVICE){
		printf("File is a device.\n");
	} 
	if(dwAttrib & FILE_ATTRIBUTE_ARCHIVE){
		printf("File is an archive.\n");
	} 
	if(dwAttrib & FILE_ATTRIBUTE_COMPRESSED){
		printf("File is compressed.\n");
	} 
	if(dwAttrib & FILE_ATTRIBUTE_HIDDEN){
		printf("File is hidden.\n");
	} 
	if(dwAttrib & FILE_ATTRIBUTE_ENCRYPTED){
		printf("File is encrypted.\n");
	} 

	return 0;
}

The GetFileAttributes() function has a corresponding SetFileAttributes() function that can be used to set the attributes for a file. The SetFileAttributes() function takes two arguments, the first is the file name, and the second is a DWORD value that specifies the new attributes.

#include "windows.h"

void printAttributes(DWORD fileAttributes);

int _tmain(int argc, _TCHAR* argv[])
{
	char *filename = "test.txt";
	DWORD dwCurrentAttrib, dwNewAttrib;

	HANDLE hwFile = CreateFile(
		filename,
		GENERIC_READ | GENERIC_WRITE,
		0,
		NULL,
		CREATE_ALWAYS,
		FILE_ATTRIBUTE_NORMAL,
		NULL
		);

	if(hwFile==INVALID_HANDLE_VALUE){
		printf("Error opening/creating file.\n");
		exit(EXIT_FAILURE);
	} else {
		CloseHandle(hwFile);
	}

	dwCurrentAttrib = GetFileAttributes(filename);
	printAttributes(dwCurrentAttrib);

	dwNewAttrib = FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_READONLY;
	if(SetFileAttributes(filename, dwNewAttrib)){
		printf("File attributes modified successfully.\n");
	} else {
		printf("File attributes could not be modified.\n");
		exit(EXIT_FAILURE);
	}

	dwCurrentAttrib = GetFileAttributes(filename);
	printAttributes(dwCurrentAttrib);

	if(SetFileAttributes(filename, FILE_ATTRIBUTE_NORMAL)){
		printf("File attributes modified successfully.\n");
	}

	printAttributes(GetFileAttributes(filename));
	

	return 0;
}

void printAttributes(DWORD fileAttributes){
	printf("Attributes: ");
	if(fileAttributes == INVALID_FILE_ATTRIBUTES){
		printf("File attribute is invalid.\n");
		return;
	}
	if(fileAttributes & FILE_ATTRIBUTE_NORMAL){
		printf("Normal file. ");
	}
	if(fileAttributes & FILE_ATTRIBUTE_DIRECTORY){
		printf("Directory. ");
	}
	if(fileAttributes & FILE_ATTRIBUTE_HIDDEN){
		printf("File is hidden. ");
	}
	if(fileAttributes & FILE_ATTRIBUTE_READONLY){
		printf("File is read only. ");
	}
	if(fileAttributes & FILE_ATTRIBUTE_ARCHIVE){
		printf("Archive. ");
	}
	if(fileAttributes & FILE_ATTRIBUTE_TEMPORARY){
		printf("Temporary. ");	
	}
	printf("\n");
}

Note that the SetFileAttributes() function returns a Boolean value.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s