MATLAB Answers

Need help moving files based on name

1 view (last 30 days)
Benjamin Colbert
Benjamin Colbert on 11 Sep 2019
Answered: 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.

Answers (1)

Shunichi Kusano
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.

Sign in to answer this question.

Tags

Products


Translated by