Looking within search path for a directory containing a chosen string
3 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 File Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!