extracting files from multiple directories

Hello everybody! I have a simple problem to solve: I have 100 folders and each folder contains around 2000 files, called v1,v2,v3...and the names are the same for every folder. How can I extract specific files (e.g v2-v10) in every folder and organize them such that all the v1 are in one new folder and the v2 in a second folder? Thank you

8 Comments

why would you like to do this with matlab?
I assume the folder names and file names obey some pattern. Can you give some representative examples?
Ok so I have a main folder which is called Simulation_control. This folder contains 100 subfolders named Sim_1 Sim_2 .... Sim_100. Every subfolder contains around 2000 precipitation data at 2000 different stations and these are called simv1 simv2 simv3...simv2000. What I would like to do is to read all these file and create a matrix A which has 2000 columns (so all the stations) and a number of row which is given by putting all the simulation one under the other...so simv1 from the first simulation (so from Sim_1), simv1 from the second simulation (Sim_2) and so on
That's what my attached program in my answer below does . Just change the starting folder and change the file pattern to simv*.* and put your reading and analysis code at the middle of the loop.
Thank you! I just looked at your code but I have few questions: - is the start_path=fullfolder... the function that reads where my main folder is allocted on my computer? -where do I say that it has to read from subfolder Sim_1 to subfolder Sim_100?
Let's say that all 100 of your sim* folders lived under a parent folder called "All_My_Sim_Folders" - then that is what your starting folder would be.
ok perfect. thank you very much
If that worked, maybe you could vote for my answer or mark it "Accepted".

Sign in to comment.

 Accepted Answer

You can use the outline in that to loop over directories. And then inside any one directory you can match the file names to find the ones of interest and handling them.
You might be interested in this file finding File Exchange contribution

More Answers (1)

See my attached demo where I recurse into all subfolders of a folder and list the files. You could then do anything you want with each file. It will be easy for you to adapt this to your specific folder names.

Categories

Community Treasure Hunt

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

Start Hunting!