Finding a string in a file
6 次查看(过去 30 天)
显示 更早的评论
Hello,
I need help with this problem:
How can I find the files with the same name in different folders that have the number "5800" in them? Furthermore, how can I list these files/folders and which line(s) the number "5800" appear in the files?
I would really appreciate the help.
Thanks.
2 个评论
Fangjun Jiang
2011-7-22
"5800" is in the folder name or in the file name? Is the file a ASCII text file?
回答(8 个)
Walter Roberson
2011-7-23
CommonFileName = 'x3175.txt'; %or whatever the common name is
folderinfo = dir('*');
folderinfo(~[folderinfo.isdir]) = [];
folderinfo(strcmp({folderinfo.name},{'.','..'})) = [];
for FoIdx = 1 : length(folderinfo)
specificname = fullfile(folderinfo(FoIdx}.name, CommonFileName);
if exists(specificname, 'file)
%at this point, insert your code to examine specificfile
end
end
0 个评论
Jason Ross
2012-9-21
I realize this question is old, but in Windows Explorer (and not strictly a MATLAB question, either), you can put "content: " in the search box in the upper right hand corner, and it will search the files (including .doc files) for the string. So for this question, putting "content: 5800" would have searched and returned a list of the files that had "5800" in it.
Also, note that you can try finding something in Explorer, and then at the bottom it says "Search again in:", and one of the choices is "content". Not the most usable thing (the search dog in previous Windows versions exposed this more quickly), but it is there.
There is also a "find" command you can run from the Command Shell, just cd to the directory in question and type
find "5800" *
And the current directory will be searched. I checked a directory where I had some Word documents and it successfully searched them.
0 个评论
Honglei Chen
2011-7-22
Hi osminbas,
You could write a script to achieve this. You can use cd to change directories, what to list all the files in the folder, then for each file, use textscan to read each line and strfind to find '5800'. You then just write the result to either the screen or a file.
HTH
Honglei
0 个评论
Fangjun Jiang
2011-7-23
How about this? Open M-Editor, select menu Edit->Find file. Specify what text to find, in what files and what folder/sub-folder to find, the result will show all the files, folders and the lines that the text appears in.
Many other IDEs probably have the same capability too.
0 个评论
osminbas
2011-7-25
6 个评论
Walter Roberson
2011-7-28
The line "if exists(specificname, 'file)" that I put in the code checks to see whether the file exists in the subdirectory you are processing, and skips the string search if the file is not there.
For searching within a specific file, there are many ways. One way is:
fid = fopen(specificfile, 'rt');
lines = textscan(fid,'%[^\n]'); %reads line by line
fclose(fid);
L = find(~cellfun(@isempty,strfind(lines, '5800')),1,'first');
if ~isempty(L)
fprintf('found in %s at line %d\n', specificfile, L);
end
venkat vasu
2012-9-22
编辑:Walter Roberson
2012-9-22
a1=dir;
l=length(a1);
for i1=3:l
files=dir(a1(i1).name);
nfiles = length(files);
for i=3:nfiles
currentfilename = files(i).name;
if currentfilename==5800
%whatever operation
end
end
end
this code surely will help you...
1 个评论
Walter Roberson
2012-9-22
The task was to search the content, not the file name.
Also, "currentfilename" from files(i).name will be a string, but you attempt to compare the string to the numeric value 5800 . That is not going to have the result you expect.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Search Path 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!