遍历所有磁盘文件

转自:http://www.sicaril.com/thread-2104-1-1.html

  1. //key code
  2. HANDLE g_Event;
  3. VOID GetFile(CTreeCtrl& TreeCtrl, LPSTR lpPath, HTREEITEM hParentsItem)
  4. {
  5.     WIN32_FIND_DATA FindFileData = {0};
  6.     char           szFileName[MAX_PATH] = {0};
  7.     ASSERT(lpPath);
  8.     ASSERT(hParentsItem);
  9.     sprintf(szFileName, "%s%s", lpPath, "*.*");
  10.     HANDLE hFile = FindFirstFile(szFileName, &FindFileData);
  11.     if (hFile == INVALID_HANDLE_VALUE)
  12.     {
  13.         return;
  14.     }
  15.     do 
  16.     {
  17.             WIN32_FILE_ATTRIBUTE_DATA a;
  18.             FILETIME FileTime;
  19.             SYSTEMTIME sysTime;
  20.             char ch = '.';
  21.             char* lpch = "..";
  22.             if (
  23.                 (FindFileData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY)&&
  24.                 (strstr(FindFileData.cFileName, &ch) == NULL)&&
  25.                 (strstr(FindFileData.cFileName, lpch) == NULL)
  26.                )
  27.             {
  28.                 //printf("%s\r\n", FindFileData.cFileName);
  29.                 FileTimeToLocalFileTime(&a.ftCreationTime, &FileTime);
  30.                 FileTimeToSystemTime(&FileTime, &sysTime);
  31.                 //printf("\t\t");
  32.                 //printf("%d-%d-%d\t", sysTime.wYear, sysTime.wMonth, sysTime.wDay);
  33.                 TreeCtrl.InsertItem(FindFileData.cFileName, hParentsItem);
  34.             }
  35.             else
  36.             {
  37.                 //printf("------%s-----", FindFileData.cFileName);
  38.                 //printf("\r\n");
  39.                 char szPath[MAX_PATH] = {0};
  40.                 memset(szPath, 0, MAX_PATH);
  41.                 sprintf(szPath, "%s\\%s\\%s", lpPath,FindFileData.cFileName, "*.*");
  42.                 GetFile(TreeCtrl, szPath, TreeCtrl.InsertItem(FindFileData.cFileName, hParentsItem));                
  43.             }
  44.     } while (FindNextFile(hFile, &FindFileData));

  45.     if (hFile != INVALID_HANDLE_VALUE)
  46.     {
  47.         FindClose(hFile);
  48.         hFile = INVALID_HANDLE_VALUE;
  49.     }

  50. }

  51. VOID Traveser(CTreeCtrl& TreeCtrl)
  52. {
  53.     DWORD dwDrives = GetLogicalDrives();
  54.     if (dwDrives <= 0)
  55.     {
  56.         return;
  57.     }
  58.     char  szbuf[MAXBYTE] = {0};   
  59.     if (GetLogicalDriveStrings(MAXBYTE, szbuf) != 0)
  60.     {
  61.         char* lpRootPathName = szbuf;
  62.         
  63.         for (int i = 0; i < MAXBYTE; i++)
  64.         {
  65.             if ((lpRootPathName[i] >= 'C' && lpRootPathName[i] <= 'Z')
  66.                 || (lpRootPathName[i] >= 'c' && lpRootPathName[i] <= 'z'))
  67.             {
  68.                 char szFormat[MAX_PATH] = {0};
  69.                 sprintf(szbuf, "%c:\\", lpRootPathName[i]);
  70.                 GetFile(TreeCtrl, szbuf, TreeCtrl.InsertItem(szbuf)); 
  71.             }
  72.         }
  73.     }
  74.     SetEvent(g_Event);
  75. }

  76. DWORD __stdcall ThreadProc(LPARAM lpParam)
  77. {
  78.      CTreeCtrl* lpTree = (CTreeCtrl*)lpParam;
  79.      ASSERT(lpTree);
  80.      lpTree->DeleteAllItems();
  81.      lpTree->EnableAutomation();
  82.      lpTree->EnableWindow(FALSE);
  83.      lpTree->SetBkColor(RGB(78,155,177));
  84.      lpTree->SetTextColor(RGB(155, 0,0));
  85.      lpTree->SetRedraw(FALSE);
  86.      g_Event = CreateEvent(NULL, FALSE, FALSE, NULL);
  87.      Traveser(*lpTree);
  88.      WaitForSingleObject(g_Event, -1);
  89.      lpTree->EnableWindow();
  90.      lpTree->SetRedraw(TRUE);
  91.      return 0;
  92. }
  93. BOOL CFileDirctoryDlg::OnInitDialog()
  94. {
  95.         CDialog::OnInitDialog();
  96.         ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  97.         ASSERT(IDM_ABOUTBOX < 0xF000);
  98.         CMenu* pSysMenu = GetSystemMenu(FALSE);
  99.         if (pSysMenu != NULL)
  100.         {
  101.                 CString strAboutMenu;
  102.                 strAboutMenu.LoadString(IDS_ABOUTBOX);
  103.                 if (!strAboutMenu.IsEmpty())
  104.                 {
  105.                         pSysMenu->AppendMenu(MF_SEPARATOR);
  106.                         pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  107.                 }
  108.         }
  109.         SetIcon(m_hIcon, TRUE);                
  110.         SetIcon(m_hIcon, FALSE);
  111.     m_hThread = CreateThread(NULL,
  112.                              NULL, 
  113.                              (DWORD (__stdcall*)(void *))ThreadProc,
  114.                              &m_FileTree, 
  115.                              CREATE_SUSPENDED,
  116.                              &m_dwPid);
  117.     
  118.     if (m_hThread != NULL)
  119.     {
  120.         ResumeThread(m_hThread);
  121.         CloseHandle(m_hThread);
  122.     }
  123.     
  124.     
  125.         return TRUE;  // return TRUE  unless you set the focus to a control
  126. }
复制代码
project:

还有很多功能没做完,待续

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值