c++ Windows下超時文件刪除

操作步驟

  1. 取得某個目錄下面所有文件

  2. 取得文件的創建日期

  3. 取得當前日期跟其創建的日期差

  4. 刪除文件

獲取文件的創建時間

    int iresult;
    struct _stat buf;

    iresult = _stat("D:\\test.txt", &buf);
    printf("seconds of file create-time from 1970 :%d\n", buf.st_atime);
    __time64_t* m_seconds = &buf.st_ctime;//create 文件創建時間   注:buf.st_atime:訪問時間  buf.st_mtime:修改時間
    struct tm* m_localTime = localtime(m_seconds);//轉換成 標準時間格式
    printf("file ctime time : %d:%d:%d\n", m_localTime->tm_hour, m_localTime->tm_min, m_localTime->tm_sec);

獲取當前時間

    __time64_t* mptr_currentSeconds = new __time64_t;
    time(mptr_currentSeconds);
    printf("current seconds from 1970 :%d\n", *mptr_currentSeconds);
    m_localTime = localtime(mptr_currentSeconds);
    printf("current Local time : %d:%d:%d\n", m_localTime->tm_hour, m_localTime->tm_min, m_localTime->tm_sec);

獲取文件列表下所有文件

void getFiles(string path,__time64_t currentTime)
{
    //文件句柄  
    long   hFile = 0;
    //文件信息  
    struct _finddata_t fileinfo;
    string p;
    if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)
    {
        do
        {
            //如果是目錄,迭代之  
            //如果不是,加入列表  
            if ((fileinfo.attrib &  _A_SUBDIR))
            {
                if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
                    getFiles(p.assign(path).append("\\").append(fileinfo.name),currentTime);
            }
            else
            {
                int iresult;
                struct _stat buf;

                iresult = _stat(p.assign(path).append("\\").append(fileinfo.name).c_str(), &buf);
                cout << "currentTime" << currentTime << "   " << fileinfo.name << "   " << buf.st_mtime << endl;//打印文件的修改時間
                if ((currentTime-buf.st_atime)>12000)
                {
                    //這裏可以對超時文件進行操作
                }
            }
        } while (_findnext(hFile, &fileinfo) == 0);
        _findclose(hFile);
    }
}

時間結構體與函數參考

//<sys/stat.h>
     struct stat {
         dev_t      st_dev;    /* device inode resides on */
         ino_t      st_ino;    /* inode's number */
         mode_t     st_mode;   /* inode's mode */
         nlink_t    st_nlink;  /* number of hard links to the file */
         uid_t      st_uid;    /* user ID of owner */
         gid_t      st_gid;    /* group ID of owner */
         dev_t      st_rdev;   /* device type, for special file inode */
         struct timespec st_atimespec;  /* time of last access */
         struct timespec st_mtimespec;  /* time of last data modification */
         struct timespec st_ctimespec;  /* time of last file status change */
         off_t      st_size;   /* file size, in bytes */
         int64_t    st_blocks; /* blocks allocated for file */
         u_int32_t  st_blksize;/* optimal file sys I/O ops blocksize */
         u_int32_t  st_flags;  /* user defined flags for file */
         u_int32_t  st_gen;    /* file generation number */
     };

時間的轉換

struct tm                                                     
{                                                                
 int tm_sec; /*秒,0-59*/                               
 int tm_min; /*分,0-59*/                               
 int tm_hour; /*時,0-23*/                              
 int tm_mday; /*天數,1-31*/                          
 int tm_mon; /*月數,0-11*/                           
 int tm_year; /*自1900的年數*/                     
 int tm_wday; /*自星期日的天數0-6*/             
 int tm_yday; /*自1月1日起的天數,0-365*/      
 int tm_isdst; /*是否採用夏時制,採用爲正數*/   
}                                                                

日期貯存結構date



struct date                              
{                                            
 int da_year; /*自1900的年數*/ 
 char da_day; /*天數*/             
 char da_mon; /*月數 1=Jan*/ 
}                                           

時間貯存結構time

struct time                                 
{                                              
 unsigned char ti_min; /*分鐘*/    
 unsigned char ti_hour; /*小時*/  
 unsigned char ti_hund;               
 unsigned char ti_sec; /*秒*/       

}
char *ctime(long *clock)
本函數把clock所指的時間(如由函time返回的時間)轉換成數下列格式的字符串:
Mon Nov 21 11:31:54 1983n
char asctime(struct tm *tm)
本函數把指定的tm結構類的時間轉換成下列格式的字符串:
Mon Nov 21 11:31:54 1983n
double difftime(time_t time2,time_t time1)
計算結構time2和time1之間的時間差距(以秒爲單位)
struct tm *gmtime(long *clock)
本函數把clock所指的時間(如由函數time返回的時間)轉換成格林威治時間,並以tm結構形式返回
struct tm *localtime(long *clock)
本函數把clock所指的時間(如函數time返回的時間)轉換成當地標準時間,並以tm結構形式返回
void tzset()
本函數提供了對UNIX操作系統的兼容性
long dostounix(struct date *dateptr,struct time *timeptr)
本函數將dateptr所指的日期,timeptr所指的時間轉換成UNIX格式, 並返回自格林威治時間1970年1月1日凌晨起到現在的秒數
void unixtodos(long utime,struct date *dateptr,struct time *timeptr)
本函數將自格林威治時間1970年1月1日凌晨起到現在的秒數utime轉換成DOS格式並保存於用戶所指的結構dateptr和timeptr中
void getdate(struct date *dateblk)
本函數將計算機內的日期寫入結構dateblk中以供用戶使用
void setdate(struct date *dateblk)
本函數將計算機內的日期改成由結構dateblk所指定的日期
void gettime(struct time *timep)
本函數將計算機內的時間寫入結構timep中, 以供用戶使用
void settime(struct time *timep)
本函數將計算機內的時間改爲由結構timep所指的時間
long time(long *tloc)
本函數給出自格林威治時間1970年1月1日凌晨至現在所經過的秒數,並將該值存於tloc所指的單元中.
int stime(long *tp)本函數將tp所指的時間(例如由time所返回的時間)寫入計算機中.

參考網址

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章