Working with Disks and Directories in win32

We can find out the maximum size of any drive, along with its available free space, using the GetDiskFreeSpace() function.

The GetDiskFreeSpace() function has five parameters, the first of which is a pointer to a string containing the pathname of the root directory of the disk. If we specify NULL for this argument, the function uses the current disk.

The second, third, fourth, and fifth arguments are all of type DWORD. The second argument is a pointer to a DWORD variable that receives the number of sectors per cluster. The third argument is a pointer to a variable that receives the number of bytes per sector. The fourth argument is a pointer to a variable that stores the total number of clusters that are free and available to the owner of the thread. The fifth and final argument is a pointer to a variable that stores the number of clusters, both free and occupied, available to the user.

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

int _tmain(int argc, char argv[]){

	BOOLEAN bSuccess;

	DWORD dwSectorsPerCluster;
	DWORD dwBytesPerSector;
	DWORD dwFreeClusters;
	DWORD dwTotalClusters;

	//get disk space for current drive
	bSuccess = GetDiskFreeSpace(
		NULL, //current drive
		&dwSectorsPerCluster, //sectors per cluster
		&dwBytesPerSector, //bytes per sector
		&dwFreeClusters, //free clusters
		&dwTotalClusters //total clusters
		);

	if(!bSuccess){
		printf("Could not get drive information.\n");
		exit(EXIT_FAILURE);
	}

	printf("Bytes per sector: %d\n", dwBytesPerSector);
	printf("Sectors per cluster: %d\n", dwSectorsPerCluster);
	printf("Free clusters: %d\n", dwFreeClusters);
	printf("Total clusters: %d\n", dwTotalClusters);

	unsigned int uiKBPerCluster = dwBytesPerSector * dwSectorsPerCluster / 1024;

	printf("You have %.2f MB of free storage\n", (double) uiKBPerCluster * dwFreeClusters / 1024);
	printf("You have %.2f MB of total storage\n", (double) uiKBPerCluster * dwTotalClusters / 1024);

	return 0;

}

The GetFreeDiskSpace() function returns data about the number of clusters and free clusters on a disk. Clusters are sets of sectors grouped together as a logical unit. The function also returns the number of sectors per cluster and the number of bytes per sector.

We can query for the drive letters that are currently in use. The GetLogicalDriveStrings() function returns the drive letters in string form, and takes two arguments, the first being the size of the buffer and the second being the buffer itself. The GetLogicalDriveStrings() function returns 0 on failure.

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

int _tmain(int argc, char argv[]){

	TCHAR szBuffer[256];
	TCHAR *szPtr;
	DWORD dwBufferLength = _tcslen(szBuffer);

	GetLogicalDriveStrings(dwBufferLength, szBuffer);

	for(szPtr = szBuffer; *szPtr!=''; szPtr++){
		//print out drive letter
		_tprintf(_T("%s\n"), szPtr);
		//move ahead to next entry
		while(*szPtr!=''){szPtr++; }
	}

	return 0;

}

Note that the final buffer entry is followed by two  characters.

The SearchPath() function searches either the system path or a given directory for a file. The SearchPath() function takes five arguments. The first argument is the path to search, the second argument is the file name, the third argument is the file extension, the fourth argument is the size of the result buffer, the fifth argument is the result buffer, and the sixth argument is a pointer to the file part of the result path.

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

int _tmain(int argc, char argv[]){

	TCHAR szFilename[] = _T("notepad.exe");
	TCHAR szDirname[MAX_PATH + 1];
	TCHAR szBuffer[MAX_PATH + 1];
	TCHAR *ptrFile;
	DWORD dwReturnValue;

	dwReturnValue = SearchPath(
		NULL, //search system path
		szFilename,
		0, //extension
		MAX_PATH, //buffer length
		szBuffer,
		&ptrFile
		);

	//if 0 returned, then file not found
	if(dwReturnValue==0){
		_tprintf(_T("File %s not found.\n"), szFilename);
		exit(EXIT_FAILURE);
	} else {
		_tprintf(_T("File %s found.\n"), szFilename);
		_tprintf(_T("%s\n"), szBuffer);
	}

	return 0;

}

Later we will cover methods for searching for filenames with wildcards.

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