Linux之根据时间段查询目录底下文件名


Linux之根据时间段查询目录底下文件名

文件名如下: 

  1. 1970_01_01_00_05_07_picture_000000231.jpg  
  2. 1970_01_01_00_05_08_picture_000000232.jpg  
  3. 1970_01_01_00_05_10_picture_000000233.jpg  
  4. 1970_01_01_00_05_11_picture_000000234.jpg  
  5. 1970_01_01_00_05_12_picture_000000235.jpg  
  6. 1970_01_01_00_05_13_picture_000000236.jpg  
  7. 1970_01_01_00_05_14_picture_000000237.jpg  
  8. 1970_01_01_00_05_15_picture_000000238.jpg  
  9. 1970_01_01_00_05_16_picture_000000239.jpg  
  10. 1970_01_01_00_05_18_picture_000000240.jpg  
  11. 1970_01_01_00_05_19_picture_000000241.jpg  
  12. 1970_01_01_00_05_20_picture_000000242.jpg  
  13. 1970_01_01_00_05_21_picture_000000243.jpg  
  14. 1970_01_01_00_05_22_picture_000000244.jpg  
  15. 1970_01_01_00_05_23_picture_000000245.jpg  
  16. 1970_01_01_00_05_25_picture_000000246.jpg  
  17. 1970_01_01_00_05_26_picture_000000247.jpg  
  18. 1970_01_01_00_05_27_picture_000000248.jpg  
  19. 1970_01_01_00_05_28_picture_000000249.jpg  
 
  1. #include <QtCore/QCoreApplication>   
  2. #include <sys/types.h>   
  3. #include <dirent.h>   
  4. #include <stdio.h>   
  5. #include <stdlib.h>   
  6. #include <vector>   
  7. #include <iostream>   
  8. #include <string>   
  9. using namespace std;  
  10.   
  11. int main(int argc, char *argv[])  
  12. {  
  13.     QCoreApplication a(argc, argv);  
  14.   
  15.    
  16.     string str2= "1970_01_01_00_45_01";  
  17.     string str1 = "1970_01_01_00_44_00";  
  18.       
  19.     DIR*   dp;  
  20.     struct   dirent*   dirp;  
  21.   
  22.     if((dp   =   opendir("/mnt/picture"))   ==   NULL   )  
  23.     {  
  24.         printf( "Can   not   open\n\n ");  
  25.   
  26.     }  
  27.     vector<string> vecPicname;  
  28.     bool flag1=false,flag2=false;  
  29.     while((dirp   =   readdir(dp))   !=   NULL   )  
  30.     {  
  31.         if(dirp->d_name[0] =='.' )  
  32.         {  
  33.             continue;  
  34.         }  
  35.   
  36.         printf( "%s\n ",dirp-> d_name);  
  37.         string strTemp = dirp-> d_name;  
  38.         strTemp = strTemp.substr(0,19);  
  39.   
  40.         if(strTemp.compare(str1)>=0 &&strTemp.compare(str2)<=0)  
  41.         {  
  42.             cout<<"wyz---------"<<strTemp<<endl;  
  43.   
  44.             vecPicname.push_back(dirp-> d_name);  
  45.         }  
  46.   
  47.   
  48.   
  49.         if(strTemp.compare(str1) ==0)  
  50.         {  
  51.             flag1=true;  
  52.         }  
  53.         if(strTemp.compare(str2) ==0)  
  54.         {  
  55.             flag2=true;  
  56.         }  
  57.   
  58.         if(flag1==true && flag2==true)  
  59.         {  
  60.             break;  
  61.         }  
  62.   
  63.     }  
  64.   
  65.   
  66.     for(unsigned int i=0; i<vecPicname.size(); i++)  
  67.     {  
  68.         cout<<"wyz------"<<vecPicname[i]<<endl;  
  69.     }  
  70.   
  71.     closedir(dp);  
  72.     return a.exec();  
  73. }  

结果查询到在某时间段自己想要的文件名 ,及图片

 

            for(int i=0; i<100; i++)    //删除最早的抓拍图片100张
            {
                system("rm -f /sddisk/picture/$(ls /sddisk/picture -rt | sed -n '1p')");
            }

相关内容