Get a directory listing of only directories / folders when folders have 1000's of files
118 views (last 30 days)
Show older comments
Jeffrey Beckstead
on 23 Dec 2021
Commented: Jeffrey Beckstead
on 23 Dec 2021
Is there a way to do the DOS command "dir /A:D" and get only a listing of directories / folders below a parent? My subfolders have 10,000's of files within them. The methods given in other posts get a listing of all folders and files using "dir", and strip out the file using "isdir" field. The "ls" function on Unix can use the "ls -d" to get only the directories. Is there an equivalent on Windows?
0 Comments
Accepted Answer
Image Analyst
on 23 Dec 2021
You could do it in MATLAB like this:
topLevelFolder = pwd; % Wherever you want.
fileList = dir(topLevelFolder);
areFolders = [fileList.isdir];
folderList = fileList(areFolders);
numFolders = length(folderList);
% Define the min number of files to record.
minFilesInFolder = 1000;
% Find out how many files are in each one
finalFolderNameList = {};
for k = 1 : length(folderList)
thisFolder = fullfile(folderList(k).folder, folderList(k).name);
filePattern = fullfile(thisFolder, '*.*');
files = dir(filePattern);
numFiles = length(files);
fprintf('"%s" has %d files in it.\n', thisFolder, numFiles);
% If it has the min number of files in it, save this folder name in our final list.
if numFiles >= minFilesInFolder
% Store this folder in the final list.
finalFolderNameList = [finalFolderNameList; thisFolder];
end
end
More Answers (0)
See Also
Categories
Find more on File Operations in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!