Excluding files with a certain keyword in them

20 views (last 30 days)
I am trying to get data from many CSV files, some of which are unneeded. These unneeded files all contain the word Composite (specifically _X0_00XX0_Composite.csv where X is a placeholder for some letter and 0 is a place holder for some number). Is there a way to tell my program to not attempt to read these files? The data is not needed and is not formatted for my program and I run into issues when trying to read the data. I assume it would be some sort of if statement, where it checks the file name for 'composite' and if true, I need it to break and continue scanning the other files. I assume I would need to put this piece of code as the first thing in the 3rd for loop.
clear all
close all
clc
%%%%%%%%%%%%%%%%%%%%%%%%%%File Pathing %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
projectdir = '\\frg1\shared\PerformanceCalibration\FieldPerformance\SAR Data\LoadProfile\SIM_LoadProfile_Dev\Processed\'; %Start here. or name an absolute directory
newdir = 'C:\Users\it58528\Desktop\Test';
folderinfo = dir(projectdir);
folderinfo = folderinfo([folderinfo.isdir]); %select only the directories
folderinfo = folderinfo(~ismember({folderinfo.name}, {'.', '..'})); %remove directories . and ..
%%%%%%%%%%%%%%%%%%Digs for Files/Reads/Saves %%%%%%%%%%%%%%%%%%%%%
for folderidx = 1 : length(folderinfo)
thisfolder = fullfile(projectdir, folderinfo(folderidx).name);
subfolderinfo = dir(thisfolder);
subfolderinfo = subfolderinfo([subfolderinfo.isdir]); %select only the directories
subfolderinfo = subfolderinfo(~ismember({subfolderinfo.name}, {'.', '..'})); %remove directories . and ..
folderidxi = folderinfo(folderidx).name;
newfolder = fullfile(newdir, folderidxi);
mkdir(newfolder);
for subfolderidx = 1 : length(subfolderinfo)
subfolderi = subfolderinfo(subfolderidx).name;
thissubfolder = fullfile(thisfolder, subfolderi);
fileinfo = dir( fullfile(thissubfolder, '*.csv') );
newsubfolder = fullfile(newfolder, subfolderi);
mkdir(newsubfolder);
for fileidx = 1 : length(fileinfo)
filenamei = fileinfo(fileidx).name;
thisfile = fullfile(thissubfolder, filenamei);
[filepath, basename] = fileparts(thisfile);
data = csvread(thisfile,5,2);
fileID = fopen(thisfile);
C=textscan(fileID,'%s %s %s %s');
fclose(fileID);
filename=fileinfo(fileidx).name(1:13);
PIN(fileidx).PIN = fileinfo(fileidx).name(1:13);
PIN(fileidx).serialnumber = C{1,4}{2,1};
PIN(fileidx).loadprofile = data(1:15,:);
PIN(fileidx).hours = sum(sum(PIN(fileidx).loadprofile,1));
PIN(fileidx).loadprofilepercent = PIN(fileidx).loadprofile./PIN(fileidx).hours;
PIN(fileidx).loadpercent = data(:,2);
PIN(fileidx).RPM = data(16,:);
loadprofilecolumn = find(PIN(fileidx).RPM>Resonance);
xSpeed = PIN(fileidx).RPM(loadprofilecolumn(1)-1);
newfilename = fullfile(newsubfolder, filename);
save(newfilename,'PIN'); %%%%FIX
end %files within subfolder
end %subfolders within folder
end %folders
  2 Comments
