Solved

How can I list current directory and subdirectory files as a vector?

Posted on 2010-11-28
6
747 Views
Last Modified: 2013-11-25
I want to list all files in the current directory and all the ones in any of its subdirectories in a vector. I know dir lists the files in the current directory as a struct array. I want to be able to search this list for a filename (as a string), maybe using findstr.
0
Comment
Question by:Mario271
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 2

Assisted Solution

by:Eyal-f
Eyal-f earned 150 total points
ID: 34226346
As you said, you can use dirto get a struct array with the list of files in the current directory, e.g.

filelist=dir('*.txt')

if you want to search through the file names, you can use arrayfun with, e.g. strcmp:

l=arrayfun(@(x)strcmpi(x.name,'aaa.txt'),filelist)

will return a vector the same length as filelist, with 1 at the index of the file named aaa.txt and 0 otherwise.

Another option is to convert filelist.name into a cell array. One way to do this is with the deal function:

A={};
[A{}]=deal(filelist.name)

and then you can use strcmp or other functions which operate on cell arrays, or use cellfun
0
 
LVL 2

Expert Comment

by:Eyal-f
ID: 34226378
As far as searching subdirectories - you can do this with a loop in two ways:

1) Recursively - use dir, find which files in the resulting array are folders, use dir on each of them, etc.

2) You can also generate a list of all the subfolders of a given directory using genpath. so, you could do something like this:

folderstring=genpath('d:\');
folderlist=regexp(folderstring,';','split')

and then loop through the elements of folderlist:

for k=1:length(folderlist)
   filelist=folderlist{k};
   :
end
0
 
LVL 2

Expert Comment

by:Eyal-f
ID: 34226386
Two corrections/notes:

1) when using genpath, I think the last character in the vector is a semicolon, so you should skip it in the loop one way or another (e.g. use k=1:length(folderlist)-1 or use an if statement to check the current folder name is a valid folder)
2) if you have a structure array of files generated by the dir structure, the isdir field of a gievn element is 1 if it is a folder.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Mario271
ID: 34227160
This is very helpful! Thank you. When I use genpath, am I putting in the name of my directory? I was trying it out and kept getting an empty string. I guess my question is what does the notation d:\ mean? Does it allow genpath to just automatically go through the current directory for subdirectories?
0
 
LVL 15

Accepted Solution

by:
yuk99 earned 350 total points
ID: 34227593
0
 
LVL 2

Expert Comment

by:Eyal-f
ID: 34227942
You need to input the name of the top directory you're searching, so in this example, genpath('d:\') will give a string containing the root of D: and all the folders and subfolders on the partition. for another example, genpath('c:\windows') will give you the windows folder and all its subfolders.
If you omit this argument, you will get the folders in matlab/toolbox.
0

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
gHappy challenge 67 135
tripleUp challenge 7 97
seriesUp challenge 7 201
What language/protocol is the Angular Chat? 2 112
There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question