Looking within search path for a directory containing a chosen string

7 views (last 30 days)
I have many directories on my search path and would like to search for path directories that contain a certain string, say 'matlab_programs'. Here is what I've tried (based on this answer and this answer). Is there a better/faster way?
strToFind = 'matlab_programs'; % Return all search path directories containing this string
dirs = regexp(path,['[^;]*'],'match'); % List all paths in search path and break them into a cell array
% Index to cell entries containing the desired string
whichCellEntry = find(cellfun(@(dirs) ~isempty( strfind(dirs, strToFind) ), dirs) == 1);
dirs(whichCellEntry)' % Display all directories containing chosen string
% Make Matlab window wide before running, or the directory listings will be truncated

Accepted Answer

dpb
dpb on 13 Oct 2015
Probably not a whole lot unless...
IF the subdirectories were to be on a given higher level path branch, then one could probably get the desired result directly via a shell dir() command with the suitable wild card. Would depend on the shell for specific switches, of course, as well as the above condition. On Winders CMD.EXE, the following seems to work...
[~,dirs]=dos(['dir /s /a:d /b *' strToFind '*'];
Whether it would be faster/better would likely be in the eye of the beholder and how high up in a location would have to be to cover the whole chain desired. Of course, one will need to ensure the starting path is where it needs must be in the running shell so likely need to create a batch file that takes care of that detail and dispatch it instead...
I haven't ever understood why there isn't an extension to a cellstr of strfind that "just works"; doesn't seem that big a deal.

More Answers (0)

Categories

Find more on File Operations in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!