将前缀和后缀相同的文件移动到同一个目录的算法设计及C代码实现

开发 开发工具 算法
在Linux系统的某几个目录下有一些前缀和后缀相同的文件,编写程序将它们移动到同一个目录下。

一、需求描述

在Linux系统的某几个目录下有一些前缀和后缀相同的文件,编写程序将它们移动到同一个目录下。

例如,有三个源目录FileDir1、FileDir2和FileDir3,里面分别存放有文件File_1.txt、File_2.txt和File_3.txt。由于它们有相同的前缀(File_)和后缀(txt),所以要将这三个文件移动到同一个结果目录(假设为GatherDir)中。

二、算法设计

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

图1 程序总体流程

三、特殊流程考虑

在编写程序的过程中,对于某些特殊流程的考虑如下:

1.如果扫描某个目录出错,则直接停止程序的运行,而不用继续扫描下一个目录。

2.对于某些空文件(即文件的大小为0),直接在源目录中将其删除,而不用移动到结果目录中。

3.为了随时能够处理放到源目录中的文件,程序每隔一段时间(如一分钟)扫描一次源目录。也就是说,如果不人为操作,程序启动之后会不停地运行。

四、程序代码

  1. /********************************************************************** 
  2. * 版权所有 (C)2016, Zhou Zhaoxiong。 
  3. * 文件名称:FileGather.c 
  4. * 文件标识:无 
  5. * 内容摘要:将各个目录中前缀相同的文件集中到一个目录中 
  6. * 其它说明:无 
  7. * 当前版本: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. // 重定义数据类型 
  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               // 需要扫描的目录数 
  30.  
  31. // 函数声明 
  32. INT32 SelectFlies(struct dirent *pDir); 
  33. void ScanDirAndGather(void); 
  34. void Sleep(UINT32 iCountMs); 
  35.  
  36.  
  37. /**************************************************************** 
  38. * 功能描述: 主函数 
  39. * 输入参数: 无 
  40. * 输出参数: 无 
  41. * 返 回 值: 0-执行完成 
  42. * 其他说明: 无 
  43. * 修改日期       版本号        修改人        修改内容 
  44. ------------------------------------------------------------- 
  45. * 20160513        V1.0     Zhou Zhaoxiong     创建 
  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.     // 调用函数执行文件的汇总 
  58.     while (1) 
  59.     { 
  60.         ScanDirAndGather(); 
  61.  
  62.         Sleep(60 * 1000);    // 每一分钟执行一次文件的汇总 
  63.     } 
  64.  
  65.     return 0; 
  66.  
  67.  
  68. /********************************************************************** 
  69. * 功能描述:根据前缀和后缀选择文件 
  70. * 输入参数:dir-目录 
  71. * 输出参数:无 
  72. * 返 回 值:0-失败   1-成功 
  73. * 其它说明:无 
  74. * 修改日期         版本号      修改人          修改内容 
  75. -------------------------------------------------------------------- 
  76. * 20160513         V1.0    ZhouZhaoxiong        创建 
  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. * 输入参数:无 
  107. * 输出参数:无 
  108. * 返 回 值:无 
  109. * 其它说明:无 
  110. * 修改日期         版本号      修改人          修改内容 
  111. -------------------------------------------------------------------- 
  112. * 20160513         V1.0    ZhouZhaoxiong        创建 
  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.     // 依次扫描各个目录, 并汇总文件 
  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)   // 扫描目录出错 
  136.         { 
  137.             printf("ScanDirAndGather:exec scandir failed, path=%s\n", szFileDir); 
  138.             return
  139.         } 
  140.         else if (iScanDirRet == 0)   // 目录下无文件 
  141.         { 
  142.             printf("ScanDirAndGather:no satisfied file in directory %s\n", szFileDir); 
  143.  
  144.             iScanedNoFileDirCount ++; 
  145.             if (iScanedNoFileDirCount >= DIRNUM)    // 表示所有目录下均无满足条件的文件 
  146.             { 
  147.                 printf("ScanDirAndGather:scaned no satisfied files in all %d dirs\n", iScanedNoFileDirCount); 
  148.                 return
  149.             } 
  150.         } 
  151.         else          // 将满足条件的文件移动到汇总目录中 
  152.         { 
  153.             for (iFileIdx = 0; iFileIdx < iScanDirRet; iFileIdx ++) 
  154.             { 
  155.                 // 先判断扫描到的文件是否为空文件, 是则直接删除, 不是才执行移动的操作 
  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)          // 打开文件失败, 直接返回 
  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. * 输入参数: iCountMs-休眠时间(单位:ms) 
  195. * 输出参数: 无 
  196. * 返 回 值: 无 
  197. * 其它说明: 无 
  198. * 修改日期      版本号       修改人        修改内容 
  199. ------------------------------------------------------------------ 
  200. * 20160513       V1.0     Zhou Zhaoxiong     创建 
  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);    // 调用select函数阻塞程序 
  216. }
  217.  

五、程序测试

将编写好的程序“FileGather.c”上传到Linux机器,并使用“gcc -g -o FileGather FileGather.c”命令对该程序进行编译,生成“FileGather”文件。下面对程序进行详细的测试。

1.在启动程序之前,分别在源目录FileDir1、FileDir2和FileDir3中放入文件File_1.txt、File_2.txt和File_3.txt,程序运行情况如下:

  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 

可以看到,源目录中的三个文件已经没有了,它们被移动到了结果目录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.一段时间之后,在源目录FileDir1中放入文件File1_4.txt,程序运行情况如下:

  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 

可以看到,因为前缀不匹配,File1_4.txt文件仍然在源目录FileDir1中:

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

3.一段时间之后,在源目录FileDir2中放入文件File_5.txt,在源目录FileDir3中放入文件File_11.c,程序运行情况如下:

  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已经没有了,因为后缀不匹配,源目录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已经被移动到了结果目录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.一段时间之后,在源目录FileDir2中放入空文件File_7.txt,程序运行情况如下:

  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已经被删除掉了:

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

六、需求扩展

基于本文中的需求和程序,可考虑对需求进行以下扩展:

1.在移动文件之前,先查看相同文件名的文件在结果目录中是否存在,如果存在,则直接将该文件在源目录中删除掉;如果不存在,才将该文件移动到结果目录中。

2.为避免结果目录中的文件过多,可以在程序中添加清理机制,即将存放时间超过一定时长的文件删除掉。

3.为了体现程序的灵活性,可将部分文件信息(如文件前缀、后缀、存放目录、扫描间隔时长等)存放到配置文件中,程序在启动时读取相关的配置项的值来执行后续目录扫描和文件移动的操作。

【本文是51CTO专栏作者周兆熊的原创文章,作者微信公众号:周氏逻辑(logiczhou)】

戳这里,看该作者更多好文

责任编辑:武晓燕 来源: csdn博客
相关推荐

2016-12-29 11:02:13

源目录前缀算法

2016-12-15 08:54:52

线程sessionopenSession

2020-07-19 10:23:13

C++进制常量

2009-06-09 12:38:12

NetBeanseclipse

2016-12-29 16:25:32

字符串算法代码

2019-08-20 10:24:39

HTTPSSSHLinux

2016-12-20 13:55:52

2019-07-10 11:10:25

Windows 10文件夹Windows

2009-07-22 17:15:04

C#实现

2016-12-29 17:14:41

回文串算法代码

2021-08-16 20:48:34

嵌入式单片机信息

2017-08-17 10:53:10

Google代码仓库

2016-12-30 13:37:50

字符串算法代码

2022-07-26 00:00:02

TCPUDPMAC

2021-04-08 14:51:20

Python编码语言

2009-08-31 13:53:03

C#创建一个文件

2015-10-16 13:41:52

程序对象设计

2021-05-06 21:49:56

索引扫描次序

2016-12-30 13:32:24

字符串算法代码

2024-03-18 08:21:06

TCPUDP协议
点赞
收藏

51CTO技术栈公众号