Recursively traverse all files and folders in a directory, counting the percentages of each type of file
Program code a.cpp (Compile command: g++ a.cpp-o a)
#include <stdio.h> #include <sys/stat.h> #include <sys/types.h> #include <unistd.h> #include <stdlib.h> #include <dirent.h> #include <string.h> struct filestat{ char m_name[100]; //File type name int m_count; //file count (calculated in the main function) float m_ percent; //percentage in all files (calculated in main function) filestat (const char* name) { strcpy (This -> m_Name, name); this -> m_count = 0; this -> m_Percent = 0; }};struct filestat* fs[8] = { new filestat (" Normal file (REGULAR) "), new filestat (" Directory files (directory) "), new filestat ("character special file (character_special)"), new filestat ("Block special file (BLOCK_ Special) "), new filestat (" Pipe or FIFO (FIFO) "), new filestat ("Symbolic link (symbolic_link) "), &NBSP;&NBSP;NEW&NBSP;FILESTAT (socket socket) "), new filestat (" Other unknown Types (UNKNOWN) ")};struct stat buf;//Traverse folder Void checkdir (char* dir, int depth) {&NBSP;&NBsp; dir *dp; struct dirent *entry; struct stat statbuf; if (Dp = opendir (dir)) == NULL ) { fprintf (stderr, "Cannot Open directory: %s\n "); return; } chdir (dir); while ((Entry = readdir (DP)) != null) { lstat (entry - >&NBSP;D_NAME,&NBSP;&STATBUF); //Traversal to Folder if (S_isdir (statbuf.st_mode)) { if (strcmp (".", entry -> d_name) == 0 | | &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;STRCMP ("..", entry -> d_name) == 0) { continue; } //printf ("%d|%*sdir:%s/\n", depth, depth, " ", entry -> d_name); fs[1] -> m_Count++; checkdir (entry -> d_name, depth + 4); } else //traversal to file   { //printf ("%d|%*sFile:%s | " , depth, depth, " ", entry -> d_name); if (Lstat (entry &NBSP;->&NBSP;D_NAME,&NBSP;&BUF) < 0) { printf ("Lstat error"); continue; } if (S_isreg (Buf.st_mode)) { //printf ("Normal file regular\n"); fs[0] -> m_Count++; } else if (S_ISCHR (buf.st_mode)) { //printf ("character special File character special\n"); fs[2] -> m_Count++; } else if (S_ISBLK (buf.st_mode)) { //printf ("Block special files BLOCK Special\n "); fs [3] -> m_count++; } else if (S_isfifo (Buf.st_mode)) { //printf ("Pipe or fifo fifo\n"); fs[4] -> m_Count++; } else if (S_islnk (buf.st_mode)) { //printf ("Symbolic Connection symbolic link\n"); fs[5] -> m_Count++; } else if (S_issock (buf.st_mode)) { // printf ("Socket socket\n"); fs[6] -> m_Count++; } else { //printf ("Unknown type unknown mode\n"); fs[7] -> m_Count++; } } } chdir (".."); closedir (DP);} Int main (int argc, char *argv[]) { if (ARGC&NBSP;!=&NBSP;2) { printf ("Input Error: Argv[1 ] must be a file name!\n "); exit (1); } printf ("Check Folder: %s\n" , argv[1]); checkdir (argv[1],&NBSP;0); //calculates M_count and m_percent int i, total; for (i = 0; i < 8; i++) { total += fs[i] -> m_count; } for (i = 0; i < 8; i++) { fs[i] -> m_percent = 1.0 * fs[i] -> m_count / total; } //Output Calculation results printf (" STAT \n "); printf (" =================== =========================\n "); for (i = 0; i < 8; i++) { printf ("%s | %7.3f%%\n", fs[i] - > m_name, 100.0 * fs[i] -> m_count / total); } printf ("done.\n"); return 0;}
Run results
END
C programs under Linux, traversing folders and counting the percentages of each type of file