C实现搜索指定目录下的所有文件及其子目录下的文件

/*******************************************************************************
*@ Description    :搜索指定目录下的所有文件及其子目录下的文件
*@ Input          :
*@ Output         :
*@ Return         :
*@ attention      :
*******************************************************************************/
void getFileName(char *dirPath)
{
	DIR *dir=opendir(dirPath);
	if(dir==NULL)
	{
		ERROR_LOG("opendir failed!\n");
		return;
	}
	
	chdir(dirPath); 		//进入到当前读取目录eg:/tmp/sdcard/hle_camera
	struct dirent *ent;
	while ((ent=readdir(dir))!=NULL)
	{
		if(strcmp(ent->d_name,".")==0||strcmp(ent->d_name,"..")==0)
		{
			continue;
		}
		
		struct stat st;
		stat(ent->d_name,&st);
		if(S_ISDIR(st.st_mode)) //递归调用解析其子目录下的文件夹
		{
			getFileName(ent->d_name);
		}
		else //过滤输出保存mp4文件名
		{
			printf("%s\n",ent->d_name);

			//对文件名进行过滤,过滤掉不符合我们自己命名标准的文件
			if()
			{
				
			}
			
		}
	}
	closedir(dir);
	chdir("..");	//返回当前目录的上一级目录
}

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Java的File类和递归函数来实现复制某个目录及其包含的文件指定目录。代码如下: ``` import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class CopyDirectory { public static void main(String[] args) { String sourceDir = "C:/sourceDir"; // 源目录 String targetDir = "C:/targetDir"; // 目标目录 File source = new File(sourceDir); File target = new File(targetDir); try { copyDirectory(source, target); System.out.println("目录复制成功!"); } catch (IOException e) { System.out.println("目录复制失败:" + e.getMessage()); } } public static void copyDirectory(File source, File target) throws IOException { if (source.isDirectory()) { if (!target.exists()) { target.mkdirs(); } String[] fileList = source.list(); for (String fileName : fileList) { File srcFile = new File(source, fileName); File destFile = new File(target, fileName); copyDirectory(srcFile, destFile); } } else { FileInputStream inStream = new FileInputStream(source); FileOutputStream outStream = new FileOutputStream(target); byte[] buffer = new byte[1024]; int length; while ((length = inStream.read(buffer)) > 0) { outStream.write(buffer, 0, length); } inStream.close(); outStream.close(); } } } ``` 在代码,设置源目录和目标目录,并创建File对象。然后调用copyDirectory函数,该函数递归地遍历源目录下的所有文件子目录,如果是目录则创建目录,并继续递归遍历该目录下的所有文件子目录,如果是文件则进行复制操作。最后,在主函数进行调用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值