自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

將前綴和后綴相同的文件移動(dòng)到同一個(gè)目錄的算法設(shè)計(jì)及C代碼實(shí)現(xiàn)

開(kāi)發(fā) 開(kāi)發(fā)工具 算法
在Linux系統(tǒng)的某幾個(gè)目錄下有一些前綴和后綴相同的文件,編寫(xiě)程序?qū)⑺鼈円苿?dòng)到同一個(gè)目錄下。

一、需求描述

在Linux系統(tǒng)的某幾個(gè)目錄下有一些前綴和后綴相同的文件,編寫(xiě)程序?qū)⑺鼈円苿?dòng)到同一個(gè)目錄下。

例如,有三個(gè)源目錄FileDir1、FileDir2和FileDir3,里面分別存放有文件File_1.txt、File_2.txt和File_3.txt。由于它們有相同的前綴(File_)和后綴(txt),所以要將這三個(gè)文件移動(dòng)到同一個(gè)結(jié)果目錄(假設(shè)為GatherDir)中。

二、算法設(shè)計(jì)

基于需求,可以采用如圖1所示的程序流程:

圖1 程序總體流程

三、特殊流程考慮

在編寫(xiě)程序的過(guò)程中,對(duì)于某些特殊流程的考慮如下:

1.如果掃描某個(gè)目錄出錯(cuò),則直接停止程序的運(yùn)行,而不用繼續(xù)掃描下一個(gè)目錄。

2.對(duì)于某些空文件(即文件的大小為0),直接在源目錄中將其刪除,而不用移動(dòng)到結(jié)果目錄中。

3.為了隨時(shí)能夠處理放到源目錄中的文件,程序每隔一段時(shí)間(如一分鐘)掃描一次源目錄。也就是說(shuō),如果不人為操作,程序啟動(dòng)之后會(huì)不停地運(yùn)行。

