Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
find file in folder or sub folders

Subject: find file in folder or sub folders

From: Stefan Heinen

Date: 5 Mar, 2011 12:09:21

Message: 1 of 10

Hey,

Thus matlab have a command that can search a directory and all sub folders. Something like:

function ( 'D:\My Documents\My Music' , '*.mp3' )

So the function returns all names containing the extension .mp3, even if the are in a folder like:

D:\My Documents\My Music\2 many dj's\As Heard on Radio Soulwax, Pt. 2

thx,

Stefan

Subject: find file in folder or sub folders

From: Nasser M. Abbasi

Date: 5 Mar, 2011 12:22:35

Message: 2 of 10

On 3/5/2011 4:09 AM, Stefan Heinen wrote:
> Hey,
>
> Thus matlab have a command that can search a directory and all sub folders. Something like:
>
> function ( 'D:\My Documents\My Music' , '*.mp3' )
>
> So the function returns all names containing the extension .mp3, even if the are in a folder like:
>
> D:\My Documents\My Music\2 many dj's\As Heard on Radio Soulwax, Pt. 2
>
> thx,
>
> Stefan

Yes, it has. It is called system()

using system, you can use the your host command to do the search.

-------------------

EDU>> [status,list]=system('dir *.m');
EDU>> list
.....
  Directory of C:\Users\me\Documents\MATLAB

02/04/2011 10:19 PM 78 foo.m
etc...
----------------------

If you want to search a whole tree, then find the DOS command to do
that.

For example, on unix one uses the find command. I am sure DOS has
something like that. You can use DOS tree command for example.

--Nasser

Subject: find file in folder or sub folders

From: Stefan Heinen

Date: 5 Mar, 2011 17:13:08

Message: 3 of 10

"Nasser M. Abbasi" <nma@12000.org> wrote in message <ikt9ub$6ch$1@speranza.aioe.org>...
> On 3/5/2011 4:09 AM, Stefan Heinen wrote:
> > Hey,
> >
> > Thus matlab have a command that can search a directory and all sub folders. Something like:
> >
> > function ( 'D:\My Documents\My Music' , '*.mp3' )
> >
> > So the function returns all names containing the extension .mp3, even if the are in a folder like:
> >
> > D:\My Documents\My Music\2 many dj's\As Heard on Radio Soulwax, Pt. 2
> >
> > thx,
> >
> > Stefan
>
> Yes, it has. It is called system()
>
> using system, you can use the your host command to do the search.
>
> -------------------
>
> EDU>> [status,list]=system('dir *.m');
> EDU>> list
> .....
> Directory of C:\Users\me\Documents\MATLAB
>
> 02/04/2011 10:19 PM 78 foo.m
> etc...
> ----------------------
>
> If you want to search a whole tree, then find the DOS command to do
> that.
>
> For example, on unix one uses the find command. I am sure DOS has
> something like that. You can use DOS tree command for example.
>
> --Nasser

Oke thx. So I tried

[status,list]=system('dir *.mp3');

which gives a list of all my mp3 files in the directory. But not in the sub folders. That's where I'm looking for.

Subject: find file in folder or sub folders

From: Stefan Heinen

Date: 5 Mar, 2011 18:43:05

Message: 4 of 10

"Nasser M. Abbasi" <nma@12000.org> wrote in message <ikt9ub$6ch$1@speranza.aioe.org>...
> On 3/5/2011 4:09 AM, Stefan Heinen wrote:
> > Hey,
> >
> > Thus matlab have a command that can search a directory and all sub folders. Something like:
> >
> > function ( 'D:\My Documents\My Music' , '*.mp3' )
> >
> > So the function returns all names containing the extension .mp3, even if the are in a folder like:
> >
> > D:\My Documents\My Music\2 many dj's\As Heard on Radio Soulwax, Pt. 2
> >
> > thx,
> >
> > Stefan
>
> Yes, it has. It is called system()
>
> using system, you can use the your host command to do the search.
>
> -------------------
>
> EDU>> [status,list]=system('dir *.m');
> EDU>> list
> .....
> Directory of C:\Users\me\Documents\MATLAB
>
> 02/04/2011 10:19 PM 78 foo.m
> etc...
> ----------------------
>
> If you want to search a whole tree, then find the DOS command to do
> that.
>
> For example, on unix one uses the find command. I am sure DOS has
> something like that. You can use DOS tree command for example.
>
> --Nasser

Oke thx. So I tried

[status,list]=system('dir *.mp3');

which gives a list of all my mp3 files in the directory. But not in the sub folders. That's where I'm looking for.

Subject: find file in folder or sub folders

From: Sadik

Date: 5 Mar, 2011 23:05:29

Message: 5 of 10

How about this one:

http://www.mathworks.com/matlabcentral/fileexchange/1492

Subject: find file in folder or sub folders

From: Jan Simon

Date: 6 Mar, 2011 00:44:08

Message: 6 of 10

Dear Stefan,

Better than SYSTEM(DIR):
  list = dir('D:\My Documents\My Music\*.mp3' )
This is not recursive also.

Kind regards, Jan

Subject: find file in folder or sub folders

From: Bruno Luong

