本帖最后由 523066680 于 2017-1-20 16:27 编辑
枚举目录内容,如果是文件在开头给出尺寸,如果是文件夹,在开头给出 DIR 或者 FOLD 的标记,遇到链接自动绕开
输出UNICODE编码的字符。
须使用 g++ 编译- /*
- Code by:523066680@163.com
- 2017-01
- */
-
- #include <stdio.h>
- #include <fcntl.h>
- #include <dirent.h>
- #include <wchar.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <string.h>
- #include <windows.h>
-
- #define NAME_MAX 10240
-
- void func(wchar_t path[]);
- void console_print(const wchar_t str[]);
- void CheckConsoleRedirect(void);
-
- DWORD written = 0;
- static bool g_bRedirect = false;
-
- int wmain(int argc, wchar_t *argv[] )
- {
- wchar_t pth[NAME_MAX] = L"D:\\Extra\\";
-
- //首先判断是否为重定向
- CheckConsoleRedirect();
-
- if ( argc >= 2 )
- {
- if ( argv[1][wcslen(argv[1])-1] == '\\' )
- swprintf(pth, L"%ls", argv[1] );
- else
- swprintf(pth, L"%ls\\", argv[1] );
- }
-
- func( pth );
-
- return 0;
- }
-
- void func( wchar_t path[] )
- {
- DWORD attr;
- _WDIR * a = _wopendir(path);
- _wdirent * dp;
- _WDIR * aa;
- struct stat stbuf;
- wchar_t full_info[NAME_MAX] = L"";
-
- WCHAR fullpath[NAME_MAX] = L"";
-
- swprintf(full_info, L"FOLD: %ls\r\n", path );
- console_print( full_info );
-
- //打印目录内容
- while ( dp = _wreaddir(a) )
- {
- //排除 当前文件夹 和 上一级 文件夹
- if (
- ( wcscmp(dp->d_name, L"." ) == 0 )
- || ( wcscmp(dp->d_name, L".." ) == 0 )
- )
- { continue; }
- swprintf(fullpath, L"%ls%ls", path, dp->d_name);
- wstat(fullpath, &stbuf);
- attr = GetFileAttributesW(fullpath);
-
- //排除符号链接类型的文件和目录
- if ( (attr & FILE_ATTRIBUTE_REPARSE_POINT) != FILE_ATTRIBUTE_REPARSE_POINT )
- {
- if ( (attr & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY )
- {
- swprintf(full_info, L"%16ls %ls\r\n", L"DIR", dp->d_name );
- console_print( full_info );
- }
- else
- {
- swprintf(full_info, L"%16lld %ls\r\n", stbuf.st_size, dp->d_name );
- console_print( full_info );
- }
- }
- }
- _wclosedir(a);
-
- //递归流程
- a = _wopendir(path);
- while ( dp = _wreaddir(a) )
- {
- //排除 当前文件夹 和 上一级 文件夹
- if (
- ( wcscmp(dp->d_name, L"." ) == 0 )
- || ( wcscmp(dp->d_name, L".." ) == 0 )
- )
- {
- continue;
- }
-
- swprintf(fullpath, L"%ls%ls", path, dp->d_name);
- attr = GetFileAttributesW(fullpath);
-
- //排除符号链接类型的文件和目录
- if ( (attr & FILE_ATTRIBUTE_REPARSE_POINT) != FILE_ATTRIBUTE_REPARSE_POINT )
- {
- if ( (attr & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY )
- {
- swprintf(fullpath, L"%ls%ls\\", path, dp->d_name);
- func( fullpath );
- }
- }
- }
- _wclosedir(a);
- }
-
- void console_print(const wchar_t str[])
- {
- if ( ! g_bRedirect )
- {
- WriteConsoleW(
- GetStdHandle(STD_OUTPUT_HANDLE), str, wcslen(str) , &written, NULL
- );
- }
- else
- {
- WriteFile(
- GetStdHandle(STD_OUTPUT_HANDLE), str, wcslen(str) * sizeof( str[0] ) , &written, NULL
- );
- }
- }
-
- void CheckConsoleRedirect(void)
- {
- if (!GetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), &written))
- {
- g_bRedirect = true;
- WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), "\xFF\xFE", 2, &written, 0);
- }
- }
复制代码 输出的一部分示例:- Fold: D:\Extra\
- 21253 065738_6eeL_1428332.jpg
- 362664 1440339f6bzggbp0x16gp3.jpg
- 398975 144034a5tty5g2s2azd3bg.jpg
- 229915 144037xhfh2jhzh1g1b2jf.jpg
- 40421 6cc028c79f3df8dc7a6e5322cd11728b461028c5.jpg
- 1853657 7cefdfa5jw1ez6iowueepg206l067qv5.gif
- DIR Book
- 11714560 freetype-2.4.0.tar
- 2903989 ft27.zip
- DIR graphics
复制代码 如此一来可以顺便写一个文件夹按大小排序的脚本。 |