How can I grab only certain files from a folder?

89 visualizaciones (últimos 30 días)
Brian Gregory
Brian Gregory el 14 de Jun. de 2021
Editada: LO el 15 de Jun. de 2021
Hello.
I currenlty have a folder labeled "2021-06-14". Inside are .csv files from the entire day in 15 minute increments labeled "Data 2021-06-14 00-00.csv, Data 2021-06-14 00-15.csv, ... , Data 2021-06-14 23-45.csv". My goal is to, programmatically, pull files 1-x and copy them to another folder.
Any ideas?
  2 comentarios
dpb
dpb el 14 de Jun. de 2021
..." programmatically, pull files 1-x"
OK, too cryptic for me. What is the specific set of files wanted?
Just write an appropriate wildcard pattern and use dir()
Brian Gregory
Brian Gregory el 14 de Jun. de 2021
Hi, sorry about that.
Basically I want the first 8 hours of data .. so "Data 2021-06-14 00-00.csv to Data 2021-06-14 07-45.csv".
I want to easily be able to copy these files to a new folder (already created) without having to change the date each day.
Essentially something like this is what I tried but it didn't work.
date = "2021-06-14";
csv1 = csvread("Data "+date+" 00-00.csv");
.
.
.
csvn = csvread("Data "+date+" 07-45.csv");
Where n is the total number of csv files. This did not work though but if I can either get this to work or write a code to pull the first n files from a folder and copy it to another folder that will work for me.

Iniciar sesión para comentar.

Respuesta aceptada

LO
LO el 15 de Jun. de 2021
Editada: LO el 15 de Jun. de 2021
first create a list of the files, then check which one has the second last digit pair lower than 08, then copy it to a given folder
% this allow you to move to the desired folder and pick a file
[logfile, pathname] = uigetfile('*.csv','Pick a csv file');
cd(pathname)
% the only reason for this would be to select the folder, but you may as well comment
% these 2 first lines and just position yourself in the folder right away and then execute
% the lines below
pathname=pwd; % this is just to make sure the current path is copied
new_folder = ('c:\where_I_want_my_file_to_go');
file_list = dir ('*.csv') ; %create a list of csv files based on folder content
conversion = transpose(struct2cell(file_list));
name_list = (conversion(:,1));
for i = 1:height(name_list)
filename = (name_list{i}); % this is the current filename to consider
timing = filename(end-8:end-4); % get the last 5 elements of your file name
timing(timing < '0' | timing > '9') = []; % this will remove special chars from the string
value = sscanf(timing, '%d');
if value(1:2) < 08 % consider only the first 2 digits
copyfile(fullfile(pathname,filename),new_folder,'f')
end
end
alternative solution:
path1 ='C:\path_to_csv_files'
path2 = 'C:\new_path'
source =fullfile(path1,'*.csv');
myfile= dir(source);
for i = 1:numel(myfile)
filename = myfile(i).name;
timing = filename(end-8:end-4); % get the last 5 elements of your file name
timing(timing < '0' | timing > '9') = []; % this will remove special chars from the string
value = sscanf(timing, '%d');
if value(1:2) < 08 % consider only the first 2 digits
copyfile(myfile(i).name,path2)
end
end
  5 comentarios
Brian Gregory
Brian Gregory el 15 de Jun. de 2021
They pop up during the second if else statement
LO
LO el 15 de Jun. de 2021
Editada: LO el 15 de Jun. de 2021
yes the problem is the value variable is empty if your filenames do not contain the time numbers at the same place. Or otherwise I do not know what the issue could be.

Iniciar sesión para comentar.

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by