Clear Filters
Clear Filters

Copying multiple files in a different location

61 views (last 30 days)
I have a bunch of files(around 1000) with the audio signals in them and I am trying to copy-paste them in another direction ( another folder). I would like to paste them with the same names but I do not want to use the dir function. Can anyone advise what other options do I have?
Many thanks in advance.
  12 Comments
Walter Roberson
Walter Roberson on 7 Oct 2020
Edited: Walter Roberson on 7 Oct 2020
If they all start with a common prefix and have a common suffix then you can use copyfile with wildcards. For example,
copyfile('B*.wav', '../save_the_waves/')
Just make sure that the destination directory exists first.
GreyHunter
GreyHunter on 7 Oct 2020
Edited: GreyHunter on 7 Oct 2020
They do not follow specific sequence.
@Mario I am comfused how can I copy with the ls all the files and audio signals into another location

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 7 Oct 2020
copy with the ls all the files and audio signals into another location
destination_directory = '../copy_of_files';
if ~exist(destination_directory, 'dir')
mkdir(destination_directory);
end
copyfile( '*.wav', destination_directory)
copyfile( '*.jpg', destination_directory);
  5 Comments
Walter Roberson
Walter Roberson on 8 Oct 2020
Example1 and example2 appear to be directories (also known as folders) rather than files ??
Are you sure you are not permitted to use dir() ? When you do not know the directory and file names ahead of time, dir() is the best way to proceed. There are cases where ls can output misleading filenames.
GreyHunter
GreyHunter on 8 Oct 2020
yes I am sure that I am not allowed to use dir.
what about if I have a text file which includes all the folder names and audio names?
I have created the below code
src = 'example';
fileFilter = '*.';
destination = 'example1';
files = ls(fullfile(src,fileFilter));
for fileIdx = 1:size(files,1)
filename = strtrim(string(files(fileIdx,:)));
srcPath = fullfile(src,filename);
destPath = fullfile(destination,filename);
copyfile(srcPath,destPath);
end
but i have an error with it:Error using copyfile
Cannot copy or move a file or directory onto itself.

Sign in to comment.

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!