Get a directory listing of only directories / folders when folders have 1000's of files

118 views (last 30 days)
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?

Accepted Answer

Image Analyst
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
  1 Comment
Jeffrey Beckstead
Jeffrey Beckstead on 23 Dec 2021
Great. After submitting question, I thought more on what I might want. Your method allows me to add in the additional needs that would have come after getting the initial listing. You are a much faster typer/coder than I to develope that sample code in that time. Thank you

Sign in to comment.

More Answers (0)

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!