Need help moving files based on name

3 次查看(过去 30 天)
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".

回答(1 个)

Shunichi Kusano
Shunichi Kusano 2019-9-11
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.

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by