Split File paths by '\' but file paths have varying subfolders and thus lengths
8 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Ali
am 22 Feb. 2024
Kommentiert: Stephen23
am 22 Feb. 2024
the aim of this function is to read and display all the subfolders from a given filepath (and subsequent sub folders within those sub folders and so on). I have gotten this to work and i get an X by 1 cell of all the filepaths to every subfolder.
fileList = getAllFiles('C:\Users\first.surname\Downloads\SA123 - Test Script Project');
splitlist = {};
for i =1:length(fileList)
splitlist = [splitlist ;split(fileList{i,1},"\",2)]
end
function fileList = getAllFiles(dirName)
dirData = dir(dirName); %# Get the data for the current directory
dirIndex = [dirData.isdir]; %# Find the index for directories
fileList = {dirData(dirIndex).name}'; %'# Get a list of the files
fileList(ismember(fileList,{'.','..'})) = [];
if ~isempty(fileList)
fileList = cellfun(@(x) fullfile(dirName,x),... %# Prepend path to files
fileList,'UniformOutput',false);
end
subDirs = {dirData(dirIndex).name}; %# Get a list of the subdirectories
validIndex = ~ismember(subDirs,{'.','..'}); %# Find index of subdirectories
%# that are not '.' or '..'
for iDir = find(validIndex) %# Loop over valid subdirectories
nextDir = fullfile(dirName,subDirs{iDir}); %# Get the subdirectory path
fileList = [fileList; getAllFiles(nextDir)]; %# Recursively call getAllFiles
end
end
the issue i pose when using either
fileparts()
or
split()
is that due to some subfolders having children and some not, the dimension of the array is never the same andit fails after the iteration of the subfolders in ths folder only
an example of the folder structure is
dirName\SubFolder1\ChildFolder1\
dirName\SubFolder1\ChildFolder2\
dirName\SubFolder2\
dirName\SubFolder3\ChildFolder1\ChildFolder1\
i'm not sure how to overcome this (possible to force the dimension of the array and pad the empties with "" cells? maybe)
FileList
splitList
2 Kommentare
Jonas
am 22 Feb. 2024
do all subfolders contain at least one file? then you could use subdir from FEX for getting all files from a specific direction and below and get the folder entries of all files
Akzeptierte Antwort
Stephen23
am 22 Feb. 2024
Bearbeitet: Stephen23
am 22 Feb. 2024
Your code is very complex. You can simplify it by letting DIR recursively parse the folder structure.
Lets try it here:
mkdir Name/SubFolder1/ChildFolder1
mkdir Name/SubFolder1/ChildFolder2
mkdir Name/SubFolder2
mkdir Name/SubFolder3/ChildFolder1/ChildFolder1
P = './Name'; % absolute or relative path to the parent folder
C = {};
S = dir(fullfile(P,'**','*')); % recursive folder search
S(strncmp({S.name},'.',1)) = [];
for k = 1:numel(S)
V = regexp(S(k).folder,'[^/\\]+','match');
V{end+1} = S(k).name;
C(k,1:numel(V)) = V;
end
C(cellfun(@isempty,C)) = {''}; % optional
display(C)
2 Kommentare
Stephen23
am 22 Feb. 2024
PS: if you also want to exclude files from the output, perhaps like this:
S(strncmp({S.name},'.',1)|~[S.isdir]) = [];
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu File Operations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!