四、程序代碼

  1. /********************************************************************** 
  2. * 版權(quán)所有 (C)2016, Zhou Zhaoxiong。 
  3. * 文件名稱(chēng):FileGather.c 
  4. * 文件標(biāo)識(shí):無(wú) 
  5. * 內(nèi)容摘要:將各個(gè)目錄中前綴相同的文件集中到一個(gè)目錄中 
  6. * 其它說(shuō)明:無(wú) 
  7. * 當(dāng)前版本:V1.0 
  8. * 作    者:Zhou Zhaoxiong 
  9. * 完成日期:20160513 
  10. **********************************************************************/ 
  11. #include <stdio.h> 
  12. #include <string.h> 
  13. #include <dirent.h> 
  14. #include <ftw.h> 
  15. #include <time.h> 
  16.  
  17. // 重定義數(shù)據(jù)類(lèi)型 
  18. typedef signed   int        INT32; 
  19. typedef unsigned int        UINT32; 
  20. typedef unsigned char       UINT8; 
  21.  
  22. // 全局變量定義 
  23. UINT8  g_szGatherDir[256] = {0};     // 匯總文件的目錄 
  24. UINT8  g_szFilePrefix[20] = {0};     // 需要匯總的文件的前綴 
  25. UINT8  g_szFileSuffix[20] = {0};     // 需要匯總的文件的后綴 
  26.  
  27.  
  28. // 宏定義 
  29. #define   DIRNUM     3               // 需要掃描的目錄數(shù) 
  30.  
  31. // 函數(shù)聲明 
  32. INT32 SelectFlies(struct dirent *pDir); 
  33. void ScanDirAndGather(void); 
  34. void Sleep(UINT32 iCountMs); 
  35.  
  36.  
  37. /**************************************************************** 
  38. * 功能描述: 主函數(shù) 
  39. * 輸入?yún)?shù): 無(wú) 
  40. * 輸出參數(shù): 無(wú) 
  41. * 返 回 值: 0-執(zhí)行完成 
  42. * 其他說(shuō)明: 無(wú) 
  43. * 修改日期       版本號(hào)        修改人        修改內(nèi)容 
  44. ------------------------------------------------------------- 
  45. * 20160513        V1.0     Zhou Zhaoxiong     創(chuàng)建 
  46. ****************************************************************/ 
  47. INT32 main(void) 
  48.     // 獲取匯總文件的目錄 
  49.     snprintf(g_szGatherDir, sizeof(g_szGatherDir)-1, "%s/zhouzx/TestDir/GatherDir", getenv("HOME")); 
  50.  
  51.     // 獲取需要匯總的文件的前綴 
  52.     snprintf(g_szFilePrefix, sizeof(g_szFilePrefix)-1, "File_"); 
  53.  
  54.     // 獲取需要匯總的文件的后綴 
  55.     snprintf(g_szFileSuffix, sizeof(g_szFileSuffix)-1, ".txt"); 
  56.  
  57.     // 調(diào)用函數(shù)執(zhí)行文件的匯總 
  58.     while (1) 
  59.     { 
  60.         ScanDirAndGather(); 
  61.  
  62.         Sleep(60 * 1000);    // 每一分鐘執(zhí)行一次文件的匯總 
  63.     } 
  64.  
  65.     return 0; 
  66.  
  67.  
  68. /********************************************************************** 
  69. * 功能描述:根據(jù)前綴和后綴選擇文件 
  70. * 輸入?yún)?shù):dir-目錄 
  71. * 輸出參數(shù):無(wú) 
  72. * 返 回 值:0-失敗   1-成功 
  73. * 其它說(shuō)明:無(wú) 
  74. * 修改日期         版本號(hào)      修改人          修改內(nèi)容 
  75. -------------------------------------------------------------------- 
  76. * 20160513         V1.0    ZhouZhaoxiong        創(chuàng)建 
  77. ***********************************************************************/ 
  78. INT32 SelectFlies(struct dirent *pDir) 
  79.     INT32 iPrefixLen    = 0; 
  80.     INT32 iLoopFlag     = 0; 
  81.     INT32 iSelectResult = 0; 
  82.  
  83.     if (pDir == NULL
  84.     { 
  85.         printf("SelectFlies:input parameter is NULL!\n"); 
  86.         return 0; 
  87.     } 
  88.  
  89.     // 匹配文件前綴和后綴 
  90.     iPrefixLen = strlen(g_szFilePrefix);       // 前綴為g_szFilePrefix 
  91.     iSelectResult = ((0 == strncmp(pDir->d_name, g_szFilePrefix, iPrefixLen))  
  92.                      && ((strncmp(&pDir->d_name[strlen(pDir->d_name) - strlen(g_szFileSuffix)], g_szFileSuffix, strlen(g_szFileSuffix)) == 0))); 
  93.  
  94.     if (iSelectResult == 1)            // 找到了匹配前綴的文件 
  95.     { 
  96.         return 1; 
  97.     } 
  98.     else 
  99.     { 
  100.         return 0; 
  101.     } 
  102.  
  103.  
  104. /********************************************************************** 
  105. * 功能描述:掃描目錄并匯總前綴相同的文件 
  106. * 輸入?yún)?shù):無(wú) 
  107. * 輸出參數(shù):無(wú) 
  108. * 返 回 值:無(wú) 
  109. * 其它說(shuō)明:無(wú) 
  110. * 修改日期         版本號(hào)      修改人          修改內(nèi)容 
  111. -------------------------------------------------------------------- 
  112. * 20160513         V1.0    ZhouZhaoxiong        創(chuàng)建 
  113. ***********************************************************************/ 
  114. void ScanDirAndGather(void) 
  115.     INT32  iScanDirRet           = 0; 
  116.     UINT32 iDirIdx               = 0; 
  117.     UINT32 iFileIdx              = 0; 
  118.     UINT32 iFileCount            = 0; 
  119.     UINT32 iScanedNoFileDirCount = 0; 
  120.     UINT32 iFileSize             = 0; 
  121.     INT32  iRetVal               = 0; 
  122.     UINT8  szFileDir[256]        = {0}; 
  123.     UINT8  szScanedFile[512]     = {0}; 
  124.     UINT8  szCmdBuf[256]         = {0}; 
  125.     FILE  *fp                    = NULL
  126.     struct dirent **ppDirEnt     = NULL
  127.  
  128.     // 依次掃描各個(gè)目錄, 并匯總文件 
  129.     for (iDirIdx = 1; iDirIdx <= DIRNUM; iDirIdx ++) 
  130.     { 
  131.         memset(szFileDir, 0x00, sizeof(szFileDir)); 
  132.         snprintf(szFileDir, sizeof(szFileDir)-1, "%s/zhouzx/TestDir/FileDir%d", getenv("HOME"), iDirIdx); 
  133.  
  134.         iScanDirRet = scandir(szFileDir, &ppDirEnt, SelectFlies, alphasort); 
  135.         if (iScanDirRet < 0)   // 掃描目錄出錯(cuò) 
  136.         { 
  137.             printf("ScanDirAndGather:exec scandir failed, path=%s\n", szFileDir); 
  138.             return
  139.         } 
  140.         else if (iScanDirRet == 0)   // 目錄下無(wú)文件 
  141.         { 
  142.             printf("ScanDirAndGather:no satisfied file in directory %s\n", szFileDir); 
  143.  
  144.             iScanedNoFileDirCount ++; 
  145.             if (iScanedNoFileDirCount >= DIRNUM)    // 表示所有目錄下均無(wú)滿足條件的文件 
  146.             { 
  147.                 printf("ScanDirAndGather:scaned no satisfied files in all %d dirs\n", iScanedNoFileDirCount); 
  148.                 return
  149.             } 
  150.         } 
  151.         else          // 將滿足條件的文件移動(dòng)到匯總目錄中 
  152.         { 
  153.             for (iFileIdx = 0; iFileIdx < iScanDirRet; iFileIdx ++) 
  154.             { 
  155.                 // 先判斷掃描到的文件是否為空文件, 是則直接刪除, 不是才執(zhí)行移動(dòng)的操作 
  156.                 memset(szScanedFile, 0x00, sizeof(szScanedFile)); 
  157.                 snprintf(szScanedFile, sizeof(szScanedFile) - 1, "%s/%s", szFileDir, ppDirEnt[iFileIdx]->d_name); 
  158.                 fp = fopen(szScanedFile, "r"); 
  159.                 if (fp == NULL)          // 打開(kāi)文件失敗, 直接返回 
  160.                 { 
  161.                     printf("ScanDirAndGather:open file %s failed, please check!\n", szScanedFile); 
  162.                     return
  163.                 } 
  164.                 fseek(fp, 0, SEEK_END); 
  165.                 iFileSize = ftell(fp); 
  166.                 if (iFileSize == 0)     // 該文件為空文件 
  167.                 { 
  168.                     printf("ScanDirAndGather:%s is an empty file, so delete it directly!\n", szScanedFile); 
  169.                     memset(szCmdBuf, 0x00, sizeof(szCmdBuf)); 
  170.                     snprintf(szCmdBuf, sizeof(szCmdBuf) - 1, "rm %s", szScanedFile); 
  171.                     system(szCmdBuf); 
  172.                 } 
  173.                 else 
  174.                 { 
  175.                     memset(szCmdBuf, 0x00, sizeof(szCmdBuf)); 
  176.                     snprintf(szCmdBuf, sizeof(szCmdBuf) - 1, "mv %s %s", szScanedFile, g_szGatherDir); 
  177.                     system(szCmdBuf); 
  178.  
  179.                     printf("ScanDirAndGather:now, %s\n", szCmdBuf); 
  180.  
  181.                     iFileCount ++; 
  182.                 } 
  183.             } 
  184.         } 
  185.     } 
  186.  
  187.     printf("ScanDirAndGather:this time,totally moved %d file(s) to %s\n", iFileCount, g_szGatherDir); 
  188.  
  189.     return
  190.  
  191.  
  192. /********************************************************************** 
  193. * 功能描述: 程序休眠 
  194. * 輸入?yún)?shù): iCountMs-休眠時(shí)間(單位:ms) 
  195. * 輸出參數(shù): 無(wú) 
  196. * 返 回 值: 無(wú) 
  197. * 其它說(shuō)明: 無(wú) 
  198. * 修改日期      版本號(hào)       修改人        修改內(nèi)容 
  199. ------------------------------------------------------------------ 
  200. * 20160513       V1.0     Zhou Zhaoxiong     創(chuàng)建 
  201. ********************************************************************/  
  202. void Sleep(UINT32 iCountMs) 
  203.     struct timeval t_timeout = {0}; 
  204.  
  205.     if (iCountMs < 1000) 
  206.     { 
  207.         t_timeout.tv_sec  = 0; 
  208.         t_timeout.tv_usec = iCountMs * 1000; 
  209.     } 
  210.     else 
  211.     { 
  212.         t_timeout.tv_sec  = iCountMs / 1000; 
  213.         t_timeout.tv_usec = (iCountMs % 1000) * 1000; 
  214.     } 
  215.     select(0, NULLNULLNULL, &t_timeout);    // 調(diào)用select函數(shù)阻塞程序 
  216. }
  217.  

