How to move files based one whether their name is contained in a spreadsheet?
6 views (last 30 days)
Show older comments
Hello,
I have a folder ('preprocessed') containing about 200 .mat files . Each file is named '[name]_preprocessed.mat' . Here are examples of file names:
34_preprocessed.mat
777_preprocessed.mat
898_preprocessed.mat
HH55_preprocessed.mat
I have a .csv file that contains the [name] of some of these files (i.e. 34; 898).
I would like to move the files whose names are contained in the .csv document to a different folder (the 'test' folder).
Here is what I have for now (I have manually imported the .csv file called "listcsv"):
basepath=['./preprocessed/']
files=dir([basepath '*.mat']);
NS=length(files);
mydir=['./test/']
for s=1:NS
if (strcmp(files(s).name(1),'string found in spreadsheet')) %I think this is where I need help
movefile basepath mydir
end
end
Any help would be greatly appreciated!!
2 Comments
Image Analyst
on 26 May 2018
Attach the csv file so people can help you. Make it easy for them to help you, not hard.
per isakson
on 26 May 2018
Replace
movefile basepath mydir
by
movefile( basepath, mydir )
Did that help?
Accepted Answer
per isakson
on 26 May 2018
Edited: per isakson
on 30 May 2018
>> cssm
moves the files

where
function cssm
fid = fopen( fullfile('h:/m/cssm/lt_name.csv'), 'r' );
cac = textscan( fid, '%s' );
[~] = fclose( fid );
names = cac{1};
basepath = fullfile('h:/m/cssm/preprocessed/');
mydir = fullfile('h:/m/cssm/test/');
sad = dir( fullfile( basepath, '*.mat' ) );
sad = reshape( sad, 1,[] );
if not( exist( mydir, 'dir' ) == 7 )
[ sts, ~, ~ ] = mkdir( mydir );
assert( sts )
end
for s = sad
str = regexp( s.name, '^[^_]+(?=_preprocessed)', 'once', 'match' );
if ismember( str, names )
movefile( fullfile(basepath,s.name), fullfile(mydir,s.name) );
end
end
end
and lt_name.csv contains
34
898
More Answers (0)
See Also
Categories
Find more on Workspace Variables and MAT Files 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!