-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileLookup.m
More file actions
98 lines (95 loc) · 8.85 KB
/
FileLookup.m
File metadata and controls
98 lines (95 loc) · 8.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
function Lookup = FileLookup(FileType, SearchMode, ConstantAddress)
% FileLookup()
% Determine file path/directories as well as folder information to be used for reading file information
% Created by: jsl5865
%
% Syntax:
% Lookup = FileLookup(FileType, SearchMode, ConstantAddress)
%
% Description:
% This function creates a structure (Lookup) that contains the type of files being found, the folder address,
% the file address, total number of files/subfolders, the folder information structure, and the current
% folder.
% Any file type can be chosen; a preset list has been made which can be extended.
% Currently supports multiple search modes: Singular file selection (SingleFile)
% Multiple file selection (MultiFile)
% Files within a single folder (SingleFolder)
% All files within subfolders (AllSubFolders
% One constant folder (TroubleShoot)
% One constant file (TroubleShoot)
%
% Input:
% FileType - The file extension for the type of desired files (.csv, .txt, .jpg, etc)
% SearchMode - File selection protocol
% ConstantAddress - Single file path that will not call for user input.
% - Only necessary if SearchMode = TroubleShoot
%
% Output:
% Lookup.{FileType, FolderAddress, AllFiles, FolderInfo.{name, folder, date, bytes, isdir, datenum}, FileCount, FolderCount, CurrentFolder}
arguments
FileType char {mustBeMember(FileType, {'csv', 'xlsx', 'txt', 'tif', 'mdf', 'mat', 'lif'})};
SearchMode char {mustBeMember(SearchMode, {'SingleFile', 'MultiFile', 'SingleFolder', 'AllSubFolders', 'TroubleShoot'})} = 'SingleFolder';
ConstantAddress char = ''
end
%% Define the file structure
Lookup.FileType = strcat('*.', FileType); % Choose file type
%% Select the folder
switch SearchMode % SearchMode = Constant filepath for testing
case 'TroubleShoot' % User input: Troubleshoot
if isfolder(ConstantAddress) % Constant address references folder
Lookup.FolderAddress = ConstantAddress; % Set folder address to user defined path
searchPattern = fullfile(Lookup.FolderAddress, Lookup.FileType); % Create a search pattern based on folder address
elseif isfile(ConstantAddress) % Constant address references file
[Lookup.FolderAddress, FileName, FileExt] = fileparts(ConstantAddress); % Identify file directory information
searchPattern = fullfile(Lookup.FolderAddress, [FileName, FileExt]); % Create a search pattern based on file address
else % Constant address does not references a valid file/folder
error("No valid file or folder address selected") % Throw error
end
case 'SingleFile' % User input: SingleFile
[FileName, FolderPath] = uigetfile(Lookup.FileType, 'Select a file'); % Prompt user to select a single file
if isequal(FileName, 0) % Validate file selection
error("No file selected"); % Throw error if invalid
end
Lookup.FolderAddress = FolderPath; % Set folder address to user defined path
searchPattern = fullfile(Lookup.FolderAddress, FileName); % Create a search pattern based on folder
case 'MultiFile'
[FileName, FolderPath] = uigetfile(Lookup.FileType, 'Select files', ... % Prompt user to select multiple files
'MultiSelect', 'on'); % Enable 'MultiSelect'
if isequal(FileName, 0) % Validate file selection
error("No file selected"); % Throw error if invalid
end
FileName = string(FileName); % Convert FileName from cell array to string array for dir()
Lookup.FolderAddress = FolderPath; % Set folder address to user defined path
searchPattern = fullfile(Lookup.FolderAddress, FileName); % Create a search pattern based on folder
case 'SingleFolder' % User input: SingleFolder
Lookup.FolderAddress = uigetdir('*.*', 'Select a folder'); % Prompt user to select a folder
if isequal(Lookup.FolderAddress, 0) % Validate folder selection
error('No folder selected'); % Throw error if invalid
end
searchPattern = fullfile(Lookup.FolderAddress, Lookup.FileType); % Create a search pattern based on an individual folder
case 'AllSubFolders' % User input: Troubleshoot
Lookup.FolderAddress = uigetdir('*.*', 'Select a folder'); % Prompt user to select a folder
if isequal(Lookup.FolderAddress, 0) % Validate folder selection
error('No folder selected'); % Throw error if invalid
end
searchPattern = fullfile(Lookup.FolderAddress, '**', Lookup.FileType); % Create a search pattern based on all sub folders
end
%% Find All FileType within defined folder
switch SearchMode % Create general file path
case 'MultiFile' % 'MultiFile' creates a string array which needs a special case for dir()
FolderInfo = arrayfun(@(x) dir(x), searchPattern, 'UniformOutput', false); % dir() can only accept single inputs, use an array function to find directories
Lookup.FolderInfo = vertcat(FolderInfo{:}); % Combine folder information
CommonFolder = Lookup.FolderInfo(1).folder; % Identify the common folder path
Ext = Lookup.FileType; % Identify the extension
FileNames = erase(string({Lookup.FolderInfo.name}), extractAfter(Ext, '*')); % Remove the extension from file names
Lookup.AllFiles = sprintf('%s%s:\t%s', CommonFolder, Ext, strjoin(FileNames, ', ')); % Create a common file path for every file
otherwise % Other search modes do not require special dir() inputs
Lookup.AllFiles = searchPattern; % General file path
Lookup.FolderInfo = dir(searchPattern); % Identify the folder directory
end
Lookup.FileCount = length(Lookup.FolderInfo); % Determine the number of files in this folder
Lookup.FolderCount = length(unique({Lookup.FolderInfo.folder})); % Determine the number of folders
[~, Lookup.CurrentFolder] = fileparts(Lookup.FolderAddress); % Collect folder information
Lookup.Path = arrayfun(@(x) fullfile(x.folder, x.name), Lookup.FolderInfo, 'UniformOutput', false); % Identify the file path
Lookup.Path = string(Lookup.Path); % Convert file paths to strings
end