Clear Filters
Clear Filters

filesep and move contents

5 views (last 30 days)
YOGESH
YOGESH on 19 Oct 2011
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?

Accepted Answer

Jan
Jan on 19 Oct 2011
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.

More Answers (1)

YOGESH
YOGESH on 19 Oct 2011
|Thanks Jan
Now the code after your suggestion, will be
cd('D:\abc1\');
list_dir = dir;
list1 = list_dir(3:end);
% check whether the folders (sdir1,2..) exists in xyz2
for i = 1:length(list1)
if exist(fullfile('D:', 'xyz2', list1{i}), 'file')
disp('folder exists...')
continue
else
stop
disp ('folder doesnt exist...')
end
Now enter in the dir abc1 >> sdir1
cd (list1{i});
Move everything to dir xyz2 >> sdir1 only if sdir1 exist in xyz2
mv '*' (fullfile('D:','xyz2','list1(i)'));
cd (../);
end

Categories

Find more on File Operations in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!