is it possible to copy a file on itself?

2 ビュー (過去 30 日間)
Idin Pushkin
Idin Pushkin 2019 年 4 月 5 日
コメント済み: Idin Pushkin 2019 年 4 月 6 日
hello
i have some files in current folder which were copieed here from other folder. i have changed content of some of them in the original folder and now i want to copy them again to the current folder. but it says u cant copy a file or directory onto itself.
is there anyway for doing this?
thanks

採用された回答

Walter Roberson
Walter Roberson 2019 年 4 月 6 日
[file,path,indx] = uigetfile( ...
{'*.DATA;*.PVO;*.PVP;*.PVI;*.slx;*.INC','Eclipse Files (*.DATA,*.INC)';
'*.PVO;*.PVP;*.PVI;*.INC','PVTi files (*.PVO,*.PVP,*.PVI)';...
'*.*', 'All Files (*.*)'},'Select a File','MultiSelect', 'on');
if isequal(file,0)
disp('User selected Cancel');
else
if ischar(file); file = {file}; end
file(ismember(file, {'.', '..'})) = [];
file = fullfile(path, file);
for i = 1:length(file)
copyfile(file{i});
end
end
  1 件のコメント
Idin Pushkin
Idin Pushkin 2019 年 4 月 6 日
great thanks, Walter. i appreciate for correctimg the code. i benefit and learned too much.

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

その他の回答 (2 件)

dpb
dpb 2019 年 4 月 5 日
That isn't copying a file onto itself; there's nothing to prevent you doing what you want.
Your copyfile command isn't referencing the alternate folder in the source file it would appear by the error; however, you neglected to show us the command as you wrote it so we can't do more than surmise the cause.
If your current working directory is the target, then
otherdir='c:\that\otherdirectory';
copyfile(fullfile(otherdir,filename),filename)
should have no issues. If you are in the other directory when trying, then
targetdir='c:\that\targetlocation';
copyfile(fullfile(targetdir,filename),filename)
To be sure, wherever you are, use fully-qualified file names for both source and target.

Idin Pushkin
Idin Pushkin 2019 年 4 月 6 日
i used this code:
clc;
[file,path,indx] = uigetfile( ...
{'*.DATA;*.PVO;*.PVP;*.PVI;*.slx;*.INC','Eclipse Files (*.DATA,*.INC)';
'*.PVO;*.PVP;*.PVI;*.INC','PVTi files (*.PVO,*.PVP,*.PVI)';...
'*.*', 'All Files (*.*)'},'Select a File','MultiSelect', 'on');
if isequal(file,0)
disp('User selected Cancel');
else
end
[m,n]=size(file);
if ischar(file)
copyfile(fullfile(path,file));
elseif iscell(file)
for i=1:n
copyfile(fullfile(path,file{i}));
k=strfind(file{i},'.DATA');
end
else
end
on multiselect mode, suppose i have all files i wanted in my current directory and i have made some changes to these files in the original folder so i want to replace them with the updated files without changing the name of files. in this case usually i have more than one extensions. i use the '.All' extension to do so and it gives:
'Error using copyfile
Cannot copy or move a file or directory onto itself.
Error in testc (line 14)
copyfile(fullfile(path,file));'
there is no problem in case i use files own extension from drop down menu to copy it evenif it exists.
  2 件のコメント
Walter Roberson
Walter Roberson 2019 年 4 月 6 日
You copyfile() with only one argument. Which directory do you want the files to end up in?
ali mohebi
ali mohebi 2019 年 4 月 6 日
current directory

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

カテゴリ

Help Center および File ExchangeFilename Construction についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by