RSS

Monthly Archives: November 2010

Counting the number of files in a directory

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 \”::\””);

system(kommand);

 

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

system(kommand);

fp = fopen(fnOutput, “r”);

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

nFile = atoi(lain);

 

 

 
Leave a comment

Posted by on November 5, 2010 in Programming