社区
C语言
帖子详情
怎么对当前目录下文件夹内的文件遍历
fujingn188
2006-02-25 09:49:01
如题
...全文
253
7
打赏
收藏
怎么对当前目录下文件夹内的文件遍历
如题
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
7 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
ywchen2000
2006-02-26
打赏
举报
回复
unix/linux 下
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<string.h>
#include <dirent.h>
void myfindfile(list<string> *needfile); //find file function
bool isfile(char* str)
{
long lens;
int i;
char *value,*newaddress;
lens=strlen(str);
for(i=lens;i>0;i--)
{
if(*(str+i)=='.')
{
value=(char *)malloc(4*sizeof(char));
newaddress=(str+i+1);
strcpy(value,newaddress);
if(strcmp(value,"map")==0)
{
free(value);
return true;
}
else
{
free(value);
return false;
}
}
}
return false;
}
char *fullpath;
void myfind(char* pathname,list<string> *xfilename)
{
int size;
size=pathconf("/",_PC_PATH_MAX);
size++;
fullpath=(char*)malloc(size);
strcpy(fullpath,pathname);
myfindfile(xfilename);
}
void myfindfile(list<string> *needfile) //find file function
{
DIR *sp;
struct dirent *dirp;
struct stat buf;
char *pathname,*newpathname,*ptr,*fullname,*tempstr;
long lens,newlen,fulllen,templen;
string newline;
ptr=fullpath+strlen(fullpath);
*ptr++='/';
*ptr=0;
sp=opendir(fullpath);
if(sp!=NULL)
{
while((dirp=readdir(sp))!=NULL)
{
strcpy(ptr,dirp->d_name);
if(lstat(fullpath,&buf)<0)
{
continue;
}
if(S_ISDIR(buf.st_mode))
{
if(strcmp(dirp->d_name,".")==0 || strcmp(dirp->d_name,"..")==0)
{
continue;
}
myfindfile(needfile);
}
if(S_ISREG(buf.st_mode))
{
if(isfile(dirp->d_name)==true)
{
newline=fullpath;
// cout<<newline<<endl;
needfile->createlist(newline);
}
}
else
{
continue;
}
}
}
if(closedir(sp)<0)
{
printf("close dir failed");
}
}
sk_fault
2006-02-26
打赏
举报
回复
学习学习!!
Rick_ang
2006-02-25
打赏
举报
回复
我看了我们的课本..有一个项目是做一个Shell..其中就有要求类似DOS中输入Dir 显示当前目录的功能..正干瞪眼呢呵呵..我也很喜欢OS..
du51
2006-02-25
打赏
举报
回复
OS用不到.OS我特别喜欢.学得算是比较用功的.
主要是进程那一块.内存那一块.
Rick_ang
2006-02-25
打赏
举报
回复
好..学习了..这学期学OS估计能用到
du51
2006-02-25
打赏
举报
回复
#include<iostream>
#include<string>
#include<io.h>
using namespace std;
void filesearch(string path,int layer)
{
struct _finddata_t filefind;
string curr=path+"\\*.*";
int done=0,i,handle;
if((handle=_findfirst(curr.c_str(),&filefind))==-1)return;
while(!(done=_findnext(handle,&filefind)))
{
if(strcmp(filefind.name,".."))//不返回
{
if ((_A_SUBDIR==filefind.attrib))
{
for(i=0;i<layer;i++)cout<<" ";
cout<<filefind.name<<"(dir)"<<endl;
curr=path+"\\"+filefind.name;
filesearch(curr,layer+1);
}
else
{
for(i=0;i<layer;i++)cout<<" ";
cout<<filefind.name<<endl;
}
}
}
_findclose(handle);
}
int main()
{
string path;
cout<<"请输入目录"<<endl;
cin>>path;
filesearch(path,0);
system("PAUSE");
return 0;
}
llf_hust
2006-02-25
打赏
举报
回复
函数名: findfirst, findnext
功 能: 搜索磁盘目录; 取得下一个匹配的findfirst模式的文件
用 法: int findfirst(char *pathname, struct ffblk *ffblk, int attrib);
int findnext(struct ffblk *ffblk);
程序例:
/* findnext example */
#include <stdio.h>
#include <dir.h>
int main(void)
{
struct ffblk ffblk;
int done;
printf("Directory listing of *.*\n");
done = findfirst("*.*",&ffblk,0);
while (!done)
{
printf(" %s\n", ffblk.ff_name);
done = findnext(&ffblk);
}
return 0;
}
BAT 一键
遍历
出
当前目录
及子
文件
夹
下指定类型的
文件
这是一个用于在Windows系统中搜索特定
文件
类型的批处理脚本。用户输入
文件
扩展名,脚本会
遍历
当前目录
及其子
文件
夹
,将找到的
文件
列表保存到一个文本
文件
中,并自动打开供用户查看。用户可连续搜索不同类型的
文件
。
遍历
当前目录
,提取指定
文件
夹
内容并拷贝到指定位置
本脚本为Windows下的bat脚本,用于
遍历
当前目录
及其子目录,查找并复制所有名为“应用程序”的
文件
夹
内容至指定目标路径。复制操作采用xcopy命令,确保完整包括所有子目录。
C/C++获取
当前目录
,
文件
列表,
遍历
文件
夹
和
文件
本文介绍了一套适用于C++17以下版本的跨平台
文件
操作代码,支持Windows和Linux系统,包括获取当前路径、创建
文件
夹
、列出指定目录下的
文件
等功能,并提供了递归
遍历
文件
夹
和
文件
的方法。
遍历
当前目录
删除指定的
文件
夹
——批处理
本文介绍了一种使用Windows批处理脚本来批量删除无用的temp
文件
夹
的方法,通过创建并运行一个.bat
文件
,可以轻松地清理指定目录下所有的temp
文件
夹
,提高工作效率。
C++
遍历
读取
文件
夹
(仅限
当前目录
而不深入)下的
文件
本文介绍了一个C++函数,该函数用于
遍历
指定路径下的所有
文件
(不包括子目录)。通过提供路径和可选的
文件
类型参数,可以获取特定类型的
文件
列表。
C语言
70,038
社区成员
243,247
社区内容
发帖
与我相关
我的任务
C语言
C语言相关问题讨论
复制链接
扫一扫
分享
社区描述
C语言相关问题讨论
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章