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 Center および File ExchangeFile Operations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by