How to read all CSV files from specific folders?
Show older comments
Hi, I want to open all CSV files and do a calculation on them. I used the code below and it worked
files = subdir('C:\Users\roozm\Desktop\New folder\*.csv');
Subdir function can do it easily. Now I want to be more specific and only open folder with the name of BIN and then read CSV files in only BIN folders.
How can I do that?
11 Comments
Paolo
on 1 Jun 2018
Do you have one folder with the term 'BIN' or multiple folders, let's say 'BIN1','BIN2' etc?
Roozbeh Yousefnejad
on 1 Jun 2018
Walter Roberson
on 1 Jun 2018
subdir() probably refers to https://www.mathworks.com/matlabcentral/fileexchange/15859-subdir--a-recursive-file-search
Walter Roberson
on 1 Jun 2018
Roozbeh, which MATLAB release are you using? Some of the newer releases have better directory facilities.
Roozbeh Yousefnejad
on 1 Jun 2018
Roozbeh,
I suspect you can actually use Matlab's dir for this purpose. I created three different folders on my Desktop:
- F21802010055
- F21802010058
- F2180201010
Within each folder, I created a folder named BIN. Within each BIN folder, I created a .csv file.
If you run the following code:
dir('*/BIN*/*.csv')
The output is as follows:
Files Found in: F21802010055\BIN
test3.csv
Files Found in: F21802010058\BIN
test.csv
Files Found in: F2180201010\BIN
test2.csv
Does this solve the problem?
EDIT Added some folders with random names in the same directory as BIN folders and they are successfully ignored by the search.
Roozbeh Yousefnejad
on 1 Jun 2018
Paolo
on 1 Jun 2018
Where are you running the command from? Are the F21802010055 folders present in your current working directory?
I am also using 2017b and the code runs fine for me.
Paolo
on 1 Jun 2018
I moved up in my working directory to my User folder, ran the command:
dir('Desktop/*/BIN*/*.csv')
And got the correct output:
Files Found in: Desktop\F21802010055\BIN
test3.csv
Files Found in: Desktop\F21802010058\BIN
test.csv
Files Found in: Desktop\F2180201010\BIN
test2.csv
Roozbeh Yousefnejad
on 1 Jun 2018
Paolo
on 1 Jun 2018
I'm not sure I understand what you mean by 'I do not know how to check if the file is in my current directory.'
Lets say your current working directory is :
C:\Users\roozm\
as from your OP. Could you list the full path of the .csv files you want, this way we can build the dir command?
Answers (3)
Walter Roberson
on 1 Jun 2018
files = dir('C:\Users\roozm\Desktop\New folder\**\BIN*\*.csv');
fullpaths = fullfile({files.folder}, {files.name});
Now fullpaths is a cell array of fully qualified .csv files that are directly under a BIN* folder anywhere under "New folder"
10 Comments
Roozbeh Yousefnejad
on 1 Jun 2018
Walter Roberson
on 1 Jun 2018
filelocation = 'C:\Users\roozm\Desktop\New folder\F21802010055\BIN*\CSV\*.csv';
files = dir(filelocation);
fullpaths = fullfile({files.folder}, {files.name});
Roozbeh Yousefnejad
on 4 Jun 2018
Roozbeh Yousefnejad
on 4 Jun 2018
Walter Roberson
on 4 Jun 2018
filename = fullpaths{i};
Roozbeh Yousefnejad
on 4 Jun 2018
Roozbeh Yousefnejad
on 4 Jun 2018
Walter Roberson
on 4 Jun 2018
When you get told that a worksheet could not be activated, it had to have found the file, because it would have errored out if it had not found the file.
Not being able to activate a worksheet can occur for different reasons, including:
- the worksheet is in use by something (possibly a different process)
- sometimes when you are looping using xlsread(), you need to put in a brief pause() to give xlsread() time to completely clean up old COM objects
- activation failure can occur if you have Excel Add-ins enabled, especially FoxIt.
There might be other reasons; I do not use the ActiveX interface myself, so I have not kept close track of the possibilities.
Roozbeh Yousefnejad
on 4 Jun 2018
Edited: Roozbeh Yousefnejad
on 4 Jun 2018
Behtom
on 9 Nov 2023
Thank You !
Roozbeh Yousefnejad
on 1 Jun 2018
0 votes
4 Comments
Paolo
on 1 Jun 2018
Change directory to new folder
cd('C:\Users\roozm\Desktop\New folder')
and run the command I posted from the comment.
Roozbeh Yousefnejad
on 1 Jun 2018
I was referring to this command:
dir('*/BIN*/*.csv')
Use this one if you are running from 'New folder'
Roozbeh Yousefnejad
on 1 Jun 2018
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!