1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
//------------------------------------------------------------------------
// Allan CORNET
// INRIA 2004
//------------------------------------------------------------------------
// copy file
function [status,msg]=copyfile(varargin)
lhs=argn(1);
rhs=argn(2);
SourceFile='';
DestinationFile='';
flag='';
Writable=%F;
status=0;
msg='';
SourceDir='';
DestDir='';
SrcFile='';
DestFile='';
Src='';
Dest='';
Status=1;
select rhs
case 2
SourceFile=varargin(1);
DestinationFile=varargin(2);
break
case 3
SourceFile=varargin(1);
DestinationFile=varargin(2);
flag=convstr(varargin(3),'u');
if (flag=='WRITABLE') then
Writable=%T;
else
error('Third parameters incorrect.');
end
break
else
error('Number of parameters incorrect.');
end
[SourceDir,SourceFile]=GetDirFile(SourceFile);
[DestDir,DestFile]=GetDirFile(DestinationFile);
Src = """"+fullfile(SourceDir,SourceFile)+"""";
Dest = """"+fullfile(DestDir,DestFile)+"""";
Rep=dir(SourceDir);
if (Rep(2)==[]) then
Status=0;
ErrorMessage='Source directory '+SourceDir+' does not exist or is unreadable.';
else
Rep=dir(DestDir);
if (Rep(2)==[]) then
Status=0;
ErrorMessage='Destination directory '+DestDir+' does not exist or is unreadable.';
else
Rep=dir(DestDir);
if (Rep(2)==[]) then
Status=0;
ErrorMessage='source file '+fullfile(SourceDir,SourceFile)+' does not exist or is unreadable.';
end
end
end
if (Status==1) then
if MSDOS then
cmd='copy '+Src+' '+Dest;
ver=OS_Version();
if ver == 'Windows 98' | ver == 'Windows 95' | ver == 'Windows ME' then
batchlog = ' >'+ TMPDIR+'\copyfile.out';
else
batchlog = ' >'+ TMPDIR+'\copyfile.out' +' 2>'+TMPDIR+'\copyfile.err';
end
else
cmd='cp '+Src+' '+Dest;
batchlog = ' >'+ TMPDIR+'/copyfile.out' +' 2>'+TMPDIR+'/copyfile.err';
end
cmdline =cmd+batchlog;
status=unix(cmdline);
if (status~=0) then
if MSDOS then
ver=OS_Version();
if ver == 'Windows 98' | ver == 'Windows 95' | ver == 'Windows ME' then
msg='Error :'+cmd;
else
msg='Error : '+mgetl(TMPDIR+'\copyfile.err');
msg=msg+' '+mgetl(TMPDIR+'\copyfile.out');
end
else
msg='Error : '+mgetl(TMPDIR+'/copyfile.err');
msg=msg+' '+mgetl(TMPDIR+'/copyfile.out');
end
status=0;
else
msg='';
status=1;
end
if (Writable==%T) & (status==1) then
if MSDOS then
ver=OS_Version();
cmd='attrib -r '+ Dest;
if ver == 'Windows 98' | ver == 'Windows 95' | ver == 'Windows ME' then
batchlog = ' >'+ TMPDIR+'\attrib.out';
else
batchlog = ' >'+ TMPDIR+'\attrib.out' +' 2>'+TMPDIR+'\attrib.err';
end
else
cmd='chmod +w '+ Dest;
batchlog = ' >'+ TMPDIR+'\attrib.out' +' 2>'+TMPDIR+'\attrib.err';
end
cmdline =cmd+batchlog;
status=unix(cmdline);
if (status~=0) then
if MSDOS then
ver=OS_Version();
if ver == 'Windows 98' | ver == 'Windows 95' then
msg='Error :'+cmd;
else
msg='Error : '+mgetl(TMPDIR+'\attrib.err');
msg=msg+' '+mgetl(TMPDIR+'\attrib.out');
end
else
msg='Error : '+mgetl(TMPDIR+'/attrib.err');
msg=msg+' '+mgetl(TMPDIR+'/attrib.out');
end
end
end
else
msg=ErrorMessage+' Cannot copy file, '+Src+' to '+Dest;
end
endfunction
//------------------------------------------------------------------------
function [Directory,Filename]=GetDirFile(OrigFile)
[path,fname,ext]=fileparts(OrigFile);
if (path=='') then
Directory=pwd();
Filename=OrigFile;
else
Directory=path;
Filename=fname+ext;
end
endfunction
//------------------------------------------------------------------------
|