/* Programm dursucht ab dem Startverzeichnis den gesamten darunter liegenden Verzeinisbaum und gibt die dain gefundenen Dateien aus mit Verwendung von readdir(), _findfirst(), _findnext() */ #include #include #include #include #include #include #include #include long int pcD = 0; long int pcF = 0; char abspfad[256]; // hier wird der absolute Programmpfad gespeichert //#include "Libs/TFile.h" #define STRSAFE_NO_DEPRECATE /*###################################################################################################################*/ int digits(char *name) { int r=0; while (isdigit(*name)) // https://www.cplusplus.com/reference/cctype/isdigit/ // Check if character is decimal digit character. Decimal digits are any of: 0 1 2 3 4 5 6 7 8 9 { r++; name++; } return r; } /*###################################################################################################################*/ void procFile(char *root, char *oldname, time_t mtime, long int *lpcF) { char st[PATH_MAX], *cp; // newname[PATH_MAX]; // printf("prüfe Datei %s, pcF=%ld \n", oldname, *lpcF); cp = strrchr(oldname, '.'); // Returns a pointer to the last occurrence of character in the C string str. https://www.cplusplus.com/reference/cstring/strrchr/ printf("Suffix (cp= = \"%s\" \n", cp); if (!cp) return; // trennenden Punkt nicht gefunden cp++; // z.B. cp = .txt cp++ = txt -> im String ein Zeichen weiterrücken strcpy(st, cp); // char * strcpy ( char * destination, const char * source ); https://www.cplusplus.com/reference/cstring/strcpy/ cp=st; // ??? // ist es eine txt-Datei ist (dann ist Funktionsergebnis ==0 ) // if (!strcmp(st, "txt")) // https://www.cplusplus.com/reference/cstring/strcmp/ if (!strcmp(st, "txt")) // https://www.cplusplus.com/reference/cstring/strcmp/ // int strcmp ( const char * str1, const char * str2 ); // Compares the C string str1 to the C string str2. { ++(*lpcF); printf("\"procFile\", Datei \"%s\" gefunden, pcF=%li \n", oldname, *lpcF); } while(*cp) // solange *cp nicht NULL ist { (*cp) = tolower(*cp); // Converts c to its lowercase equivalent if c is an uppercase letter and has a lowercase equivalent. https://www.cplusplus.com/reference/cctype/tolower/ cp++; } // printf("Endung=%s\n", st); // // if (strcmp(st, "pdf")) return; // wenn es eine pdf-Datei ist //c = digits(oldname); // //printf("%d Ziffern\n", c); // // if (c==8) return; // wenn der Dateiname 8 Zeichen lang ist // t = localtime(&mtime); // // sprintf(st, "%s/%s", root, oldname); // // sprintf(newname, "%s/%04d%02d%02d_%s", root, t->tm_year+1900, t->tm_mon+1, t->tm_mday, oldname); // // printf("%s -> %s\n", st, newname); // // rename(st, newname); // Datei umbenennen } /*###################################################################################################################*/ void procDir(char *root, long int *lpcD) { DIR *di; // The type DIR, which is defined in the header, represents a directory stream, // which is an ordered sequence of all the directory entries in a particular directory. // Directory entries represent files; files may be removed from a directory or added to a directory asynchronously to the operation of readdir(). struct dirent *d; // siehe unten, https://stackoverflow.com/questions/12991334/members-of-dirent-structure struct stat sb; // data structure containing detailed file information, siehe unten, https://www.mkssoftware.com/docs/man5/struct_stat.5.asp char st[PATH_MAX]; // TCHAR szDir[MAX_PATH]; // externe Variable struct _finddata_t c_file; // intptr_t hFile; char tempStr[512] = {0}; // char dateiFilter[] = "*.txt"; // LARGE_INTEGER filesize; // DWORD dwError=0; printf("\ndurchsuche Verzeichnis %s\n", root); di = opendir(root); // https://pubs.opengroup.org/onlinepubs/009604599/functions/opendir.html if (!di) return; // while (d = readdir(di)) // Struktur "dirent" initialisieren https://pubs.opengroup.org/onlinepubs/009604599/functions/readdir.html // https://pubs.opengroup.org/onlinepubs/009604599/functions/readdir.html { if (d->d_name[0] != '.') // wenn nicht Startverzeichnis { sprintf(st, "%s/%s", root, d->d_name); // stat(st, &sb); // Informationen in struct stat kopieren if ((sb.st_mode & S_IFMT) == S_IFDIR) // // st_mode Access mode and file type for the file (see Flags). // S_IFMT Type of file. (ist eine Datei) // S_IFDIR File is a directory. { ++(*lpcD); printf(" -> bin in Verzeichnis %s, pcD=%li \n", st, *lpcD); // printf("\"getFilePath()\" liefert : \"%s\"\n", getFilePath() ); // Anfang neuer Code zum Finden und Anzeigen von Dateien // sprintf( tempStr, "%s\\%s", getFilePath(), dateiFilter ); // sprintf( tempStr, "%s\\%s", st, dateiFilter ); // hFind = FindFirstFile( tempStr, &FindFileData); sprintf( tempStr, "%s\\%s", st, dateiFilter ); printf( "Suche Datei(en) : \"%s\"\n", tempStr); // Find first .c file in current directory if ( (hFile = _findfirst( tempStr, &c_file )) == -1L ) printf( "Keine Dateien im Verzeichnis gefunden.\n" ); else { printf( "Gefundene Dateien : \n" ); printf( "RDO HID SYS ARC FILE DATE %25c SIZE\n", ' ' ); printf( "--- --- --- --- ---- ---- %25c ----\n", ' ' ); do { char buffer[30]; printf( ( c_file.attrib & _A_RDONLY ) ? " Y " : " N " ); printf( ( c_file.attrib & _A_HIDDEN ) ? " Y " : " N " ); printf( ( c_file.attrib & _A_SYSTEM ) ? " Y " : " N " ); printf( ( c_file.attrib & _A_ARCH ) ? " Y " : " N " ); ctime_s( buffer, _countof(buffer), &c_file.time_write ); printf( " %-12s %.24s %9ld\n", c_file.name, buffer, c_file.size ); } while( _findnext( hFile, &c_file ) == 0 ); } _findclose( hFile ); // Ende neuer Code zum Finden und Anzeigen von Dateien procDir(st, lpcD); // Rekursiver Aufruf von procDir(); } // end of "if ((sb.st_mode & S_IFMT) == S_IFDIR)" /* if ((sb.st_mode & S_IFMT) == S_IFREG) // // st_mode Access mode and file type for the file (see Flags). // S_IFMT Type of file. (ist eine Datei) // S_IFREG File is a regular file. { printf("reguläre Datei gefunden : %s\n", st); //procFile(root, d->d_name, sb.st_mtime.tv_sec); // void procFile(char *root, char *oldname, time_t mtime) procFile(root, d->d_name, sb.st_mtime, &pcF); // void procFile(char *root, char *oldname, time_t mtime) //procFile(root, d->d_name,sb.st_mtim.tv_sec); } */ } // ende von "if (d->d_name[0]!='.')" } // ende von "while (d=readdir(di))" closedir(di); // Verzeichnis mschliessen } /*###################################################################################################################*/ int main(int argc, char **argv) { if(argc<2) return -1; // #ifdef __cplusplus printf("__cplusplus definiert \n"); #else printf("NICHT __cplusplus\n"); #endif #ifdef PATH_MAX printf("PATH_MAX is defined as %d.\n",PATH_MAX); #else printf("PATH_MAX isn't defined on this system.\n"); #endif /* PATH_MAX */ getcwd(abspfad, 256); //der Programmpfad ist jetzt in 'pfad' gespeichert printf("absolutes Startverzeichnis : \"%s\" \n", abspfad); printf("argv[1] =\"%s\"\n", argv[1]); // procDir(argv[1], &pcD); // printf("fertig.\n"); // return 0; } /*###################################################################################################################*/ /* https://stackoverflow.com/questions/12991334/members-of-dirent-structure struct dirent { ino_t d_ino; / * inode number * / off_t d_off; / * offset to the next dirent * / unsigned short d_reclen; / * length of this record * / unsigned char d_type; / * type of file; not supported by all file system types * / char d_name[256]; / * filename * / }; ####################################################################################################################### https://www.mkssoftware.com/docs/man5/struct_stat.5.asp SYNOPSIS #include #include struct stat { dev_t st_dev; ino_t st_ino; mode_t st_mode; nlink_t st_nlink; uid_t st_uid; gid_t st_gid; dev_t st_rdev; off_t st_size; time_t st_atime; time_t st_mtime; time_t st_ctime; blksize_t st_blksize; blkcnt_t st_blocks; mode_t st_attr; }; struct stat64 { dev_t st_dev; ino64_t st_ino; mode_t st_mode; nlink_t st_nlink; uid_t st_uid; gid_t st_gid; dev_t st_rdev; off64_t st_size; time_t st_atime; time_t st_mtime; time_t st_ctime; blksize_t st_blksize; blkcnt64_t st_blocks; mode_t st_attr; }; Fields of the Structure The stat and stat64 structures contain the following fields: st_dev ID of device containing the file. st_ino Serial number for the file. st_mode Access mode and file type for the file (see Flags). st_nlink Number of links to the file. st_uid User ID of file owner. st_gid Group ID of group owner. st_rdev Device ID (if the file is a character or block special device). st_size File size in bytes (if the file is a regular file). st_atime Time of last access. st_mtime Time of last data modification. st_ctime Time of last file status change. st_blksize A file system-specific preferred I/O block size for this object. On some file system types, this may vary from file to file. st_blocks Number of blocks allocated for this file. st_attr The DOS-style attributes for this file (see Flags). This is a PTC MKS Toolkit UNIX APIs extension. Flags The st_mode field contains flags that identify the access mode. There are several NuTCRACKER Platform extensions to the standard access modes, to describe the extended file permissions provided by Windows. Flag Meaning S_IRWXU Read, write, execute/search permission for owner. S_IRUSR Read permission for owner. S_IWUSR Write permission for owner. S_IXUSR Execute/search permission for owner. S_IRWXG Read, write, execute/search for group. S_IRGRP Read permission for group. S_IWGRP Write permission for group. S_IXGRP Execute/search permission for group. S_IRWXO Read, write, execute/search for others. S_IROTH Read permission for others. S_IWOTH Write permission for others. S_IXOTH Execute/search permission for others. S_IDPOU Delete, change permission, and take ownership for owner. S_IDUSR Delete permission for owner. S_IPUSR Change permission permission for owner. S_IOUSR Take ownership permission for owner. S_IDPOG Delete, change permission, and take ownership for group. S_IDGRP Delete permission for group. S_IPGRP Change permission permission for group. S_IOGRP Take ownership permission for group. S_IDPOO Delete, change permission, and take ownership for others. S_IDOTH Delete permission for others. S_IPOTH Change permission permission for others. S_IOOTH Take ownership permission for others. S_ISUID Set-user-ID on execution (not supported). S_ISGID Set-group-ID on execution (not supported). If this bit is set, and S_IXGRP is not set, then mandatory locking is enabled for the file. Refer to fcntl() for more discussion on file locks. S_ISVTX Restricted-deletion flag for directories In addition, the st_mode field defines the file type, using the following flags. The macros described in Access Macros should be used rather than directly accessing the bits in st_mode. Flag Meaning S_IFMT Type of file. S_IFBLK File is a block-device special file. S_IFCHR File is a character-device special file. S_IFIFO File is a FIFO special file. S_IFREG File is a regular file. S_IFDIR File is a directory. S_IFSOCK File is a socket. The NuTCRACKER Platform stat structure has an st_attr field which specifies the DOS-style attributes for a file, as follows: Flag Meaning S_IAREAD File is marked as read-only S_IAHID File is marked as hidden S_IASYS File is marked as a system file S_IADIR File is a directory S_IAENCR File is encrypted S_IAARCH File needs backup ('archive' bit) S_IANORM File is a normal file S_IATEMP File is a temporary file S_IASPARSE File is a sparse file S_IAREPARSE File has an associated reparse point S_IACOMP File is compressed S_IAOFFL File is offline Access Macros The file type can be determined from the stat structure by testing the st_mode field of the structure with the following macros. Each returns a non-zero value if the file is of that type, or 0 if it is not. Macro Meaning S_ISBLK(m) File is a block-device special file. S_ISCHR(m) File is a character-device special file. S_ISFIFO(m) File is a FIFO special file. S_ISREG(m) File is a regular file. S_ISDIR(m) File is a directory. S_ISSOCK(m) File is a socket. */