找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
楼主: crownking1983

[文件操作] 求批处理统计文件夹内的所有文件的数量和总大小(精确到M)

[复制链接]
 楼主| 发表于 2015-11-22 23:32:08 | 显示全部楼层
以上几位修改的都不行呢 最后还是我那哥们儿搞定了 毕竟是他写的程序所以还是他自己比较熟悉 麻烦大家了 不好意思啊
发表于 2015-11-22 23:38:26 | 显示全部楼层
本帖最后由 523066680 于 2015-11-22 23:45 编辑

最近重新学C,
没有测试链接的目录,
风格好渣……

  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cwchar>
  4. #include <cstring>
  5. #include <sys/stat.h>
  6. #include <sys/types.h>
  7. #include <dirent.h>
  8. #include <locale.h>

  9. #define NAME_MAX 1024

  10. void func(wchar_t path[]);
  11. static long long int TotalSize = 0;
  12. static long long int FileCount = 0;
  13. static long long int DirCount = 0;

  14. int main(int argc, char *argv[] )
  15. {
  16.     setlocale( LC_ALL, ".936" );
  17.     wchar_t wspath[1024] = L"";
  18.     char ts_str[30] = "";

  19.     if (argc > 1)
  20.     {
  21.         mbstowcs( wspath, argv[1], strlen(argv[1]) );

  22.         _WDIR * a;
  23.         DIR * b;

  24.         if ( (a = _wopendir(wspath)) == NULL )
  25.         {
  26.             fprintf(stderr, "Argument is not correct!");
  27.         }
  28.         else
  29.         {
  30.             func( wspath );
  31.             fprintf(stderr, "%20lld Folders\n", DirCount);
  32.             fprintf(stderr, "%20lld Files\n"  , FileCount);
  33.             fprintf(stderr, "%20lld Bytes\n"   , TotalSize);
  34.             fprintf(stderr, "%20lld MB\n"      , TotalSize/1024/1024);
  35.             fprintf(stderr, "%20lld GB"        , TotalSize/1024/1024/1024);
  36.         }
  37.     }
  38.     else
  39.     {
  40.         fprintf(stderr, "No arguments!");
  41.     }
  42.    
  43.     return 0;
  44. }

  45. void func(wchar_t path[])
  46. {
  47.     _WDIR * a = _wopendir(path);
  48.     _wdirent * dp;
  49.     _WDIR * aa;
  50.     struct _stati64 stbuf;
  51.     wchar_t fullpath[NAME_MAX] = L"";

  52.     while (dp = _wreaddir(a))
  53.     {
  54.         if (
  55.                wcscmp(dp->d_name, L".") == 0
  56.             || wcscmp(dp->d_name, L"..") == 0  
  57.         )
  58.         {
  59.             continue;
  60.         }

  61.         swprintf(fullpath, L"%ls\\%ls", path, dp->d_name);
  62.         _wstati64(fullpath, &stbuf);

  63.         if ( (stbuf.st_mode & S_IFMT) == S_IFDIR )
  64.         {
  65.             DirCount ++;
  66.             func( fullpath );
  67.         }
  68.         else
  69.         {
  70.             TotalSize +=  (long long int)stbuf.st_size;
  71.             FileCount ++;
  72.         }

  73.     }
  74.     _wclosedir(a);
  75. }
复制代码
                1962 Folders
               17329 Files
         76636660679 Bytes
               73086 MB
                  71 GB

评分

参与人数 1技术 +1 收起 理由
依山居 + 1 看你的骨骼精奇,是万中无一的武学奇才,维 ...

查看全部评分

发表于 2018-3-28 16:06:04 | 显示全部楼层
您能把修改好的程度共享出来看看吗?
发表于 2018-3-28 21:30:29 | 显示全部楼层
回复 18# rogerzhao9


    8楼和11楼的代码有啥问题吗?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-18 04:56 , Processed in 0.016759 second(s), 7 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表