Excluding files with a certain keyword in them
40 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Stephen23
le 26 Déc 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
Réponse acceptée
TastyPastry
le 4 Nov 2015
Modifié(e) : TastyPastry
le 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 commentaire
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur File Operations dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!