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 ...
随机推荐
- [leetcode]Populating Next Right Pointers in Each Node @ Python
原题地址:https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node/ 题意: 1 / \ 2 3 / \ ...
- Android中ActionBar及Overflow的显示
最近在按照Android的API文档学习Android中actionbar的使用,Action bar 最基本的形式,就是为 activity 显示标题,并且在标题左边显示一个 app icon.在这 ...
- UNdelete
--90兼容模式以上,2005+ -- http://raresql.com/2012/10/24/sql-server-how-to-find-who-deleted-what-records-at ...
- effective C++中条款37:绝不又一次定义继承而来的缺省參数值
virtual 函数会动态绑定,而virtual函数的缺省參数值是静态绑定的. 用一个base类型的指针p去指向一个derived类对象.通过p调用虚函数时,会动态绑定到实际所指对象中的函数:用一个d ...
- jQuery cssHook的经典例子
/*--------------------------- example ------------------------------*/ $.cssHooks.foo = { get: fun ...
- (字符串)最长公共字串(Longest-Common-SubString,LCS)
题目: 给定两个字符串X,Y,求二者最长的公共子串,例如X=[aaaba],Y=[abaa].二者的最长公共子串为[aba],长度为3. 子序列是不要求连续的,字串必须是连续的. 思路与代码: 1.简 ...
- Discuz常见大问题-如何使用云采集插件
在百度中搜discuz 云采集插件(直接在Discuz官方的插件库中搜可能搜不到) 启用这个插件 随后在任意版块发帖的时候,顶部都会有一个文本框,可以直接放别人博客的帖子,点击获取将生成到你发的帖子中 ...
- 提高ASP.NET首页性能的十大方法
本文是我对ASP.NET页面载入速度提高的一些做法,这些做法分为以下部分: http://www.cnblogs.com/xiachufeng/archive/2011/11/09/2242130.h ...
- 如何不使用pthread_cancel而杀死线程
http://www.cnblogs.com/no7dw/archive/2012/09/27/2705847.html During the time I use standalone cross ...
- LoadRunner录制:关联
一般请求中看到 hash strings, random strings, session ID 这种,就需要动态去获取该内容,这样就需要用到关联. 回放时自动关联 1. 录制脚本并执行. 2. 打开 ...