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);
}

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?

How do you make it list all the files? Figure that out and change it.

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.

You need to use the command line parameters. See this series -- it may help.

The parameter to FindFirstFile() is wrong. If you want to find all the files in c:\\MyDir *.c then use that as the parameter to FindFirstFile.

Note: The code below has not been compiled or tested. This is supposed to concantinate all the command-line parameters to make a single path statement. For example, if you type on the command line myprog c:\MyDir <space> *.c then it should result in "c:\MyDir\*.c"

int main(int argc, char* argv[])
{
   char path[_MAX_PATH] = {0};
   int i;
   for(i = 1; i < argc; i++)
   {
      strcat(path,argv[i]);
      if( i < argc)
        strcat(path,"\\");
   }
   find = FindFirstFile(path,&found);
}

The parameter to FindFirstFile() is wrong. If you want to find all the files in c:\\MyDir *.c then use that as the parameter to FindFirstFile.

Note: The code below has not been compiled or tested. This is supposed to concantinate all the command-line parameters to make a single path statement. For example, if you type on the command line myprog c:\MyDir <space> *.c then it should result in "c:\MyDir\*.c"

int main(int argc, char* argv[])
{
   char path[_MAX_PATH] = {0};
   int i;
   for(i = 1; i < argc; i++)
   {
      strcat(path,argv[i]);
      if( i < argc)
        strcat(path,"\\");
   }
   find = FindFirstFile(path,&found);
}

Okay, thanks! But I'm still having trouble running my program from command line. How can I make it so that I can simply type "myDir blah.ext" from the cmd for it to print the directory ON the cmd window itself?

Thanks.

Okay, thanks! But I'm still having trouble running my program from command line. How can I make it so that I can simply type "myDir blah.ext" from the cmd for it to print the directory ON the cmd window itself?

Thanks.

I'm now sorry I bothered to post. :icon_confused:

Edited 5 Years Ago by WaltP: n/a

I'm now sorry I bothered to post. :icon_confused:

Oh, no I didn't ask that question because I ignored your post. I asked the question again because I tried that already but it didn't work. CMD keeps giving me the same prompt: "myDir is not a command" or something along those lines.

Sorry, I wasn't trying to be rude. I appreciated your effort! :)

By the way, is the ">" necessary to execute the program? So for instance, would I need to type "C:\Users\Documents\>myDir blah" to execute the program myDir with the single argument "blah" ?

CMD keeps giving me the same prompt: "myDir is not a command" or something along those lines.

Ahhh, info that actually tells us something about your problem. Either:
1) There are compile errors therefore myDir was never built.
2) You are not in the directory where myDir.exe exists when you type the command.

Ahhh, info that actually tells us something about your problem. Either:
1) There are compile errors therefore myDir was never built.
2) You are not in the directory where myDir.exe exists when you type the command.

Well it got built w/o any errors but it is NOT an .exe file--it's a .c file.

How are you compiling the program ?
There has to be a .exe file ...

Hm...my solution file is still in the ".c" extension. How can I make this into .exe? And if I do convert this into an executable file can I type myDir <filename.ext> to get <filename.ext> as an argument into the solution code?

You have to use a compiler to convert the solution *.c file into *.exe file. You can not execute *.c files. There are at least two good free compilers for the MS-Windows operating system: Code::Blocks and VC++ 2010 Express. Choose either one you want, but you might find Code::Blocks a little easier to learn.

MyDir is not the name of an executable program, but just the name of the directory that contains the *.c files you want to list.

You have to use a compiler to convert the solution *.c file into *.exe file. You can not execute *.c files. There are at least two good free compilers for the MS-Windows operating system: Code::Blocks and VC++ 2010 Express. Choose either one you want, but you might find Code::Blocks a little easier to learn.

MyDir is not the name of an executable program, but just the name of the directory that contains the *.c files you want to list.

Well my solution (".c") file is named myDir. If I compiled this into an .exe couldn't I run it by calling myDir <filename.ext> ? And I have Visual Studio 2010, could I compile it into an exe through visual studio to make it into an .exe?

yes you can compile it with that compiler. Do if you have MyDir.exe then you would type on the command line MyDir c:\somepath\*.c <Enter Key> I don't know what you mean by <filename.exe>.

yes you can compile it with that compiler. Do if you have MyDir.exe then you would type on the command line MyDir c:\somepath\*.c <Enter Key> I don't know what you mean by <filename.exe>.

By saying <filename.ext> I'm just using that as a placeholder for a file name. And it was .EXT (extension) not .exe (executable).

Any idea how to compile a .c to an .exe with visual studio? Cause I am clueless.

This article has been dead for over six months. Start a new discussion instead.