全部版块 我的主页
论坛 金融投资论坛 六区 金融学(理论版) 金融工程(数量金融)与金融衍生品
4827 5
2013-05-09
由于个人需要,需要编写一个显示文件完整路径的程序,自己尝试了一下,但是出现了一些问题,希望高手指教

程序要求:

输入: 起始文件路径
输出: 文件夹以及其子文件夹中所有文件完整路径
程序名 find_path
例如:
     d盘中有: 文件夹 1 ,2 ,3
                  文件 1.txt  2.txt  3.txt
     文件夹1中有: 文件 a.txt b.txt 文件夹 a b
        文件夹 2 3 a b 都是空的
    运行   find_path('d:\')
    输出结果: d:\1.txt
                     d:\2.txt
                     d:\3.txt
                     d:\1\a.txt
                     d:\1\b.txt
                     d:\1\c.txt
输出各个文件的顺序可以不同

在下主要有两个问题需要解决: 1、函数中数据的存储用什么格式
                                              2、迭代的具体实现

这是我编写的程序:


function full_path=find_path(start_path)
path_name=dir(start_path);
file_found_first=0;
file_found_next=0;
for i=3:length(path_name)
    if path_name(i).isdir
         file_found_next=find_path(strcat(start_path,'\',path_name(i).name));
    else
         file_found_first=strcat(start_path,'\',path_name(i).name);
    end
end
file_found={file_found_first file_found_next};
full_path=file_found;


希望高手不吝赐教。

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2013-5-10 00:09:44
1. You can save in a cell array and use char() to convert into string when you have further use.

2. You mean you don't know the number of the subdirectory in the path? That is a little complicated.

If you only have folder and txt file under the path. You can use dir() to get he dir info, and use .isdir to get the subfolders. Then you again use dir() to get the files and subsubfolders.

This way is only for finite number of subdirectories. If you only have the original directory and one layer of subdirectory d:\ and (d:\a d:\b)  I think you can use what I recommend.

best,
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2013-5-10 17:37:35
非常感谢您的回答。
不过我还有点儿问题。我的问题跟你说的第二点类似。
我想达到的效果是输入一个路径,函数返回输入路径中所有文件(包括所有子文件夹)的完整路径,如果某个子文件夹是空的,这个子文件夹的路径输出与否都可以。
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2013-5-10 17:39:37
Chemist_MZ 发表于 2013-5-10 00:09
1. You can save in a cell array and use char() to convert into string when you have further use.

...
您说的第一点很好,我下去再试一下
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2013-5-11 15:08:31
非常感谢matlabsky的smachine
我看了genpath()的源码,类似编了我的函数,达到了目的
我的源码:
find_path.m

function file_path=find_path(start_path)
%------------------------------------------------------
%参考内建函数genpath
%本函数返回所给文件夹中所有文件完整路径
%------------------------------------------------------

file_path=[];
file=dir(start_path);
isfile=logical(~cat(1,file.isdir));
for i=3:length(isfile)
    if isfile(i)
        file_path=[file_path fullfile(start_path,file(i).name) pathsep];
    else
        file_path=[file_path find_path(fullfile(start_path,file(i).name))];
    end
end
end

这样返回的是把找到的所有文件完整路径(包含文件名)连成了一个字符串,字符串之间按平台路径分隔符分割(windows下是;)
后续需要可以用regexp(find_path('your_path'),';','split')分割出来每个路径
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2013-5-11 18:28:03
little007 发表于 2013-5-11 15:08
非常感谢matlabsky的smachine
我看了genpath()的源码,类似编了我的函数,达到了目的
我的源码:
Ok, congradulations~
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群