Excluding files with a certain keyword in them

조회 수: 40 (최근 30일)
Ibro Tutic
Ibro Tutic 2015년 11월 4일
댓글: Rena Berman 2021년 5월 6일
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
Stephen23
Stephen23 2020년 12월 26일
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
Rena Berman
Rena Berman 2021년 5월 6일
(Answers Dev) Restored edit

댓글을 달려면 로그인하십시오.

채택된 답변

TastyPastry
TastyPastry 2015년 11월 4일
편집: TastyPastry 2015년 11월 4일
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
Ibro Tutic
Ibro Tutic 2015년 11월 4일
편집: Ibro Tutic 2015년 11월 4일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by