Copy files from one specific folder to multiple folders
2 views (last 30 days)
Show older comments
Hi everyone. I want to copy FLAC files from one specific folder to multiple folders. I need help with the copying part. I tried many codes but couldn't achieve the desired results. Any help would be appreciated.
[ndata, text, alldata] = xlsread('E:\Thesis\PA\ASVspoof2019_PA_dev\dev-female-trl-3.xlsx')
inputFullFileName = fullfile('E:\Thesis\PA\ASVspoof2019_PA_dev\flac');
filelist = dir(inputFullFileName);
name = {filelist.name};
b=name';
b(:);
outputFolder = fullfile(pwd, 'image_sorted');
if ~exist(outputFolder, 'dir')
mkdir(outputFolder);
end
a=cell2table(alldata);
alldata1.HOMETOWNS1 = categorical(a.alldata2);
SID4 = (alldata1.HOMETOWNS1 == '78')
SID14=alldata(SID4,:)
%Common files name
fun = @(s)any(strncmp(s,b,numel(s)));
out =unique( SID11(cellfun(fun,SID14)))
%Copying
%copy files based on cell array values that matches with input folder and copy to another new folder that
% should be created once the folder flac values are matched with out values.
0 Comments
Answers (1)
Deepak
on 5 Dec 2024
To copy FLAC files from a specific folder to multiple folders based on criteria from an Excel file, first ensure that the directory listing filters specifically for FLAC files using “dir(fullfile(inputFullFileName, '*.flac'))”.
Then convert the Excel data into a format that allows easy comparison, such as a table or categorical array. Use a matching logic with functions like “contains” to identify files in the directory that correspond to identifiers or criteria extracted from the Excel file.
Then implement a loop to iterate over these matched files and use “copyfile” to copy each one to the desired output directory.
Below is a sample MATLAB code to achieve the same:
% Read data from Excel file
[ndata, text, alldata] = xlsread('E:\Thesis\PA\ASVspoof2019_PA_dev\dev-female-trl-3.xlsx');
% Define the input directory containing the FLAC files
inputFullFileName = 'E:\Thesis\PA\ASVspoof2019_PA_dev\flac';
filelist = dir(fullfile(inputFullFileName, '*.flac')); % List only FLAC files
name = {filelist.name}'; % Get file names
% Convert alldata to a table
a = cell2table(alldata);
alldata1.HOMETOWNS1 = categorical(a.alldata2);
SID4 = (alldata1.HOMETOWNS1 == '78');
SID14 = alldata(SID4, :);
% Define a function to match filenames
fun = @(s) any(strncmp(s, name, numel(s)));
out = unique(SID14(cellfun(fun, SID14)));
% Define the output folder
outputFolder = fullfile(pwd, 'image_sorted');
if ~exist(outputFolder, 'dir')
mkdir(outputFolder);
end
% Copy matching files to the output folder
for i = 1:length(out)
match = out{i};
% Find files that match
for j = 1:length(name)
if contains(name{j}, match)
sourceFile = fullfile(inputFullFileName, name{j});
destFile = fullfile(outputFolder, name{j});
copyfile(sourceFile, destFile); % Copy file to the output folder
end
end
end
fprintf('Files have been copied successfully.\n');
Please find attached the documentation of functions used for reference:
I hope this will help in resolving the issue.
0 Comments
See Also
Categories
Find more on File Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!