五、程序測(cè)試

將編寫(xiě)好的程序“FileGather.c”上傳到Linux機(jī)器,并使用“gcc -g -o FileGather FileGather.c”命令對(duì)該程序進(jìn)行編譯,生成“FileGather”文件。下面對(duì)程序進(jìn)行詳細(xì)的測(cè)試。

1.在啟動(dòng)程序之前,分別在源目錄FileDir1、FileDir2和FileDir3中放入文件File_1.txt、File_2.txt和File_3.txt,程序運(yùn)行情況如下:

  1. ScanDirAndGather:now, mv /home/zhou/zhouzx/TestDir/FileDir1/File_1.txt /home/zhou/zhouzx/TestDir/GatherDir  
  2. ScanDirAndGather:now, mv /home/zhou/zhouzx/TestDir/FileDir2/File_2.txt /home/zhou/zhouzx/TestDir/GatherDir  
  3. ScanDirAndGather:now, mv /home/zhou/zhouzx/TestDir/FileDir3/File_3.txt /home/zhou/zhouzx/TestDir/GatherDir  
  4. ScanDirAndGather:this time,totally moved 3 file(s) to /home/zhou/zhouzx/TestDir/GatherDir 

可以看到,源目錄中的三個(gè)文件已經(jīng)沒(méi)有了,它們被移動(dòng)到了結(jié)果目錄GatherDir中:

  1. ~/zhouzx/TestDir/GatherDir> ll  
  2. -rw——- 1 zhou users 12 2016-05-13 15:14 File_1.txt  
  3. -rw——- 1 zhou users 12 2016-05-13 15:14 File_2.txt 
  4. -rw——- 1 zhou users 12 2016-05-13 15:14 File_3.txt 

