- 论坛徽章:
- 0
|
1 #include <stdio.h>
2 #include <dirent.h>
3 #include <string.h>
4 #include <sys/types.h>
5 #include <unistd.h>
6 #include <sys/stat.h>
7
8 int main( void )
9 {
10 //
11 chdir("./dir");
12 renameAll("dir");
13 return 1;
14 }
15
16 /* */
17 int renameAll( char *pathname )
18 {
19 // tell us type of the file
20 int dir = is_dir( pathname );
21 if( dir == -1 ) {
22 printf( "Error: Fu*king. No such directory!" );
23 return -1;
24 }
25
26 // open dir
27 chdir(pathname);
28 DIR *dp = opendir( pathname );
29 if(dp == NULL) {
30 printf( "Error: Can't read dir!\n" );
31 printf("pathname=%s\n", pathname);
32 //exit(1);
33 return -1;
34 }
35
36 // read dir
37 struct dirent *ptr;
38 struct stat statbuf; // Record the info
39 while ( (ptr=readdir(dp)) != NULL ) {
40
41 //printf("name=%s, type=%d\n", ptr->d_name, ptr->d_type);
42
43 lstat( ptr->d_name, &statbuf );
44 if ( S_ISDIR(statbuf.st_mode ) && (ptr->d_name != ".") && (ptr->d_name != "..") ) {
45 chdir( ptr->d_name );
46 renameAll( ptr->d_name );
47 } else {
48 printf("F**king: name=%s, type=%d\n", ptr->d_name, ptr->d_type);
49 }
50
51 }
52 closedir( dp );
53
54 return -1;
55 }
56
57
58 int is_dir(const char *pathname)
59 {
60 struct stat buf;
61 memset(&buf, 0, sizeof(buf));
62 lstat(pathname, &buf);
63 if( S_ISDIR(buf.st_mode) ) {
64 return 1;
65 }
66 return 0;
67 }
我是希望递归的打印出各个文件夹底下的文件名的。不打印文件夹名。 |
|