linux --> 删除指定目录下所有文件
删除指定目录下所有文件
代码样例:
/////////////////////////////////////////////////////
//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 --> 删除指定目录下所有文件的更多相关文章
- linux find-在指定目录下查找文件
推荐:更多Linux 文件查找和比较 命令关注:linux命令大全 find命令用来在指定目录下查找文件.任何位于参数之前的字符串都将被视为欲查找的目录名.如果使用该命令时,不设置任何参数,则find ...
- java递归删除指定目录下的文件和文件夹
public static boolean deleteFolder(String delDir) { File delFolder = new File(delDir); File[] delFil ...
- Linux复制指定目录下的文件夹结构
[root@ebs12vis ~]# su - applmgr[applmgr@ebs12vis ~]$ cd $APPL_TOP/inv[applmgr@ebs12vis inv]$ find . ...
- linux复制指定目录下的全部文件到另一个目录中
linux复制指定目录下的全部文件到另一个目录中复制指定目录下的全部文件到另一个目录中文件及目录的复制是经常要用到的.linux下进行复制的命令为cp.假设复制源目录 为 dir1 ,目标目录为dir ...
- linux复制指定目录下的全部文件到另一个目录中,linux cp 文件夹
linux复制指定目录下的全部文件到另一个目录中复制指定目录下的全部文件到另一个目录中文件及目录的复制是经常要用到的.linux下进行复制的命令为cp.假设复制源目录 为 dir1 ,目标目录为dir ...
- [No000073]C#直接删除指定目录下的所有文件及文件夹(保留目录)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- Python批量删除指定目录下的指定类型的文件
Python作为一种脚本语言.其很适合文件级的各种操作.以下的代码能够批量删除指定目录下的所有特定类型(CSV类型)的文件. import sys, csv , operator import os ...
- C# 删除指定目录下的所有文件及文件夹
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...
- C#直接删除指定目录下的所有文件及文件夹(保留目录)
#region 直接删除指定目录下的所有文件及文件夹(保留目录) /// <summary> /// 直接删除指定目录下的所有文件及文件夹(保留目录) /// </summary&g ...
随机推荐
- 任务栈 启动模式 Task Flag launchMode MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- Android -- Fragment注意事项
ViewPager+Fragment 让Fragment成为ViewPager的一页时,FragmentManager会一直保存管理创建好了的Fragment,即使当前不是显示的这一页,Fragmen ...
- ListView GridViewColumn.CellTemplate
<ListView.View> <GridView> <GridViewColumn Header="Meaningful Use Objectives&quo ...
- Memcached和Memcache 配置教程windows X64
一.Memcached和Memcache的区别: 网上关于Memcached和Memcache的区别的理解众说纷纭,我个人的理解是: Memcached是一个内存缓存系统,而Memcache是php的 ...
- Run Repository Creation Utility (RCU) for Oracle Identity Management components
Run Repository Creation Utility (RCU) for Oracle Identity Management components Installing O ...
- PL/SQL之DBMS_SQL程序包使用(1)(学习笔记)
dbms_sql程序包dbms_sql程序包是系统提供给我们的另一种使用动态SQL的方法:使用DBMS_SQL包实现动态的SQL的步骤如下:1.将要执行的SQL语句或者一个语句放到一个字符串变量中2. ...
- ViewPage + RadioGroup + Fragment学习
底部是RadioGroup中RadioButton的切换.上面时ViewPage ,能够滑动,假设你们的需求是不须要滑动的话,那就直接用FrameLayout就能够了. 以下将会用两种方式实现.请大家 ...
- linux的fork()函数-进程控制
进程作为构成系统的基本细胞,不仅是系统中独立活动的实体,而且是独立竞争资源的基本实体.它要经历创建.执行.等待.终止等一系列过程. 一.fork入门知识(转载) 一个进程,包括代码.数据和分配给进程的 ...
- CSS3 选择器浏览器兼容性汇总 IE8
1.css选择器 css(包括css1.css2和css3)有哪些选择器? http://www.w3school.com.cn/cssref/css_selectors.asp 2.CSS3选择器 ...
- 腾讯地图api将物理地址转化成坐标
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...