Date: 6 Mar, 2011 11:53:04

Message: 7 of 10

"Stefan Heinen" wrote in message <iku07p$jkm$1@fred.mathworks.com>...
> "Nasser M. Abbasi" <nma@12000.org> wrote in message <ikt9ub$6ch$1@speranza.aioe.org>...
> > On 3/5/2011 4:09 AM, Stefan Heinen wrote:
> > > Hey,
> > >
> > > Thus matlab have a command that can search a directory and all sub folders. Something like:
> > >
> > > function ( 'D:\My Documents\My Music' , '*.mp3' )
> > >
> > > So the function returns all names containing the extension .mp3, even if the are in a folder like:
> > >
> > > D:\My Documents\My Music\2 many dj's\As Heard on Radio Soulwax, Pt. 2
> > >
> > > thx,
> > >
> > > Stefan
> >
> > Yes, it has. It is called system()
> >
> > using system, you can use the your host command to do the search.
> >
> > -------------------
> >
> > EDU>> [status,list]=system('dir *.m');
> > EDU>> list
> > .....
> > Directory of C:\Users\me\Documents\MATLAB
> >
> > 02/04/2011 10:19 PM 78 foo.m
> > etc...
> > ----------------------
> >
> > If you want to search a whole tree, then find the DOS command to do
> > that.
> >
> > For example, on unix one uses the find command. I am sure DOS has
> > something like that. You can use DOS tree command for example.
> >
> > --Nasser
>
> Oke thx. So I tried
>
> [status,list]=system('dir *.mp3');
>
> which gives a list of all my mp3 files in the directory. But not in the sub folders. That's where I'm looking for.

Are you arware of /S option.

 [status,list]=system('dir /S *.mp3');

I'm not for using DIR because the output is messy.

Bruno

Subject: find file in folder or sub folders

From: Stefan Heinen

Date: 6 Mar, 2011 12:27:04

Message: 8 of 10

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <ikvsj0$7jl$1@fred.mathworks.com>...
> "Stefan Heinen" wrote in message <iku07p$jkm$1@fred.mathworks.com>...
> > "Nasser M. Abbasi" <nma@12000.org> wrote in message <ikt9ub$6ch$1@speranza.aioe.org>...
> > > On 3/5/2011 4:09 AM, Stefan Heinen wrote:
> > > > Hey,
> > > >
> > > > Thus matlab have a command that can search a directory and all sub folders. Something like:
> > > >
> > > > function ( 'D:\My Documents\My Music' , '*.mp3' )
> > > >
> > > > So the function returns all names containing the extension .mp3, even if the are in a folder like:
> > > >
> > > > D:\My Documents\My Music\2 many dj's\As Heard on Radio Soulwax, Pt. 2
> > > >
> > > > thx,
> > > >
> > > > Stefan
> > >
> > > Yes, it has. It is called system()
> > >
> > > using system, you can use the your host command to do the search.
> > >
> > > -------------------
> > >
> > > EDU>> [status,list]=system('dir *.m');
> > > EDU>> list
> > > .....
> > > Directory of C:\Users\me\Documents\MATLAB
> > >
> > > 02/04/2011 10:19 PM 78 foo.m
> > > etc...
> > > ----------------------
> > >
> > > If you want to search a whole tree, then find the DOS command to do
> > > that.
> > >
> > > For example, on unix one uses the find command. I am sure DOS has
> > > something like that. You can use DOS tree command for example.
> > >
> > > --Nasser
> >
> > Oke thx. So I tried
> >
> > [status,list]=system('dir *.mp3');
> >
> > which gives a list of all my mp3 files in the directory. But not in the sub folders. That's where I'm looking for.
>
> Are you arware of /S option.
>
> [status,list]=system('dir /S *.mp3');
>
> I'm not for using DIR because the output is messy.
>
> Bruno

No I wasn't. I knew about dir and how to use it, but I was not aware of the /S option.
Thx!

Subject: find file in folder or sub folders

From: bugguts99

Date: 27 Apr, 2011 02:47:05

Message: 9 of 10

