删除指定目录下所有文件

代码样例:

/////////////////////////////////////////////////////
//Name: DeleteFile
//Purpose: Delete file in the special directory
//Author: xxxxxxxx
//Created: 2011-12-01
//Copy right:
//Licence:
////////////////////////////////////////////////////// #ifndef _DELETE_FILE
#define _DELETE_FILE
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <dirent.h>
#include <limits.h>
#include <string.h>
#include <stdio.h>
#include <limits.h> //判断是否为目录
bool is_dir(const char *path)
{
struct stat statbuf;
if(lstat(path, &statbuf) ==)//lstat返回文件的信息,文件信息存放在stat结构中
{
return S_ISDIR(statbuf.st_mode) != ;//S_ISDIR宏,判断文件类型是否为目录
}
return false;
} //判断是否为常规文件
bool is_file(const char *path)
{
struct stat statbuf;
if(lstat(path, &statbuf) ==)
return S_ISREG(statbuf.st_mode) != ;//判断文件是否为常规文件
return false;
} //判断是否是特殊目录
bool is_special_dir(const char *path)
{
return strcmp(path, ".") == || strcmp(path, "..") == ;
} //生成完整的文件路径
void get_file_path(const char *path, const char *file_name, char *file_path)
{
strcpy(file_path, path);
if(file_path[strlen(path) - ] != '/')
strcat(file_path, "/");
strcat(file_path, file_name);
} void delete_file(const char *path)
{
DIR *dir;
dirent *dir_info;
char file_path[PATH_MAX];
if(is_file(path))
{
remove(path);
return;
}
if(is_dir(path))
{
if((dir = opendir(path)) == NULL)
return;
while((dir_info = readdir(dir)) != NULL)
{
get_file_path(path, dir_info->d_name, file_path);
if(is_special_dir(dir_info->d_name))
continue;
delete_file(file_path);
rmdir(file_path);
}
}
}
int main(int argc, char **argv)
{
delete_file("/Users/jiangtengfei/WorkSpace/test_code/tmp/asd");
return ;
}
#endif

linux --> 删除指定目录下所有文件的更多相关文章

  1. linux find-在指定目录下查找文件

    推荐:更多Linux 文件查找和比较 命令关注:linux命令大全 find命令用来在指定目录下查找文件.任何位于参数之前的字符串都将被视为欲查找的目录名.如果使用该命令时,不设置任何参数,则find ...

  2. java递归删除指定目录下的文件和文件夹

    public static boolean deleteFolder(String delDir) { File delFolder = new File(delDir); File[] delFil ...

  3. Linux复制指定目录下的文件夹结构

    [root@ebs12vis ~]# su - applmgr[applmgr@ebs12vis ~]$ cd $APPL_TOP/inv[applmgr@ebs12vis inv]$ find . ...

  4. linux复制指定目录下的全部文件到另一个目录中

    linux复制指定目录下的全部文件到另一个目录中复制指定目录下的全部文件到另一个目录中文件及目录的复制是经常要用到的.linux下进行复制的命令为cp.假设复制源目录 为 dir1 ,目标目录为dir ...

  5. linux复制指定目录下的全部文件到另一个目录中,linux cp 文件夹

    linux复制指定目录下的全部文件到另一个目录中复制指定目录下的全部文件到另一个目录中文件及目录的复制是经常要用到的.linux下进行复制的命令为cp.假设复制源目录 为 dir1 ,目标目录为dir ...

  6. [No000073]C#直接删除指定目录下的所有文件及文件夹(保留目录)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  7. Python批量删除指定目录下的指定类型的文件

    Python作为一种脚本语言.其很适合文件级的各种操作.以下的代码能够批量删除指定目录下的所有特定类型(CSV类型)的文件. import sys, csv , operator import os ...

  8. C# 删除指定目录下的所有文件及文件夹

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...

  9. C#直接删除指定目录下的所有文件及文件夹(保留目录)

    #region 直接删除指定目录下的所有文件及文件夹(保留目录) /// <summary> /// 直接删除指定目录下的所有文件及文件夹(保留目录) /// </summary&g ...

随机推荐

  1. HTTP请求方法 GET POST【总结】

    HTTP 8种请求方法概述 HTTP/1.1协议中共定义了八种方法(有时也叫"动作"),分别为:get,post,put, options,head,delete,trace,co ...

  2. C# 特性(Attribute)(一)

    特性(Attributes)是一种崭新的声明性信息.我们不仅可以通过特性来定义设计层面的信息(例如help file, URL for documentation)以及运行时(run-time)信息( ...

  3. quickcocos2dx framework环境变 fatal error C1083: 无法打开源文件:“.Box2D/Dynamics/b2World.h”: No such file or d

      : fatal error C1083: 无法打开源文件:".Box2D/Dynamics/b2World.h": No such file or directory 解决方法 ...

  4. Android -- 图片异步上传到PHP服务器

    背景                                                                                           网上很多上传到 ...

  5. (转)PlayerPrefs游戏存档

    unity3d提供了一个用于本地持久化保存与读取的类——PlayerPrefs.工作原理非常简单,以键值对的形式将数据保存在文件中,然后程序可以根据这个名称取出上次保存的数值.    PlayerPr ...

  6. Mongo的安全验证

    参考如下的文档: https://docs.mongodb.org/manual/tutorial/enable-authentication/          1.1. 在启用匿名验证的情况下,创 ...

  7. VM虚拟机如何和主机共享文件夹或文件

    请一定要选中Map as a network drive in Windows guests,否则将无法查看共享.

  8. BIO、NIO、AIO差别

    网上非常多IO资料,对新手来说.越看越晕.依据自己的理解.总结对照了一下BIO.NIO.AIO. BIO:线程发起IO请求,无论内核是否准备好IO操作,从发起请求起,线程一直堵塞,直到操作完毕. 例如 ...

  9. 寻找[nginx] 由Lua 粘合的Nginx生态环境-- agentzh

    来自:linuxtone org     Chnangelog:         120312 fixed as s/hhttp/http/g ,thanx muxueqz         12030 ...

  10. maven 如何给web项目添加jar包依赖

      maven 如何给web项目添加jar包依赖 CreateTime--2018年4月19日19:06:21 Author:Marydon 开发工具:eclipse 1.打开pom.xml文件--& ...