In actual software development projects, there is often a need to implement the deletion of outdated files. This requires that the program be able to automatically delete files that were generated before the time. This article provides a sample code that provides a reference for related development work.
The program is as follows (this program compiles with makefile):
/**********************************************************************
* All rights reserved (C), Zhou Zhaoxiong.
*
* File name: testfiledelete.c
* Content Summary: Used to demonstrate the deletion of outdated files under Linux
* Other Instructions: none
* Current Version: V1.0
* Author: Zhou Zhaoxiong
* Completion Date: 20140811
*
* Version modification time modified by the person to modify the content
********************************************************************
* V1.0 20,140,811 week Mega Bear created
**********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Data type
typedef unsigned char UINT8;
typedef unsigned int UINT32;
typedef signed INT INT32;
/*---------------------------------------------------------------
* Function Description: main function
* Input Parameters: None
* Output Parameters: None
* Return value: 0-Execution end
* Other Instructions: none
* Modified Date version number modify the content of the person
* ---------------------------------------------------------------
* 20140811 V1.0 Zhou Zhaoxiong created
-----------------------------------------------------------------*/
INT32 Main ()
{
UINT8 szcmdbuf[1024] = {0}; For storing Linux commands
UINT8 szlocalfilesavedir[1000] = {0}; For storing local directories
UINT32 ifilesavedays = 10; The number of days the file is retained can be determined by the configuration item, which is set to 0
memcpy (Szlocalfilesavedir, "/home/zhouzx/testdir", strlen ("/home/zhouzx/testdir")); The file's storage path
//Note the format of the delete command
snprintf (szcmdbuf, sizeof (SZCMDBUF)-1, "find%s-name \" *.txt\ "-ctime +%d-exec rm-f {} \ \;", Szlocalfilesave Dir, ifilesavedays);
printf ("Delete the file (s), exec:%s\n", szcmdbuf);
System (SZCMDBUF); Execute Delete command
return 0;
}
The contents of the makefile file are as follows:
Testfiledelete:testfiledelete.c
Gcc-c-G testfiledelete.c
Gcc-g-O release/testfiledelete testfiledelete.o
RM *.O
Description
(1) The retention days and file directories in this article can be determined by configuration items in the configuration file, which can be flexibly configured as needed.
(2) to write code strictly according to the syntax of the "find" command, otherwise the command will not work. For specific syntax rules for this command, please check online.
(I Weibo: Http://weibo.com/zhouzxi?topnav=1&wvr=5, No.: 245924426, welcome attention!) )