tool: count line count of files in a folder
#include <stdio.h>
#include <windows.h>
//#include <hash_map>
//#include <string>
char line[10000];
FILE *fpW = 0; //count line
FILE *fpW_Date = 0; //for srch file within some date
long gnchar=0,gnline=0;
int hours_before = 0;
char destFolder[MAX_PATH]={0};
//std::hash_map<std::string> exts;
char *exts[] = {"h", "hpp","c","cpp"
};
int nExt = 4;
void count(const char *file, long &nchar, long &nline)
{
nchar = 0;
nline = 0;
FILE *fp = fopen(file, "r");
if(fp)
{
for(char *p = fgets(line, 10000, fp); p; p = fgets(line, 10000, fp))
{
nline++;
nchar += strlen(p);
}
fclose(fp);
}
}
inline bool isExt(const char *file)
{
const char *ext = strrchr(file, '.');
if(!ext)
return false;
ext++;
for(int i=0; i<nExt; i++)
{
if(stricmp(ext, exts[i])==0)
return true;
}
return false;
}
float seconds_between(const FILETIME & from, const FILETIME & to)
{
enum {
TICKS_PER_DAY = 10000000,
};
return float((*(__int64 *) & to) - (*(__int64 *) & from)) / TICKS_PER_DAY;
}
void getFileList(const char *folder, long &fnchar, long &fnline)
{
WIN32_FIND_DATA stFindData;
long nchar, nline;
char sFull[MAX_PATH];
sprintf(sFull, "%s\\*", folder);
HANDLE Handle = FindFirstFile(sFull, &stFindData);
if(Handle == INVALID_HANDLE_VALUE)return;
FindNextFile(Handle, &stFindData);
while (FindNextFile(Handle, &stFindData))
{
sprintf(sFull, "%s\\%s", folder, stFindData.cFileName);
if(stFindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) //folder
{
printf("%s\n",sFull);
long subfnchar=0, subfnline=0;
getFileList(sFull, subfnchar, subfnline);
fnchar += subfnchar;
fnline += subfnline;
}
else //file
{
if(isExt(stFindData.cFileName))
{
count(sFull, nchar, nline);
fprintf(fpW, "<tr><td>%s<td>%d<td>%d</tr>\n", sFull, nchar, nline);
gnchar += nchar;
gnline += nline;
fnchar += nchar;
fnline += nline;
//SYSTEMTIME stUTC;
//FileTimeToSystemTime( stFindData.ftLastWriteTime, &stUTC );
if(hours_before)
{
FILETIME stNow;
GetSystemTimeAsFileTime(&stNow);
float diff_second = seconds_between(stFindData.ftLastWriteTime,stNow);
if(diff_second>hours_before*24*60*60) //check date
fprintf(fpW_Date, "%s\n", sFull);
}
}
}
}
FindClose(Handle);
fprintf(fpW, "<tr><td><font color=red>%s</font> <td><font color=red>%d</font> <td><font color=red>%d</font> </tr>\n", folder, fnchar, fnline);
}
void main(int argc, char *argv[])
{
bool succeed = false;
if(argc>=2)
{
//parse arguments
for(int i=1; i<argc; i++)
{
if(argv[i][0]=='-')
{
if(argv[i][1]=='t')
{
sscanf(argv[i+1], "%d", &hours_before);
}
else if(argv[i][1]=='c')
{
strcpy(destFolder, argv[i+1]);
}
}
}
printf("parse result, hours_before %d, destFolder %s\n", hours_before, destFolder);
long fnchar=0, fnline=0;
char destfile[300];
sprintf(destfile, "%s\\lineCnt.htm", argv[1]);
fpW = fopen(destfile, "w");
sprintf(destfile, "%s\\DateCnt.txt", argv[1]);
fpW_Date = fopen(destfile, "w");
fprintf(fpW, "<table>\n");
getFileList(argv[1],fnchar,fnline);
fprintf(fpW, "<tr><td>%s<td>%d<td>%d</tr>\n", "Total", gnchar, gnline);
fprintf(fpW, "</table>\n");
fclose(fpW);
fclose(fpW_Date);
}
if(!succeed)
{
printf("Usage:\n");
printf("lineCnt [-t <hours before>] [-copyto <folder>] <folder>\n");
}
}
#include <windows.h>
//#include <hash_map>
//#include <string>
char line[10000];
FILE *fpW = 0; //count line
FILE *fpW_Date = 0; //for srch file within some date
long gnchar=0,gnline=0;
int hours_before = 0;
char destFolder[MAX_PATH]={0};
//std::hash_map<std::string> exts;
char *exts[] = {"h", "hpp","c","cpp"
};
int nExt = 4;
void count(const char *file, long &nchar, long &nline)
{
nchar = 0;
nline = 0;
FILE *fp = fopen(file, "r");
if(fp)
{
for(char *p = fgets(line, 10000, fp); p; p = fgets(line, 10000, fp))
{
nline++;
nchar += strlen(p);
}
fclose(fp);
}
}
inline bool isExt(const char *file)
{
const char *ext = strrchr(file, '.');
if(!ext)
return false;
ext++;
for(int i=0; i<nExt; i++)
{
if(stricmp(ext, exts[i])==0)
return true;
}
return false;
}
float seconds_between(const FILETIME & from, const FILETIME & to)
{
enum {
TICKS_PER_DAY = 10000000,
};
return float((*(__int64 *) & to) - (*(__int64 *) & from)) / TICKS_PER_DAY;
}
void getFileList(const char *folder, long &fnchar, long &fnline)
{
WIN32_FIND_DATA stFindData;
long nchar, nline;
char sFull[MAX_PATH];
sprintf(sFull, "%s\\*", folder);
HANDLE Handle = FindFirstFile(sFull, &stFindData);
if(Handle == INVALID_HANDLE_VALUE)return;
FindNextFile(Handle, &stFindData);
while (FindNextFile(Handle, &stFindData))
{
sprintf(sFull, "%s\\%s", folder, stFindData.cFileName);
if(stFindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) //folder
{
printf("%s\n",sFull);
long subfnchar=0, subfnline=0;
getFileList(sFull, subfnchar, subfnline);
fnchar += subfnchar;
fnline += subfnline;
}
else //file
{
if(isExt(stFindData.cFileName))
{
count(sFull, nchar, nline);
fprintf(fpW, "<tr><td>%s<td>%d<td>%d</tr>\n", sFull, nchar, nline);
gnchar += nchar;
gnline += nline;
fnchar += nchar;
fnline += nline;
//SYSTEMTIME stUTC;
//FileTimeToSystemTime( stFindData.ftLastWriteTime, &stUTC );
if(hours_before)
{
FILETIME stNow;
GetSystemTimeAsFileTime(&stNow);
float diff_second = seconds_between(stFindData.ftLastWriteTime,stNow);
if(diff_second>hours_before*24*60*60) //check date
fprintf(fpW_Date, "%s\n", sFull);
}
}
}
}
FindClose(Handle);
fprintf(fpW, "<tr><td><font color=red>%s</font> <td><font color=red>%d</font> <td><font color=red>%d</font> </tr>\n", folder, fnchar, fnline);
}
void main(int argc, char *argv[])
{
bool succeed = false;
if(argc>=2)
{
//parse arguments
for(int i=1; i<argc; i++)
{
if(argv[i][0]=='-')
{
if(argv[i][1]=='t')
{
sscanf(argv[i+1], "%d", &hours_before);
}
else if(argv[i][1]=='c')
{
strcpy(destFolder, argv[i+1]);
}
}
}
printf("parse result, hours_before %d, destFolder %s\n", hours_before, destFolder);
long fnchar=0, fnline=0;
char destfile[300];
sprintf(destfile, "%s\\lineCnt.htm", argv[1]);
fpW = fopen(destfile, "w");
sprintf(destfile, "%s\\DateCnt.txt", argv[1]);
fpW_Date = fopen(destfile, "w");
fprintf(fpW, "<table>\n");
getFileList(argv[1],fnchar,fnline);
fprintf(fpW, "<tr><td>%s<td>%d<td>%d</tr>\n", "Total", gnchar, gnline);
fprintf(fpW, "</table>\n");
fclose(fpW);
fclose(fpW_Date);
}
if(!succeed)
{
printf("Usage:\n");
printf("lineCnt [-t <hours before>] [-copyto <folder>] <folder>\n");
}
}