diff --git a/getAllFiles.m b/getAllFiles.m index c305749..14892a2 100644 --- a/getAllFiles.m +++ b/getAllFiles.m @@ -37,17 +37,22 @@ % % fileList = getAllFiles(rootPath, 'FileFilter', '\.m$'); % -% 2) Find all '.m' files in the given folder and its subfolders: +% 2) Find all '.jpg', '.png', and '.tif' files: +% +% fileList = getAllFiles(rootPath, ... +% 'FileFilter', '\.(jpg|png|tif)$'); +% +% 3) Find all '.m' files in the given folder and its subfolders: % % fileList = getAllFiles(rootPath, 'Depth', 1, ... % 'FileFilter', '\.m$'); % -% 3) Find all '.m' files, returning only the file names: +% 4) Find all '.m' files, returning only the file names: % % fileList = getAllFiles(rootPath, 'FileFilter', '\.m$', ... % 'PrependPath', false); % -% 4) Find all '.jpg' files with a size of more than 1MB: +% 5) Find all '.jpg' files with a size of more than 1MB: % % bigFcn = @(s) (s.bytes > 1024^2); % fileList = getAllFiles(rootPath, 'FileFilter', '\.jpg$', ... @@ -57,7 +62,7 @@ % Author: Ken Eaton % Version: MATLAB R2016b -% Last modified: 2/23/17 +% Last modified: 3/6/17 % Copyright 2017 by Kenneth P. Eaton %-------------------------------------------------------------------------- diff --git a/getAllFiles_demo.m b/getAllFiles_demo.m index fb6cb0a..f1106e5 100644 --- a/getAllFiles_demo.m +++ b/getAllFiles_demo.m @@ -2,39 +2,51 @@ % The _getAllFiles_ function recursively collects a list of files from a % folder tree, allowing you to specify the selection criteria for which % files are collected and how they are formatted. For these examples, we'll -% be using the Image Processing Toolbox path. +% be using the main MATLAB toolbox path. %% -rootPath = 'C:\Program Files\MATLAB\R2016b\toolbox\images'; +rootPath = 'C:\Program Files\MATLAB\R2016b\toolbox\matlab'; +format compact %% The 'FileFilter' option -% We can specify a regular-expression pattern to filter the file names on, -% collecting a list of files that match. Here's an example that uses the -% 'FileFilter' option to recursively find every file with a '.m' extension -% in the Image Processing Toolbox: +% We can specify a +% pattern to filter the file names on, collecting a +% list of files that match. Here's an example that uses the 'FileFilter' +% option to recursively find every file with a '.m' extension: fileList = getAllFiles(rootPath, 'FileFilter', '\.m$'); -size(fileList) -display(fileList(1:5)) +fprintf('%d files found.\n', size(fileList, 1)); +fprintf('%s\n', fileList{1:5}, '...'); %% % It's a pretty long list, so I've only shown the first 5 files it finds. % Notice they are listed with the full path prepended by default. +%% +% If you have multiple match expressions to filter on, you can use +% to include them all in one expression. For example, +% this will find every '.jpg', '.bmp', and '.tif' file: + +fileList = getAllFiles(rootPath, 'FileFilter', '\.(jpg|bmp|tif)$'); +fprintf('%d files found.\n', size(fileList, 1)); +fprintf('%s\n', fileList{1:5}, '...'); + %% The 'Depth' option % If we don't want to search quite so far down the folder tree, we can % limit the search depth with the 'Depth' option. Let's see how many '.m' -% files are in the root folder for the Image Processing Toolbox: +% files are in the root folder: fileList = getAllFiles(rootPath, 'FileFilter', '\.m$', 'Depth', 0); -size(fileList) +fprintf('%d files found.\n', size(fileList, 1)); %% % Looks like none are. They are all contained in subfolders. Let's see how % many are located in just the immediate subfolders of the root folder: fileList = getAllFiles(rootPath, 'FileFilter', '\.m$', 'Depth', 1); -size(fileList) +fprintf('%d files found.\n', size(fileList, 1)); %% The 'PrependPath' option % Maybe we just want the file names, but don't care about the absolute @@ -42,28 +54,29 @@ fileList = getAllFiles(rootPath, 'FileFilter', '\.m$', ... 'PrependPath', false); -display(fileList(1:5)) +fprintf('%d files found.\n', size(fileList, 1)); +fprintf('%s\n', fileList{1:5}, '...'); %% The 'ValidateFcn' option % Sometimes we might want to select files based on a more complicated % criteria than just what's in their names. In this case, we can use the % 'ValidateFcn' option to specify a function that is to be run on each file % found. This function should accept as input a structure of the form -% returned by the _dir_ function and return a logical value (_true_ to -% collect it in the list, _false_ to ignore it). First, let's find all the -% '.jpg' files in the Image Processing Toolbox: +% returned by the |dir| function and return a logical value (|true| to +% collect it in the list, |false| to ignore it). First, let's find all the +% '.png' files: -fileList = getAllFiles(rootPath, 'FileFilter', '\.jpg$'); -display(fileList) +fileList = getAllFiles(rootPath, 'FileFilter', '\.png$'); +fprintf('%d files found.\n', size(fileList, 1)); %% % Now, we can specify an anonymous function that gets the byte size of each -% file and returns _true_ for only those greater than 1MB: +% file and returns |true| for only those greater than 250KB: -bigFcn = @(s) (s.bytes > 1024^2); -fileList = getAllFiles(rootPath, 'FileFilter', '\.jpg$', ... +bigFcn = @(s) (s.bytes > 512^2); +fileList = getAllFiles(rootPath, 'FileFilter', '\.png$', ... 'ValidateFcn', bigFcn); -display(fileList) +fprintf('%s\n', fileList{:}); %% -% Just the two. \ No newline at end of file +% Just the one. \ No newline at end of file diff --git a/html/getAllFiles_demo.html b/html/getAllFiles_demo.html index 86f08e7..020ddef 100644 --- a/html/getAllFiles_demo.html +++ b/html/getAllFiles_demo.html @@ -6,7 +6,7 @@ getAllFiles demo

getAllFiles demo

The getAllFiles function recursively collects a list of files from a folder tree, allowing you to specify the selection criteria for which files are collected and how they are formatted. For these examples, we'll be using the Image Processing Toolbox path.

Contents

rootPath = 'C:\Program Files\MATLAB\R2016b\toolbox\images';
-

The 'FileFilter' option

We can specify a regular-expression pattern to filter the file names on, collecting a list of files that match. Here's an example that uses the 'FileFilter' option to recursively find every file with a '.m' extension in the Image Processing Toolbox:

fileList = getAllFiles(rootPath, 'FileFilter', '\.m$');
-size(fileList)
-display(fileList(1:5))
-
-ans =
-
-        1147           1
-
-  5×1 cell array
-
-    'C:\Program Files\MATLAB\R2016b\toolbox\images\colorspaces\Contents.m'
-    'C:\Program Files\MATLAB\R2016b\toolbox\images\colorspaces\applycform.m'
-    'C:\Program Files\MATLAB\R2016b\toolbox\images\colorspaces\iccfind.m'
-    'C:\Program Files\MATLAB\R2016b\toolbox\images\colorspaces\iccread.m'
-    'C:\Program Files\MATLAB\R2016b\toolbox\images\colorspaces\iccroot.m'
-
-

It's a pretty long list, so I've only shown the first 5 files it finds. Notice they are listed with the full path prepended by default.

The 'Depth' option

If we don't want to search quite so far down the folder tree, we can limit the search depth with the 'Depth' option. Let's see how many '.m' files are in the root folder for the Image Processing Toolbox:

fileList = getAllFiles(rootPath, 'FileFilter', '\.m$', 'Depth', 0);
-size(fileList)
-
-ans =
-
-     0     0
-
+  

getAllFiles demo

The getAllFiles function recursively collects a list of files from a folder tree, allowing you to specify the selection criteria for which files are collected and how they are formatted. For these examples, we'll be using the main MATLAB toolbox path.

Contents

rootPath = 'C:\Program Files\MATLAB\R2016b\toolbox\matlab';
+format compact
+

The 'FileFilter' option

We can specify a regular-expression pattern to filter the file names on, collecting a list of files that match. Here's an example that uses the 'FileFilter' option to recursively find every file with a '.m' extension:

fileList = getAllFiles(rootPath, 'FileFilter', '\.m$');
+fprintf('%d files found.\n', size(fileList, 1));
+fprintf('%s\n', fileList{1:5}, '...');
+
7591 files found.
+C:\Program Files\MATLAB\R2016b\toolbox\matlab\addons\cef\+matlab\+internal\+addons\AddOnsWindow.m
+C:\Program Files\MATLAB\R2016b\toolbox\matlab\addons\cef\+matlab\+internal\+addons\Explorer.m
+C:\Program Files\MATLAB\R2016b\toolbox\matlab\addons\cef\+matlab\+internal\+addons\Manager.m
+C:\Program Files\MATLAB\R2016b\toolbox\matlab\addons\cef\+matlab\+internal\+addons\WindowPositionUtil.m
+C:\Program Files\MATLAB\R2016b\toolbox\matlab\addons\cef\+matlab\+internal\+addons\showExplorer.m
+...
+

It's a pretty long list, so I've only shown the first 5 files it finds. Notice they are listed with the full path prepended by default.

If you have multiple match expressions to filter on, you can use grouping operators to include them all in one expression. For example, this will find every '.jpg', '.bmp', and '.tif' file:

fileList = getAllFiles(rootPath, 'FileFilter', '\.(jpg|bmp|tif)$');
+fprintf('%d files found.\n', size(fileList, 1));
+fprintf('%s\n', fileList{1:5}, '...');
+
31 files found.
+C:\Program Files\MATLAB\R2016b\toolbox\matlab\addons\release\images\bg_global_logo.jpg
+C:\Program Files\MATLAB\R2016b\toolbox\matlab\addons\release\images\bg_h1_matlab.jpg
+C:\Program Files\MATLAB\R2016b\toolbox\matlab\appdesigner\web\release\images\bg_global_logo.jpg
+C:\Program Files\MATLAB\R2016b\toolbox\matlab\appdesigner\web\release\images\bg_h1_matlab.jpg
+C:\Program Files\MATLAB\R2016b\toolbox\matlab\codetools\liveeditor\release\images\bg_global_logo.jpg
+...
+

The 'Depth' option

If we don't want to search quite so far down the folder tree, we can limit the search depth with the 'Depth' option. Let's see how many '.m' files are in the root folder:

fileList = getAllFiles(rootPath, 'FileFilter', '\.m$', 'Depth', 0);
+fprintf('%d files found.\n', size(fileList, 1));
+
0 files found.
 

Looks like none are. They are all contained in subfolders. Let's see how many are located in just the immediate subfolders of the root folder:

fileList = getAllFiles(rootPath, 'FileFilter', '\.m$', 'Depth', 1);
-size(fileList)
-
-ans =
-
-   350     1
-
-

The 'PrependPath' option

Maybe we just want the file names, but don't care about the absolute paths. In this case, we just set the 'PrependPath' option to false:

fileList = getAllFiles(rootPath, 'FileFilter', '\.m$', ...
+fprintf('%d files found.\n', size(fileList, 1));
+
1917 files found.
+

The 'PrependPath' option

Maybe we just want the file names, but don't care about the absolute paths. In this case, we just set the 'PrependPath' option to false:

fileList = getAllFiles(rootPath, 'FileFilter', '\.m$', ...
                                  'PrependPath', false);
-display(fileList(1:5))
-
  5×1 cell array
-
-    'Contents.m'
-    'applycform.m'
-    'iccfind.m'
-    'iccread.m'
-    'iccroot.m'
-
-

The 'ValidateFcn' option

Sometimes we might want to select files based on a more complicated criteria than just what's in their names. In this case, we can use the 'ValidateFcn' option to specify a function that is to be run on each file found. This function should accept as input a structure of the form returned by the dir function and return a logical value (true to collect it in the list, false to ignore it). First, let's find all the '.jpg' files in the Image Processing Toolbox:

fileList = getAllFiles(rootPath, 'FileFilter', '\.jpg$');
-display(fileList)
-
-fileList =
-
-  13×1 cell array
-
-    'C:\Program Files\MATLAB\R2016b\toolbox\images\imdata\car1.jpg'
-    'C:\Program Files\MATLAB\R2016b\toolbox\images\imdata\car2.jpg'
-    'C:\Program Files\MATLAB\R2016b\toolbox\images\imdata\football.jpg'
-    'C:\Program Files\MATLAB\R2016b\toolbox\images\imdata\greens.jpg'
-    'C:\Program Files\MATLAB\R2016b\toolbox\images\imdata\hands1.jpg'
-    'C:\Program Files\MATLAB\R2016b\toolbox\images\imdata\hands2.jpg'
-    'C:\Program Files\MATLAB\R2016b\toolbox\images\imdata\office_1.jpg'
-    'C:\Program Files\MATLAB\R2016b\toolbox\images\imdata\office_2.jpg'
-    'C:\Program Files\MATLAB\R2016b\toolbox\images\imdata\office_3.jpg'
-    'C:\Program Files\MATLAB\R2016b\toolbox\images\imdata\office_4.jpg'
-    'C:\Program Files\MATLAB\R2016b\toolbox\images\imdata\office_5.jpg'
-    'C:\Program Files\MATLAB\R2016b\toolbox\images\imdata\office_6.jpg'
-    'C:\Program Files\MATLAB\R2016b\toolbox\images\imdata\yellowlily.jpg'
-
-

Now, we can specify an anonymous function that gets the byte size of each file and returns true for only those greater than 1MB:

bigFcn = @(s) (s.bytes > 1024^2);
-fileList = getAllFiles(rootPath, 'FileFilter', '\.jpg$', ...
+fprintf('%d files found.\n', size(fileList, 1));
+fprintf('%s\n', fileList{1:5}, '...');
+
7591 files found.
+AddOnsWindow.m
+Explorer.m
+Manager.m
+WindowPositionUtil.m
+showExplorer.m
+...
+

The 'ValidateFcn' option

Sometimes we might want to select files based on a more complicated criteria than just what's in their names. In this case, we can use the 'ValidateFcn' option to specify a function that is to be run on each file found. This function should accept as input a structure of the form returned by the dir function and return a logical value (true to collect it in the list, false to ignore it). First, let's find all the '.png' files:

fileList = getAllFiles(rootPath, 'FileFilter', '\.png$');
+fprintf('%d files found.\n', size(fileList, 1));
+
4544 files found.
+

Now, we can specify an anonymous function that gets the byte size of each file and returns true for only those greater than 250KB:

bigFcn = @(s) (s.bytes > 512^2);
+fileList = getAllFiles(rootPath, 'FileFilter', '\.png$', ...
                                  'ValidateFcn', bigFcn);
-display(fileList)
-
-fileList =
-
-  2×1 cell array
-
-    'C:\Program Files\MATLAB\R2016b\toolbox\images\imdata\car1.jpg'
-    'C:\Program Files\MATLAB\R2016b\toolbox\images\imdata\car2.jpg'
-
-

Just the two.

\ No newline at end of file