Files and Directories using win32

The CreateDirectory() function is used to create a new directory. It takes two arguments, the first is the directory name, and the second is the security settings for the new directory. The RemoveDirectory() function deletes directories. It takes only one argument, the path of the directory to be deleted.

#include <stdio.h>
#include <Windows.h>

int _tmain(int argc, _TCHAR* argv[])
{
	char name[56] = "aDirectory";

	if(CreateDirectory(name, NULL)){
		printf("Directory %s created.\n", name);
	}

	if(RemoveDirectory(name)){
		printf("Directory %s removed.\n", name);
	}

	return 0;
}

We can use the GetFileAttributes() function to make sure a file or directory exists.

#include <stdio.h>
#include <Windows.h>

bool CheckIfFileObjectExists(const char *filename);
bool CheckIfDirectory(const char *filename);
bool CheckIfFile(const char *filename);

int _tmain(int argc, _TCHAR* argv[])
{
	HANDLE hFile;
	char dirName[56] = "TestDirectory";
	char fileName[56] = "TestFile.txt";

	if(CheckIfFileObjectExists(dirName)){
		printf("File object '%s' exists.\n", dirName);
		if(CheckIfDirectory(dirName)){
			printf("It is a directory.\n");
		}
	} else {
		printf("Directory '%s' does not exist.\n", dirName);
		CreateDirectory(dirName, NULL);
	}

	hFile = CreateFile(fileName, GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);

	if(hFile == INVALID_HANDLE_VALUE){
		printf("Error creating file %s\n", fileName);
	} else {
		printf("File %s created.\n", fileName);
		CloseHandle(hFile);
	}

	if(CheckIfFileObjectExists(fileName)){
		printf("%s exists.\n", fileName);
		if(CheckIfDirectory(fileName)){
			printf("%s is a directory.\n", fileName);
		} else {
			printf("%s is not a directory.\n", fileName);
		}
		if(CheckIfFile(fileName)){
			printf("%s is a file.\n", fileName);
			printf("Deleting file now...");
			DeleteFile(fileName);
		}
	}

	return 0;
}

bool CheckIfFileObjectExists(const char *filename){
	DWORD dwAttributes = GetFileAttributes(filename);
	return (bool)(dwAttributes != INVALID_FILE_ATTRIBUTES);
}

bool CheckIfDirectory(const char *filename){
	DWORD dwAttributes = GetFileAttributes(filename);
	return (bool)(dwAttributes & FILE_ATTRIBUTE_DIRECTORY);
}

bool CheckIfFile(const char *filename){
	DWORD dwAttributes = GetFileAttributes(filename);
	return (bool)!(dwAttributes & FILE_ATTRIBUTE_DIRECTORY);
}

The FindFirstFile() function searches through a directory for a given file. Since the FindFirstFile() function accepts a wildcard, we can use it to list all the files in a directory. The FindFirstFile() function takes two arguments, the first of which is the file to find, the second of which is a pointer to a WIN32_FIND_DATA structure.

If the FindFirstFile() function executes successfully, we get a search handle that can be used in subsequent find operations. If FindFirstFile() fails, we get an INVALID_HANDLE_VALUE returned.

The FindNextFile() is used to continue a search. The FindNextFile() function returns true if it finds another file, and false if it doesn’t.

#include <Windows.h>
#include <stdio.h>

void listDirectoryContents(const char *filename);

int _tmain(int argc, _TCHAR* argv[])
{
	listDirectoryContents("C:\\Users\\AL");
	listDirectoryContents("C:\\Users\\AL\\Downloads");
	return 0;
}

void listDirectoryContents(const char *filename){

	DWORD dwAttrib = GetFileAttributes(filename);
	//note negation operator
	if(!(dwAttrib & FILE_ATTRIBUTE_DIRECTORY)){
		printf("%s is not a directory", filename);
		return;
	}

	//create search filename with wildcard (*)
	char buffer[256];
	sprintf_s(buffer, "%s\\*", filename);

	LARGE_INTEGER unionFilesize;
	WIN32_FIND_DATA structWFD;
	HANDLE hSrch = FindFirstFile(buffer, &structWFD);

	if(hSrch == INVALID_HANDLE_VALUE){
		printf("Error %x\n", GetLastError);
		return;
	}

	printf("\nFiles in %s:\n", filename);

	if(structWFD.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY){
		printf("%s \t (directory)\n", structWFD.cFileName);
	} else {
		//get filesize
		unionFilesize.HighPart = structWFD.nFileSizeHigh;
		unionFilesize.LowPart = structWFD.nFileSizeLow;
		printf("%s \t %lld bytes\n", structWFD.cFileName, unionFilesize.QuadPart);
	}

	while(FindNextFile(hSrch, &structWFD)!=0){
		if(structWFD.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY){
			printf("%s \t (directory)\n", structWFD.cFileName);
		} else {
			unionFilesize.HighPart = structWFD.nFileSizeHigh;
			unionFilesize.LowPart = structWFD.nFileSizeLow;
			printf("%s \t %lld bytes\n", structWFD.cFileName, unionFilesize.QuadPart);
		}
	}

	FindClose(hSrch);

}

The FindClose() function is used to close the search handle. It’s a good idea to explicitly close the handle.

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