extract files from a folder with a perticular string and save those files in a folder

1 view (last 30 days)
clear all;
cd E:\SFRUBLU_SSURAnalysis\MGTFiles; % Full path of the directory to be searched in
filesAndFolders = dir('*.mgt'); % Returns all the files and folders in the directory
filesInDir = filesAndFolders(~([filesAndFolders.isdir])); % Returns only the files in the directory
stringToBeFound = 'AGRR';
numOfFiles = length(filesInDir);
i=1;
while(i<=numOfFiles)
filename = filesInDir(i).name; % Store the name of the file
fid = fopen(filename);
while(~feof(fid)) % Execute till EOF has been reached
contentOfFile = fgetl(fid); % Read the file line-by-line and store the content
found = strfind(contentOfFile,stringToBeFound); % Search for the stringToBeFound in contentOfFile
if ~isempty(found)
foundString = strcat('Found in file------', filename);
disp(foundString);
Agriculture= ['E:\SFRUBLU_SSURAnalysis\Agriculturemgtfiles\',filename(i).name];
% fout1 = fopen(fout1_name, 'w');
%save('E:\SFRUBLU_SSURAnalysis\Agriculturemgtfiles\',filename(i).name);
break;
end
end
fclose(fid); % Close the file
i = i+1;
end

Answers (0)

Categories

Find more on Variables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!