Looking within search path for a directory containing a chosen string

조회 수: 5 (최근 30일)
K E
K E 2015년 10월 13일
답변: dpb 2015년 10월 13일
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

채택된 답변

dpb
dpb 2015년 10월 13일
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.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by