2.一段時(shí)間之后,在源目錄FileDir1中放入文件File1_4.txt,程序運(yùn)行情況如下:

  1. ScanDirAndGather:no satisfied file in directory /home/zhou/zhouzx/TestDir/FileDir1  
  2. ScanDirAndGather:no satisfied file in directory /home/zhou/zhouzx/TestDir/FileDir2  
  3. ScanDirAndGather:no satisfied file in directory /home/zhou/zhouzx/TestDir/FileDir3  
  4. ScanDirAndGather:scaned no satisfied files in all 3 dirs 

可以看到,因?yàn)榍熬Y不匹配,F(xiàn)ile1_4.txt文件仍然在源目錄FileDir1中:

  1. ~/zhouzx/TestDir/FileDir1> ll  
  2. -rw——- 1 zhou users 36 2016-05-13 15:19 File1_4.txt 

3.一段時(shí)間之后,在源目錄FileDir2中放入文件File_5.txt,在源目錄FileDir3中放入文件File_11.c,程序運(yùn)行情況如下:

  1. ScanDirAndGather:no satisfied file in directory /home/zhou/zhouzx/TestDir/FileDir1  
  2. ScanDirAndGather:now, mv /home/zhou/zhouzx/TestDir/FileDir2/File_5.txt /home/zhou/zhouzx/TestDir/GatherDir  
  3. ScanDirAndGather:no satisfied file in directory /home/zhou/zhouzx/TestDir/FileDir3  
  4. ScanDirAndGather:this time,totally moved 1 file(s) to /home/zhou/zhouzx/TestDir/GatherDir 

