Trying to sort write script that will sort files within sub-folders within a main directory

1 次查看(过去 30 天)
I have gotten a lot of help from the community with my code. Right now my code takes all wav files in a folder and places them in sub-folders based on the file name, creating the folders if necessary. I want to take this a step further and do the same thing withing all the sub-folders (i.e., first sorted by date and now run through each sub-folder and sort by hour). I have the bones of the script but not sure how to tell MATLAB to do this through each folder, moving to the next one when done. I'd appreciate any help doing this. Code:
DirIn = 'E:\Site 1_Working\2019-09'; %set incoming directory
filelist=dir(fullfile(DirIn, '*.wav')); %get file list
for i = 1:length(filelist);
Filename = filelist(i).name
newStr = Filename(7:8); %Get date from file name
DirOut = fullfile(DirIn, newStr); %Set DirOut to date
if ~exist(DirOut)
mkdir(DirOut)
end
movefile(fullfile(filelist(i).folder, filelist(i).name), DirOut); %Move file to DirOut
end

回答(2 个)

Harsha Priya Daggubati
编辑:Harsha Priya Daggubati 2019-9-23
Hi,
I suggest traversing through all the subfolders present in Directory E:\Site 1_Working which is divided based on dates, extract the hour information from file name and create folders within that folder.
DirIn = 'E:\Site 1_Working'
subfoldersInDir = dir DirIn
for i=1:length(subfoldersInDir)
currentfolder = DirIn(i).name;
cd(currentfolder)
fileList = dir(currentfolder);
for j = 1:length(fileList);
Filename = fileList(j).name
newStr = ;%Get hour from file name and store it in newStr
DirOut = fullfile(DirIn,currentfolder, newStr); %Set DirOut to date
if ~exist(DirOut)
mkdir(DirOut)
end
movefile(fullfile(filelist(j).folder, filelist(j).name), DirOut); %Move file to DirOut
end
  1 个评论
Stephen23
Stephen23 2019-9-23
编辑:Stephen23 2019-9-23
Bugs in this code that are highlighted by the MATLAB editor:
1. RHS is not a character vector or string
DirIn = E:\Site 1_Working
2. Attempt to call DIR using command syntax with an input and an output:
subfoldersInDir = dir DirIn
3. No idea what this is supposed to do:
newStr = ;
4. Missing end (is highlighted when the code is aligned consistently).

请先登录,再进行评论。


Harsha Priya Daggubati
Code is edited, check it now. newStr is supposed to have the hour information.This isnt completed as I dont know the format of the filename.

类别

Help CenterFile Exchange 中查找有关 File Operations 的更多信息

标签

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by