MATLAB Answers

0

Need help moving files based on name

Asked by Benjamin Colbert on 11 Sep 2019
Latest activity Answered by Shunichi Kusano on 11 Sep 2019
I have a series of wav files from a recorder which records six 1-minute files each hour. When I retrieve my data it is in files sorted by month with as many as 4400 files in total. The files are named like this: 20190901T04500.wav (2019-09-01 04:50):
I would like to sort each day into a different folder like this:
I know the movefile command but the problem here is I want to move the file based up the 7th and 8th digits (day). I.e., I want the script to place all files with digits 7-8 = "09" into a folder called "9".

  0 Comments

Sign in to comment.

Tags

Products

1 Answer

Answer by Shunichi Kusano on 11 Sep 2019

Hi Benjamin,
you can pick up the filename with the specific day as the followings:
for i = 1:30 % from 1st to 30th
searchStr = sprintf('201909%02d*', i); % %02i means two digits integer with 0 padding
fileInfo = dir(searchStr);
fileInfo.name % just for cheking
targetFolder = sprintf('%d',i) % no padding
% movefile for each
filenames = cat(1, fileInfo.name);
for filei = 1:size(filenames,1)
filename = filenames(filei,:);
disp(filename) % just for cheking
if exist(targetFolder) == 7 % if the targetFolder is the existing folder
movefile(filename, targetFolder);
end
end
end
Perhaps, this codes will work well. But, just in case, please try without movefile at once in your environment.
Hope this helps.

  0 Comments

Sign in to comment.