Clear Filters
Clear Filters

how to copy a specific files from one folder to another folder?

6 views (last 30 days)
I have Folder name as 8PSK and it contain 5000 files. but i need first 3500 files (frame_snr-108PSK101: frame_snr-108PSK3500) to copy to another folder.
8PSK
frame_snr-108PSK101
frame_snr-108PSK102
frame_snr-108PSK103
...... frame_snr-108PSK5000
i have tried the below code but it gives me random 3500 file, Although i need first files from 101 to 3500
filePattern = fullfile('E:\SNR-Dataset\DATA-11-time\time-data-training\8PSK\8PSK', '*.mat');
dest = 'E:\SNR-Dataset\DATA-11-time\time-data-training\8PSK\8PSK\8PSK'
% Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 101 : 3600
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
copyfile( fullFileName, dest)
end
Please Assist

Answers (1)

Simon Chan
Simon Chan on 8 Mar 2022
Try the following:
filePattern = fullfile('E:\SNR-Dataset\DATA-11-time\time-data-training\8PSK\8PSK', '*.mat');
dest = 'E:\SNR-Dataset\DATA-11-time\time-data-training\8PSK\8PSK\8PSK'
% Change to whatever pattern you need.
theFiles = dir(filePattern);
%
nameFiles = {theFiles.name};
[num,pos] = sort(cellfun(@(x) str2double(extractBetween(x,'frame_snr-108PSK','.txt')),nameFiles));
startnum = 101;
finishnum = 3600;
fileindex = pos(num>=startnum & num<=finishnum);
for k = 1:numel(fileindex)
baseFileName = theFiles(fileindex(k)).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
copyfile( fullFileName, dest)
end

Categories

Find more on Install Products 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!