2

is there a command to search for a 'particular entry inside the files present in a folder' in matlab? like if i want to search for the word "hello" in all the files present in folder A.

allFiles = dir( 'G:\folder\myfilename' );
allNames = { allFiles.name };

only lets me search for a particular file in a specific folder.:(

learningMatlab
  • 219
  • 1
  • 3
  • 8
  • You mean like Linux' `grep -r "*hello*" *`? Both answers seem to be focusing on finding *file names* containing "\*hello\*", but I think you're asking for a "\*hello\*" in the *any file's contents*...is this true? – Rody Oldenhuis Nov 15 '12 at 08:37
  • Yes.Like if i have search for a particular entry in nested folders(folder A has 20 more folders in it and i need to search for an entry within every folder and sub folder) – learningMatlab Nov 16 '12 at 19:49

2 Answers2

4

You can use wildcards:

 allFiles = dir( 'G:\folder\myfilename\*hello*.*' );
Andrey Rubshtein
  • 20,795
  • 11
  • 69
  • 104
0

See this answer to get you a list of all files in a directory.

Then you can use regexpi to identify any files containing the string 'hello'.

Or as Peter D points out:

I found it useful to build in regular expressions into the function.

if ~isempty(fileList)     
   fileList = cellfun(@(x) fullfile(dirName,x),'UniformOutput',false);
   matchstart =  regexp(fileList, pattern); 
   fileList = fileList(~cellfun(@isempty,  matchstart));
end 

and change the function signature to

getAllFiles(dirName, pattern) (also in the 2nd to last line)

Community
  • 1
  • 1
Doresoom
  • 7,398
  • 14
  • 47
  • 61
  • Actually, looks like the last commentator on that question has laid the groundwork for doing exactly what you want to do. I'll add his quote to my answer. – Doresoom Nov 14 '12 at 21:42