filesep and move contents
3 次查看(过去 30 天)
显示 更早的评论
Hello, I have two folders. both contain some subfolders, and their subfolders also contain subfolders.
abc1 >> sdir1 >> ab1 >> file1.txt
abc1 >> sdir2 >> ab1 >> file2.txt
abc1 >> sdir3 >> ab1 >> file3.txt
|and|
xyz2 >> sdir1 >>
xyz2 >> sdir2 >>
xyz2 >> sdir3 >>
Ok, Now I want to go in abc1, into sdir1 and move everything to xyz2 into sdir1 next, change to folder abc1 into sdir2 and move everything to xyz2 into sdir2. I want to do in a for loop, and i tried something like this
cd D:\abc1\;
dir = dir;
list1 = dir(3:end);
% check whether the folder (sdir1,2..)me name exists
for i = 1:length(list1)
if (exist (fullfile 'D:','xyz2','list1(i)'))
disp 'folder exists...'
continue
else
stop
disp 'folder doesnt exist...'
end
cd (list1{i})
mv '*' (fullfile('D:','xyz2','list1(i)'));
cd ../;
end
any idea how I can do this?
0 个评论
采纳的回答
Jan
2011-10-19
Using the abbreviated form of functions without using parenthesis is confusing. I strongly recommend to stay at the functional form to avoid strange pitfals:
cd D:\dir1\; ==> cd('D:\dir1\');
disp 'folder exists...' ==> disp('folder exists...')
Sometimes the almost intelligent parsing works, but as a drawback you steer into lines like this, which are deeply confused:
if (exist (fullfile 'D:','dir2','list1(i)'))
% Better:
if exist(fullfile('D:', 'dir2', list1{i}), 'file')
Do not use the names of build-in functions as names of variables:
dir = dir % *never never never*
After this the dir command cannot be used anymore.
What does the stop command do?
Please fix this and edit the original question. Then the rest can be enhanced in a further step.
0 个评论
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 File Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!