How can I address files with the same name for a number of folders?

I have a folder in which a number of folders contain the same filename. Example: D:\Experiment\200516.001\Analog.skv, D:\Experiment\220516.013\Analog.skv etc.
I would like to copy all Analog.skv files into one folder, in order to then merge them into one large table.
What I tried was (with wildcard):
copyfile('D:\Experiment\*\Analog.skv','D:\Experiment\Newfolder');
But I get the error: Error using copyfile, No matching files were found. I assume Matlab searches for the exact path, but thats not what I want. Further, it would be great if the Analog files would not overwrite each other when copying one after the other.

 Akzeptierte Antwort

goerk
goerk am 3 Jun. 2016
Bearbeitet: goerk am 3 Jun. 2016
It is not even possible "by hand" so there is no way to do that. You can not place two files with the same name at the same location.
Therefor you have to merge the files first, and then copy the result to the new folder.
If all folders are on the search path you can use
which('Analog.skv','-all')
to get the paths to all files.
To get all folders into the search path use
addpath(genpath('D:/Experiment'))

4 Kommentare

Thank you for your help!
After reading a little, I now understand the search path. (and what you proposed).
However, I noticed that my approach for merging the files only works when they are in the same folder.
How would you go about that? Thanks for your time!
What is your approach to merge the files?
If there is no other solution maybe you can rename the files into "Analog1.skv", "Analog2.skv", ... and then copy them into the same folder.
This should be possible with the copyfile command.
I used outerjoin (key = datenum), since for the other data sources this was possible. (Imported the files into tables from one folder)
Do you mean renaming by hand? There are too many files for that, since I would need to rename a couple of files per folder. Copyfile with a loop would work, but only if the files are allready renamed...
You can make a renamed copy with
copyfile('D:\Experiment\Folder1\Analog.skv','D:\Experiment\Newfolder\Analog1.skv');
To get the target file name you can do something like
newFileName = ['Analog' num2str(i) '.skv'];
path = ['D:\Experiment\Newfolder\'];
copyfile(sourcepath(i),[path newFileName]);

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Gefragt:

am 3 Jun. 2016

Kommentiert:

am 6 Jun. 2016

Community Treasure Hunt

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

Start Hunting!

Translated by