可以看到,源目錄FileDir2中的文件File_5.txt已經(jīng)沒(méi)有了,因?yàn)楹缶Y不匹配,源目錄FileDir3中的文件File_11.c還存在:

  1. ~/zhouzx/TestDir/FileDir3> ll  
  2. -rw——- 1 zhou users 4 2016-05-13 15:23 File_11.c 

File_5.txt已經(jīng)被移動(dòng)到了結(jié)果目錄GatherDir中:

  1. ~/zhouzx/TestDir/GatherDir> ll  
  2. -rw——- 1 zhou users 12 2016-05-13 15:14 File_1.txt  
  3. -rw——- 1 zhou users 12 2016-05-13 15:14 File_2.txt  
  4. -rw——- 1 zhou users 12 2016-05-13 15:14 File_3.txt  
  5. -rw——- 1 zhou users 36 2016-05-13 15:23 File_5.txt 

4.一段時(shí)間之后,在源目錄FileDir2中放入空文件File_7.txt,程序運(yùn)行情況如下:

  1. ScanDirAndGather:no satisfied file in directory /home/zhou/zhouzx/TestDir/FileDir1  
  2. ScanDirAndGather:/home/zhou/zhouzx/TestDir/FileDir2/File_7.txt is an empty file, so delete it directly!  
  3. ScanDirAndGather:no satisfied file in directory /home/zhou/zhouzx/TestDir/FileDir3  
  4. ScanDirAndGather:this time,totally moved 0 file(s) to /home/zhou/zhouzx/TestDir/GatherDir 

可以看到,源目錄FileDir2中的文件File_7.txt已經(jīng)被刪除掉了:

  1. ~/zhouzx/TestDir/FileDir2> ll  
  2. total 0 

六、需求擴(kuò)展

基于本文中的需求和程序,可考慮對(duì)需求進(jìn)行以下擴(kuò)展:

1.在移動(dòng)文件之前,先查看相同文件名的文件在結(jié)果目錄中是否存在,如果存在,則直接將該文件在源目錄中刪除掉;如果不存在,才將該文件移動(dòng)到結(jié)果目錄中。

2.為避免結(jié)果目錄中的文件過(guò)多,可以在程序中添加清理機(jī)制,即將存放時(shí)間超過(guò)一定時(shí)長(zhǎng)的文件刪除掉。

3.為了體現(xiàn)程序的靈活性,可將部分文件信息(如文件前綴、后綴、存放目錄、掃描間隔時(shí)長(zhǎng)等)存放到配置文件中,程序在啟動(dòng)時(shí)讀取相關(guān)的配置項(xiàng)的值來(lái)執(zhí)行后續(xù)目錄掃描和文件移動(dòng)的操作。

【本文是51CTO專(zhuān)欄作者周兆熊的原創(chuàng)文章,作者微信公眾號(hào):周氏邏輯(logiczhou)】

戳這里,看該作者更多好文

責(zé)任編輯:武曉燕 來(lái)源: csdn博客
相關(guān)推薦

2016-12-29 11:02:13

源目錄前綴算法

2016-12-15 08:54:52

線程sessionopenSession

2020-07-19 10:23:13

C++進(jìn)制常量

2016-12-29 16:25:32

字符串算法代碼

2009-06-09 12:38:12

NetBeanseclipse

2009-07-22 17:15:04

C#實(shí)現(xiàn)

2019-08-20 10:24:39

HTTPSSSHLinux

2016-12-20 13:55:52

2019-07-10 11:10:25

Windows 10文件夾Windows

2016-12-29 17:14:41

回文串算法代碼

2016-12-30 13:37:50

字符串算法代碼

2021-08-16 20:48:34

嵌入式單片機(jī)信息

2016-12-30 13:32:24

字符串算法代碼

2017-08-17 10:53:10

Google代碼倉(cāng)庫(kù)

2009-08-31 13:53:03

C#創(chuàng)建一個(gè)文件

2022-07-26 00:00:02

TCPUDPMAC

2021-04-08 14:51:20

Python編碼語(yǔ)言

2021-05-28 18:12:51

C++類(lèi)設(shè)計(jì)

2015-10-16 13:41:52

程序對(duì)象設(shè)計(jì)

2021-05-06 21:49:56

索引掃描次序
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)