Counting the number of files in a directory

05 Nov

For the directory “C:\folder”, the command line is

dir “C:\folder” /b/s |find /v /c “::”

Without considering sub folders, remove “/s”

dir “C:\folder” /b |find /v /c “::”

In C++ code,

char kommand[200];

sprintf(kommand, “dir \”C:/folder\” /b/s |find /v /c \”::\””);

//sprintf(kommand, “dir \”C:/folder\” /b |find /v /c \”::\””);



To use the command line output in C++ codes, save the result as a text file (“result.txt”) and read it.

int nFile;

FILE *fp = NULL;

char kommand[200], lain[200], *fnOutput = “result.txt”;

sprintf(kommand, “dir \”C:/folder\” /b/s |find /v /c \”::\” > %s”, fnOutput);

//sprintf(kommand, “dir \”C:/folder\” /b |find /v /c \”::\” > %s”, fnOutput);


fp = fopen(fnOutput, “r”);

fgets(lain, sizeof(lain), fp);

nFile = atoi(lain);



Leave a comment

Posted by on November 5, 2010 in Programming


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your 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

%d bloggers like this: