How can i find the path of a file?

214 ビュー (過去 30 日間)
Arnaud Dupont
Arnaud Dupont 2016 年 8 月 19 日
コメント済み: Guillaume 2016 年 8 月 19 日
First i will explain my situation. I have a folder that has a lot of sublevels and one of those subfolders contains the file i need.
main folder
subfolder1
subfolder1.1
subfolder1.1.1
subfolder1.1.2
subfolder1.1.3
File.sldd
subfolder1.1.4
...
subfolder1.2
...
subfolder2
...
I know the name of the file: File.sldd
But now I want to know the full path of the file so i can add this file to my current folder and open it with fopen(). The main folder is already added to my current folder. Is there a command that has the name of the file as input and gives me the path as output? Or is there a quick way to get the path of the file?
Thanks
Arnaud

回答 (3 件)

Honglei Chen
Honglei Chen 2016 年 8 月 19 日
Try
fileparts(which('File.sldd'))
But your File.sldd has to be on path.
  4 件のコメント
Arnaud Dupont
Arnaud Dupont 2016 年 8 月 19 日
sorry! my mistake
Honglei Chen
Honglei Chen 2016 年 8 月 19 日
Then you'll have to write a script yourself to find this file. You can use the dir command to list all the files/subfolders under current folder and keeps repeating for each subfolder until you find that file. The result of dir will contain the path when you find your file.

サインインしてコメントする。


Guillaume
Guillaume 2016 年 8 月 19 日
編集済み: Guillaume 2016 年 8 月 19 日
"so i can add this file to my current folder and open it with fopen()"
That is completely unnecessary. You don't need to (and shouldn't) add the folder of the file to the path in order to open it with fopen. Just pass the full path of the file to fopen.
There is no function in matlab to do what you want (yet! there will be in R2014b). You have to recurse through the directory using dir. This should work:
function filefolder = findfile(root, filename)
dircontent = dir(fullfile(root, '*'));
if any(strcmpi({dircontent(~[dircontent.isdir]).name}, filename)) %case insensitive comparison. Works for Windows only.
%file is found in directory
filefolder = root;
else
%look in subdirectories
for subdir = dircontent([dircontent.isdir] & ~ismember({dircontent.name}, {'.', '..'}))'
filefolder = findfile(fullfile(root, subdir.name), filename);
if ~isempty(filefolder)
%found in a directory, all done
return;
end
end
%looked in all subdirectories and not found
filefolder = ''; %return empty
end
end
You can then do:
folder = findfile(mainfolder, 'File.sldd');
fid = fopen(fullfile(folder, 'File.sldd'));

Image Analyst
Image Analyst 2016 年 8 月 19 日
You can use genpath to make a list of all folders, then use dir() to see if the file you want is in that folder. See attached demo.
  1 件のコメント
Guillaume
Guillaume 2016 年 8 月 19 日
Note that genpath will ignore any folder named private, and any folder beginning with @ or +. It's designed for manipulating matlab toolbox folders, not arbitrary folders.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by