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

8 visualizaciones (últimos 30 días)
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

Respuestas (2)

Harsha Priya Daggubati
Harsha Priya Daggubati el 23 de Sept. de 2019
Editada: Harsha Priya Daggubati el 23 de Sept. de 2019
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 comentario
Stephen23
Stephen23 el 23 de Sept. de 2019
Editada: Stephen23 el 23 de Sept. de 2019
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).

Iniciar sesión para comentar.


Harsha Priya Daggubati
Harsha Priya Daggubati el 23 de Sept. de 2019
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.

Categorías

Más información sobre File Operations en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by