99 lines
2.4 KiB
C++
99 lines
2.4 KiB
C++
|
#include <stdio.h>
|
|||
|
#include <stdlib.h>
|
|||
|
#include <string.h>
|
|||
|
#include <dirent.h>
|
|||
|
#include <sys/stat.h> // <20>Ω<EFBFBD><CEA9>ˬd<CBAC>O<EFBFBD>_<EFBFBD><5F><EFBFBD>ؿ<EFBFBD>
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ܼƫŧi
|
|||
|
FILE *fp;
|
|||
|
long fileCount = 0;
|
|||
|
|
|||
|
// <20>s<EFBFBD>W<EFBFBD>Y<EFBFBD>ƻ<EFBFBD><C6BB>U<EFBFBD>禡
|
|||
|
void printIndent(int level) {
|
|||
|
printf("|");
|
|||
|
for(int i = 0; i < level; i++) {
|
|||
|
printf(" "); // <20>C<EFBFBD>h<EFBFBD>Y<EFBFBD>ƨ<EFBFBD><C6A8>ӪŮ<D3AA>
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// <20>קאּ<D7A7><EFACB0><EFBFBD>j<EFBFBD><6A><EFBFBD><EFBFBD>
|
|||
|
void open_folder(char *path, int level) {
|
|||
|
DIR *dir;
|
|||
|
struct dirent *entry;
|
|||
|
char fullPath[512];
|
|||
|
struct stat statbuf;
|
|||
|
long fileSize;
|
|||
|
|
|||
|
// <20>}<7D>ҥؿ<D2A5>
|
|||
|
if ((dir = opendir(path)) == NULL) {
|
|||
|
printf("<EFBFBD>L<EFBFBD>k<EFBFBD>}<7D>ҥؿ<D2A5>: %s\n", path);
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
// Ū<><C5AA><EFBFBD>ؿ<EFBFBD><D8BF><EFBFBD><EFBFBD>e
|
|||
|
while ((entry = readdir(dir)) != NULL) {
|
|||
|
// <20><><EFBFBD>L . <20>M ..
|
|||
|
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
|
|||
|
continue;
|
|||
|
|
|||
|
// <20>c<EFBFBD>ا<EFBFBD><D8A7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>|
|
|||
|
snprintf(fullPath, sizeof(fullPath), "%s/%s", path, entry->d_name);
|
|||
|
|
|||
|
// <20><><EFBFBD>o<EFBFBD>ɮ<C9AE><D7B8>T
|
|||
|
if (stat(fullPath, &statbuf) == -1) {
|
|||
|
printf("<EFBFBD>L<EFBFBD>k<EFBFBD><EFBFBD><EFBFBD>o<EFBFBD>ɮ<EFBFBD><EFBFBD>T: %s\n", fullPath);
|
|||
|
continue;
|
|||
|
}
|
|||
|
|
|||
|
// <20>L<EFBFBD>X<EFBFBD>ثe<D8AB>h<EFBFBD>Ū<EFBFBD><C5AA>Y<EFBFBD><59>
|
|||
|
printIndent(level);
|
|||
|
|
|||
|
// <20>P<EFBFBD>_<EFBFBD>O<EFBFBD>ؿ<EFBFBD><D8BF>٬O<D9AC>ɮ<EFBFBD>
|
|||
|
if (S_ISDIR(statbuf.st_mode)) {
|
|||
|
// <20>O<EFBFBD>ؿ<EFBFBD>
|
|||
|
printf("[<5B><><EFBFBD>Ƨ<EFBFBD>] %s\n", entry->d_name);
|
|||
|
// <20><><EFBFBD>j<EFBFBD>B<EFBFBD>z<EFBFBD>l<EFBFBD>ؿ<EFBFBD>
|
|||
|
open_folder(fullPath, level + 1);
|
|||
|
} else {
|
|||
|
// <20>O<EFBFBD>ɮ<EFBFBD>
|
|||
|
if ((fp = fopen(fullPath, "rb")) != NULL) {
|
|||
|
fseek(fp, 0, SEEK_END);
|
|||
|
fileSize = ftell(fp);
|
|||
|
fclose(fp);
|
|||
|
printf(" %s (%ld bytes)\n", entry->d_name, fileSize);
|
|||
|
fileCount++;
|
|||
|
} else {
|
|||
|
printf("%s (<28>L<EFBFBD>kŪ<6B><C5AA>)\n", entry->d_name);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
closedir(dir);
|
|||
|
}
|
|||
|
|
|||
|
int main() {
|
|||
|
char directoryPath[128];
|
|||
|
|
|||
|
system("cls");
|
|||
|
printf("\n<EFBFBD><EFBFBD><EFBFBD>J<EFBFBD>ؿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>| ==> ");
|
|||
|
scanf("%127[^\n]%*c", directoryPath);
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>|<7C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>u<D7BD>]<5D>p<EFBFBD>G<EFBFBD><47><EFBFBD><EFBFBD><EFBFBD>ܡ^
|
|||
|
size_t len = strlen(directoryPath);
|
|||
|
if (len > 0 && (directoryPath[len-1] == '\\' || directoryPath[len-1] == '/')) {
|
|||
|
directoryPath[len-1] = '\0';
|
|||
|
}
|
|||
|
|
|||
|
if (opendir(directoryPath) == NULL) {
|
|||
|
printf("<EFBFBD>ؿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>s<EFBFBD>b!\n");
|
|||
|
return -1;
|
|||
|
}
|
|||
|
|
|||
|
printf("\n<EFBFBD>ؿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>e<EFBFBD><EFBFBD><EFBFBD>c<EFBFBD>p<EFBFBD>U<EFBFBD>G\n=======================================\n");
|
|||
|
open_folder(directoryPath, 0);
|
|||
|
printf("=======================================\n");
|
|||
|
printf("<EFBFBD>`<60>@<40><> %ld <20><><EFBFBD>ɮ<EFBFBD>\n", fileCount);
|
|||
|
|
|||
|
return 0;
|
|||
|
}
|