How can I grab only certain files from a folder?
15 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Brian Gregory
el 14 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
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()
Respuesta aceptada
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
Más respuestas (0)
Ver también
Categorías
Más información sobre Spreadsheets en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!