"Stefan Heinen" wrote in message <ikvuio$enn$1@fred.mathworks.com>...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <ikvsj0$7jl$1@fred.mathworks.com>...
> > "Stefan Heinen" wrote in message <iku07p$jkm$1@fred.mathworks.com>...
> > > "Nasser M. Abbasi" <nma@12000.org> wrote in message <ikt9ub$6ch$1@speranza.aioe.org>...
> > > > On 3/5/2011 4:09 AM, Stefan Heinen wrote:
> > > > > Hey,
> > > > >
> > > > > Thus matlab have a command that can search a directory and all sub folders. Something like:
> > > > >
> > > > > function ( 'D:\My Documents\My Music' , '*.mp3' )
> > > > >
> > > > > So the function returns all names containing the extension .mp3, even if the are in a folder like:
> > > > >
> > > > > D:\My Documents\My Music\2 many dj's\As Heard on Radio Soulwax, Pt. 2
> > > > >
> > > > > thx,
> > > > >
> > > > > Stefan
> > > >
> > > > Yes, it has. It is called system()
> > > >
> > > > using system, you can use the your host command to do the search.
> > > >
> > > > -------------------
> > > >
> > > > EDU>> [status,list]=system('dir *.m');
> > > > EDU>> list
> > > > .....
> > > > Directory of C:\Users\me\Documents\MATLAB
> > > >
> > > > 02/04/2011 10:19 PM 78 foo.m
> > > > etc...
> > > > ----------------------
> > > >
> > > > If you want to search a whole tree, then find the DOS command to do
> > > > that.
> > > >
> > > > For example, on unix one uses the find command. I am sure DOS has
> > > > something like that. You can use DOS tree command for example.
> > > >
> > > > --Nasser
> > >
> > > Oke thx. So I tried
> > >
> > > [status,list]=system('dir *.mp3');
> > >
> > > which gives a list of all my mp3 files in the directory. But not in the sub folders. That's where I'm looking for.
> >
> > Are you arware of /S option.
> >
> > [status,list]=system('dir /S *.mp3');
> >
> > I'm not for using DIR because the output is messy.
> >
> > Bruno
>
> No I wasn't. I knew about dir and how to use it, but I was not aware of the /S option.
> Thx!

Hi there,

Further to this, can you create a cell array of all the files generated from this list for further processing??

Thanks in advance!

Subject: find file in folder or sub folders

From: Jason

Date: 25 Nov, 2014 20:27:05

Message: 10 of 10

Just my two cents, but here's a possible solution. Albeit a bit late.


function Outfiles=recursdir(baseDir,searchExpression)
% OUTFILES = RECURSDIR(BASEDIRECTORY,SEARCHEXPRESSION)
% A recursive search to find files that match the search expression
%

dstr = dir(baseDir);%search current directory and put results in structure
Outfiles = {};
for II = 1:length(dstr)
    if ~dstr(II).isdir && ~isempty(regexp(dstr(II).name,searchExpression,'match'))
    %look for a match that isn't a directory
        Outfiles{length(Outfiles)+1} = dstr(II).name;
    elseif dstr(II).isdir && ~strcmp(dstr(II).name,'.') && ~strcmp(dstr(II).name,'..')
    %if it is a directory(and not current or up a level), search in that
        pname = fullfile(baseDir,dstr(II).name);
        OutfilesTemp=recursdir(pname,searchExpression);
        if ~isempty(OutfilesTemp)
        %if recursive search is fruitful, add it to the current list
            Outfiles((length(Outfiles)+1):(length(Outfiles)+length(OutfilesTemp))) = OutfilesTemp;
        end
    end
end


"bugguts99" wrote in message <ip8039$bk1$1@fred.mathworks.com>...
> "Stefan Heinen" wrote in message <ikvuio$enn$1@fred.mathworks.com>...
> > "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <ikvsj0$7jl$1@fred.mathworks.com>...
> > > "Stefan Heinen" wrote in message <iku07p$jkm$1@fred.mathworks.com>...
> > > > "Nasser M. Abbasi" <nma@12000.org> wrote in message <ikt9ub$6ch$1@speranza.aioe.org>...
> > > > > On 3/5/2011 4:09 AM, Stefan Heinen wrote:
> > > > > > Hey,
> > > > > >
> > > > > > Thus matlab have a command that can search a directory and all sub folders. Something like:
> > > > > >
> > > > > > function ( 'D:\My Documents\My Music' , '*.mp3' )
> > > > > >
> > > > > > So the function returns all names containing the extension .mp3, even if the are in a folder like:
> > > > > >
> > > > > > D:\My Documents\My Music\2 many dj's\As Heard on Radio Soulwax, Pt. 2
> > > > > >
> > > > > > thx,
> > > > > >
> > > > > > Stefan
> > > > >
> > > > > Yes, it has. It is called system()
> > > > >
> > > > > using system, you can use the your host command to do the search.
> > > > >
> > > > > -------------------
> > > > >
> > > > > EDU>> [status,list]=system('dir *.m');
> > > > > EDU>> list
> > > > > .....
> > > > > Directory of C:\Users\me\Documents\MATLAB
> > > > >
> > > > > 02/04/2011 10:19 PM 78 foo.m
> > > > > etc...
> > > > > ----------------------
> > > > >
> > > > > If you want to search a whole tree, then find the DOS command to do
> > > > > that.
> > > > >
> > > > > For example, on unix one uses the find command. I am sure DOS has
> > > > > something like that. You can use DOS tree command for example.
> > > > >
> > > > > --Nasser
> > > >
> > > > Oke thx. So I tried
> > > >
> > > > [status,list]=system('dir *.mp3');
> > > >
> > > > which gives a list of all my mp3 files in the directory. But not in the sub folders. That's where I'm looking for.
> > >
> > > Are you arware of /S option.
> > >
> > > [status,list]=system('dir /S *.mp3');
> > >
> > > I'm not for using DIR because the output is messy.
> > >
> > > Bruno
> >
> > No I wasn't. I knew about dir and how to use it, but I was not aware of the /S option.
> > Thx!
>
> Hi there,
>
> Further to this, can you create a cell array of all the files generated from this list for further processing??
>
> Thanks in advance!

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us