So I'm trying to code a program where it produces a directory listing of all files matching a specified file name pattern. I have compiled a code that is somewhat similar (But still functions differently) than the code I want; my code just lists the directory with full parameters. How can I make it from here so that it can list only the directory list that matches a specified file name pattern. So for example, how can I code it so that if I type myDir *.c it would list all files with an extension ".c" in that current directory?
This brings me to my second question: how can I make it so that my solution file "myDir.c" can be ran from the command prompt? Navigating to my solution file in CMD and entering "myDir bla" doesn't do anything when it should print the current directory with full pathnames on CMD. Please help!!
Here's my code:
#include <windows.h>
#include <winbase.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
WIN32_FIND_DATA found; // Search buffer
HANDLE hFind; // Search handle
char path[MAX_PATH];
char fname[MAX_PATH];
GetCurrentDirectory(MAX_PATH, path);
hFind = FindFirstFile("*.*", &found);
if(hFind != INVALID_HANDLE_VALUE) { // Found a first file
do {
if( (strcmp(found.cFileName, ".") != 0) &&
(strcmp(found.cFileName, "..") != 0) )
{
// Construct Fully-qualified File Specification
strcpy_s(fname, MAX_PATH, path);
strcat_s(fname, MAX_PATH, "\\");
strcat_s(fname, MAX_PATH, found.cFileName);
printf("%s\n", fname);
}
} while(FindNextFile(hFind, &found));
FindClose(hFind);
}
return(0);
}