フィルターのクリア

Copy file from one directory to another

227 ビュー (過去 30 日間)
Marisabel Gonzalez
Marisabel Gonzalez 2018 年 12 月 17 日
回答済み: Prajwol Tamrakar 2020 年 5 月 27 日
Hi,
So I am using this piece of code to copy some files with similar names from several destination paths (e.g. file03431314, file6767464363, etc).
When I use 'file*' it assumes it is a directory, which is not...
When I actualy copy and paste the filename, e.g. 'file03431314', it works fine.
PS: the file that I want to copy does not have an extension.
How can I fix this?
Thanks
p1 = 'filepath'
% Copying file from respective path
pdest = 'mydestinationpath';
filename = 'file*';
source = fullfile(p1,filename);
destination = fullfile(pdest,filename);
copyfile(source,destination)
% Renaming
oldname = filename;
newname = '1_file.txt';
copyfile(oldname,newname);
% Deleting old file
delete(filename)
  1 件のコメント
Jan
Jan 2018 年 12 月 17 日
編集済み: Jan 2018 年 12 月 17 日
What does this mean:
When I use 'file*' it assumes it is a directory, which is not...
What are the "it"s and why do you assume that it assumes that what is a directory?
Is renaming the files a part of the question?

サインインしてコメントする。

回答 (3 件)

Jan
Jan 2018 年 12 月 17 日
If there are really problems with the copy of a file pattern, do it explicitly with a loop:
psource = 'filepath'
pdest = 'mydestinationpath';
pattern = 'file*';
sourceDir = dir(fullfile(psource, pattern));
sourceDir([sourceDir.isdir]) = []; % Files only
for k = 1:numel(sourceDir)
sourceFile = fullfile(psource, sourceDir(k).name);
destFile = fullfile(pdest, sourceDir(k).name); % Or another name?
movefile(sourceFile, destFile);
end

Prajwol Tamrakar
Prajwol Tamrakar 2020 年 5 月 27 日
This issue of copying file to another directory is possibly due to "ready only access". Use 'f' option to override the read-only status of the destination folder.
copyfile(SourceFile, DestinyFile, 'f')

Omer Yasin Birey
Omer Yasin Birey 2018 年 12 月 17 日
Maybe you can get parts of the filename by using
[filepath,name,ext] = fileparts(file);
and after that you can use directly the name of the file instead of this "filename = 'file*';"
[filepath,name,ext] = fileparts(file);
filepath =
'H:\user4\matlab'
name =
'myfile'
ext =
'.txt'
once you get the name of the file by this, you can use it in your code.
  2 件のコメント
Marisabel Gonzalez
Marisabel Gonzalez 2018 年 12 月 17 日
It's not just one file. I'm running simulations and I want to save the output files. All of them start with the same name but then are followed by a series of numbers. Also, the files don't have an extension. I save it as .txt but the original file doesn't have any extension.
Omer Yasin Birey
Omer Yasin Birey 2018 年 12 月 17 日
filename = 'file';
numbers = [1 2 3 4 5 1234324123 03431314 3431314123123 ..... ]
for i = 1:length(k)
nFile{i} = sprintf('%s_%d',filename,numbers(i));
end

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by