1

I would like to list available directories and text files in specific directories recursively in Matlab command window (and ultimately in an m-file). I know commands like ls are available, but I would like to know the text files available in a string or vector before I recursively read each text file in the following file system structure:

master (contains A and B, all directories)

A contains A1 and A2 (all directories)

A1 contains A11, A12, A13, A14 (all directories)

A11 contains 1.txt, 2.txt, ...

Would be great to hear some feedback! Thanks in advance!

Shai
  • 111,146
  • 38
  • 238
  • 371
stanigator
  • 10,768
  • 34
  • 94
  • 129
  • 2
    See http://stackoverflow.com/questions/2652630/how-to-get-all-files-under-a-specific-directory-in-matlab/2654459#2654459. – mtrw Jul 28 '10 at 19:53
  • If your folders A11, A12, etc. contain no other directories and only the .txt files you are interested in, my solution that mtrw linked to above should work for you without any modification. This will give you a list of all the files: `fileList = getAllFiles('master');` – gnovice Jul 28 '10 at 20:10

1 Answers1

5

You can use DIR recursively.

The output of dir is a structure where the first and the second element are current and parent directory, respectively, and the rest are the contents of the folder listed. Step through those. If it's a directory (dirOutput.isdir == 1), call dir on it. If it's a file, add it to the list.

If you don't want to code this yourself, have a look around the Matlab File Exchange. Here's one of many solutions.

Jonas
  • 74,690
  • 10
  • 137
  • 177