Stephen23
Stephen23 on 26 Dec 2020
Original question by Ibro Tutic on 4th of November 2015 retrieved from Google Cache:
Excluding files with a certain keyword in them
I am trying to get data from many CSV files, some of which are unneeded. These unneeded files all contain the word Composite (specifically _X0_00XX0_Composite.csv where X is a placeholder for some letter and 0 is a place holder for some number). Is there a way to tell my program to not attempt to read these files? The data is not needed and is not formatted for my program and I run into issues when trying to read the data. I assume it would be some sort of if statement, where it checks the file name for 'composite' and if true, I need it to break and continue scanning the other files. I assume I would need to put this piece of code as the first thing in the 3rd for loop.
clear all
close all
clc
%%%%%%%%%%%%%%%%%%%%%%%%%%File Pathing %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
projectdir = '\\frg1\shared\PerformanceCalibration\FieldPerformance\SAR Data\LoadProfile\SIM_LoadProfile_Dev\Processed\'; %Start here. or name an absolute directory
newdir = 'C:\Users\it58528\Desktop\Test';
folderinfo = dir(projectdir);
folderinfo = folderinfo([folderinfo.isdir]); %select only the directories
folderinfo = folderinfo(~ismember({folderinfo.name}, {'.', '..'})); %remove directories . and ..
%%%%%%%%%%%%%%%%%%Digs for Files/Reads/Saves %%%%%%%%%%%%%%%%%%%%%
for folderidx = 1 : length(folderinfo)
thisfolder = fullfile(projectdir, folderinfo(folderidx).name);
subfolderinfo = dir(thisfolder);
subfolderinfo = subfolderinfo([subfolderinfo.isdir]); %select only the directories
subfolderinfo = subfolderinfo(~ismember({subfolderinfo.name}, {'.', '..'})); %remove directories . and ..
folderidxi = folderinfo(folderidx).name;
newfolder = fullfile(newdir, folderidxi);
mkdir(newfolder);
for subfolderidx = 1 : length(subfolderinfo)
subfolderi = subfolderinfo(subfolderidx).name;
thissubfolder = fullfile(thisfolder, subfolderi);
fileinfo = dir( fullfile(thissubfolder, '*.csv') );
newsubfolder = fullfile(newfolder, subfolderi);
mkdir(newsubfolder);
for fileidx = 1 : length(fileinfo)
filenamei = fileinfo(fileidx).name;
thisfile = fullfile(thissubfolder, filenamei);
[filepath, basename] = fileparts(thisfile);
data = csvread(thisfile,5,2);
fileID = fopen(thisfile);
C=textscan(fileID,'%s %s %s %s');
fclose(fileID);
filename=fileinfo(fileidx).name(1:13);
PIN(fileidx).PIN = fileinfo(fileidx).name(1:13);
PIN(fileidx).serialnumber = C{1,4}{2,1};
PIN(fileidx).loadprofile = data(1:15,:);
PIN(fileidx).hours = sum(sum(PIN(fileidx).loadprofile,1));
PIN(fileidx).loadprofilepercent = PIN(fileidx).loadprofile./PIN(fileidx).hours;
PIN(fileidx).loadpercent = data(:,2);
PIN(fileidx).RPM = data(16,:);
loadprofilecolumn = find(PIN(fileidx).RPM>Resonance);
xSpeed = PIN(fileidx).RPM(loadprofilecolumn(1)-1);
newfilename = fullfile(newsubfolder, filename);
save(newfilename,'PIN'); %%%%FIX
end %files within subfolder
end %subfolders within folder
end %folders

Sign in to comment.

Accepted Answer

TastyPastry
TastyPastry on 4 Nov 2015
Edited: TastyPastry on 4 Nov 2015
Assuming you have the file name as a variable, I'll use myName:
myName = lower(myName);
idx = strfind(str,'composite');
if ~isempty(idx)
%do nothing
else
%do something
end
You need to find if 'Composite' is in the file name. strfind() returns the indices where it finds the match as a vector, if it doesn't find anything, it returns the empty vector. If you check whether or not the vector is empty, you can check whether or not 'Composite' is in the file name. I used lower() because strfind() is case sensitive. If case matters, you can just ignore the line
myName = lower(myName);
  1 Comment
Ibro Tutic
Ibro Tutic on 4 Nov 2015
Edited: Ibro Tutic on 4 Nov 2015
Cool, thanks. In your example, if the filename did contain composite, how would I go about breaking the current operation and moving onto the next file? Would I replace %do something with break? How would this fit into my above code? Would I need to put the majority of the contents of the 3rd for loop where %do something currently is? I tried that and none of the files got read.

Sign in to comment.

More Answers (0)

Categories

Find more on File Operations 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!