Skip to content

Tool: drivelist

This is a tool I wrote for windows, that will list all of the drives currently mounted. I didn’t know of a better way to show this information from the command line. I might add volume names later

/**
 * Drivelist v1.0.1
 * @file
 *
 * 05-25-2008:	JPH - Created.
 * 08-22-2008:	JPH - Added case 0, 1 and 6 to the switch of types.
 *
 * @author Jacob Hammack
 */
 
#include <windows.h>
#include <stdio.h>
 
/**
 * List's and prints all mounted drives and their drive type.
 *
 * @author Jacob Hammack
 */
void ListMountedDrives(void)
{
	char Buffer[MAX_PATH];
	char *DriveLetter;
	int DriveType;
	GetLogicalDriveStrings(MAX_PATH, Buffer);
	DriveLetter = Buffer;
 
	while (*DriveLetter) 
	{
		if(*DriveLetter == 0)
		{
			break;
		}
 
		DriveType = GetDriveType(DriveLetter);
 
		switch(DriveType)
		{
			case 0:
				printf("%s\tUnknown Type\n", DriveLetter);
			break;
 
			case 1:
				printf("%s\tInvalid Root Path\n", DriveLetter);
			break;
 
			case 2:
				printf("%s\tRemoveable Drive\n", DriveLetter);
			break;
 
			case 3:
				printf("%s\tFixed Drive\n", DriveLetter);
			break;
 
			case 4:
				printf("%s\tNetwork Drive\n", DriveLetter);	
			break;
 
			case 5:
				printf("%s\tCD-ROM Drive\n", DriveLetter);
			break;	
 
			case 6:
				printf("%s\tRam Drive\n", DriveLetter);
			break;
		}
 
		DriveLetter = &DriveLetter[strlen(DriveLetter) + 1];
	}
}
 
/**
 * Main entry point for the DriveList Application.
 *
 * @author Jacob Hammack
 */
int main(int argc, char *argv[])
{
	printf("Drivelist v1.0.1\nJacob Hammack\nhttp://www.hammackj.com\n\n");
 
	ListMountedDrives();
 
	return 0;		
}

CC=cl
CFLAGS= /nologo /MT /O2 /TC
LINKS=/link kernel32.lib /OUT:dl.exe /SUBSYSTEM:CONSOLE
 
all: drivelist
 
drivelist: drivelist.c
$(CC) $(CFLAGS) drivelist.c $(LINKS)
 
clean:
del *.exe; del *.obj
C:\drivelist>dl.exe
Drivelist v1.0.1
Jacob Hammack
http://www.hammackj.com
 
A:\     Removeable Drive
C:\     Fixed Drive
D:\     Fixed Drive
E:\     CD-ROM Drive
Z:\     Network Drive
 
C:\drivelist>

static void RecurseFileSystem(TCHAR *StartingPath);

In a few of the tools that I have written, I have needed to list the windows file system recursively. While .Net makes this much easier, all of the tools I write are in win32 C. Hopefully this will help someone else, as when I looked for information on this I did not find very much.

static void RecurseFileSystem(TCHAR *StartingPath)
{
    HANDLE CurrentFileHandle;
    WIN32_FIND_DATA FileInformation;
    TCHAR CurrentFileName[MAX_PATH];
    TCHAR m_szFolderInitialPath[MAX_PATH];
    TCHAR wildCard[MAX_PATH] = TEXT("\\*.*");
 
    _tcscpy_s(CurrentFileName, MAX_PATH, StartingPath);
    _tcscpy_s(m_szFolderInitialPath, MAX_PATH, StartingPath);
    _tcsncat_s(m_szFolderInitialPath, MAX_PATH, wildCard, MAX_PATH);
 
    CurrentFileHandle = FindFirstFile(m_szFolderInitialPath, &amp;FileInformation);
 
    if(CurrentFileHandle != INVALID_HANDLE_VALUE)
    {
        do
        {
            if((_tcscmp( FileInformation.cFileName, TEXT(".") ) != 0) &amp;&amp;
                (_tcscmp(FileInformation.cFileName, TEXT("..")) != 0))
            {
                _tcscpy_s(CurrentFileName, MAX_PATH, StartingPath);
                _tcsncat_s(CurrentFileName, MAX_PATH, TEXT("\\"), MAX_PATH);
                _tcsncat_s(CurrentFileName, MAX_PATH, FileInformation.cFileName, MAX_PATH);
 
                if(FileInformation.dwFileAttributes &amp; FILE_ATTRIBUTE_DIRECTORY)
                {
                    RecurseFileSystem(CurrentFileName);
                }
                else
                {
                    /* Do action on file here! */
                }
            }
        }
        while(FindNextFile(CurrentFileHandle, &amp;FileInformation) == TRUE);
 
        FindClose(CurrentFileHandle);
    }
}