2016-10-18 56 views
1

我有一個外部硬盤用作我的PC的備份目標。然後我不得不恢復它,因爲顯然它變得不可讀。恢復程序將所有文件放在HD的根目錄下,以便在主文件夾中有300.000個文件。刪除外部硬盤根目錄中的大量文件

我根據文件夾中的擴展名重新組織文件(JPG,3GP,TIFF,C/H頭等)。然後,我仍然有大量(60.000)的.html和.htm文件來自備份中的某個位置,我無法移動或從Windows 10的資源管理器中刪除(凍結)。

的主要挑戰是:

  1. 文件都在我的HD根 - >我無法用刪除目錄命令(無論是我可以在文件夾中複製它們,或一切凍結)
  2. 在HD的其他文件無法被移動到另一臺PC,因爲我沒有足夠的空的存儲空間

我寫的實際工作罰款這個C程序,但我不得不啓動它的3倍,因爲它由於SIGSEV而崩潰。

我的問題是如果你能解釋我使用這個程序的缺點,以及它如何改進。

的代碼是:

#include <stdio.h> 
    #include <sys/types.h> 
    #include <dirent.h> 
    #include <stdlib.h> 
    #include <string.h> 

    void makeStr (char* fileName, char* file) 
    { 
     static char* str = "F:/"; 

     //Build the string 
     sprintf (fileName, "%s%s", str, file); 
     return; 
    } 

    int main (void) 
    { 
     DIR *dp; 
     struct dirent *ep; 
     dp = opendir ("F:/"); 
     char* ext; 
     char name[260]; 
     int count = 0; 
     const char* format1 = "html"; 
     const char* format2 = "htm"; 

     if (dp != NULL) 
     { 
      // Look in the folder 
      while (((ep = readdir (dp)) != NULL) || count < 60000) 
      { 
       count++; 
       // Find files with an associated file type 
       ext = strrchr(ep->d_name, '.'); 
       if (!ext) 
       { 
        /* no extension */ 
       } 
       else 
       { 
        if (memcmp((char*)format1, (char*)(ext + 1), 4) == 0) 
        { 
         makeStr(name, ep->d_name); 
         if(remove(name) == -1) 
         { 
          printf ("ERROR REMOVING %s\n", ep->d_name); 
          //exit(-1); 
         } 
        } 
        else if (memcmp((char*)format2, (char*)(ext + 1), 3) == 0) 
        { 
         makeStr(name, ep->d_name); 
         if(remove(name) == -1) 
         { 
          printf ("ERROR REMOVING %s\n", ep->d_name); 
          //exit(-1); 
         } 
         else if (count % 100 == 0) 
         { 
          printf("Going well: %d files removed . . .\n",count); 
         } 

        } 
       } 
       //printf ("%s\n", ep->d_name); 
      } 

     closedir (dp); 
     } 
     else 
     printf ("Couldn't open the directory"); 

     return 0; 
    } 
+3

我正在投票關閉這個問題作爲題外話,因爲有關如何改善工作代碼的問題是堆棧溢出的話題。相反,這些問題應該根據其規則和標準在代碼評審中提出。 – TylerH

+0

我正在投票結束這個問題作爲題外話題,因爲它對Stack Overflow來說太廣泛了,它處理的是更多關注的編碼問題。有關改進工作代碼的建議,請考慮[codereview.se],但請首先閱讀[Stack Overflow用戶的代碼審閱指南](// codereview.meta.stackexchange.com/a/5778),因爲有些事情是在那裏完成不同! –

回答

2

尋址只有窄「它如何才能改善」問題的一面:循環條件應&&而非|| - 因爲它代表來自READDIR將返回NULL( )將在strrchr()中解除引用。