Need help with deleting and moving files by file type
3 views (last 30 days)
Show older comments
I am trying to develop a code that unzips multiple .zip files and organizes the contents. The code needs to look through the extracted contents and if there is a .stl file, move it to a subfolder and then delete the rest of the extracted contents. If there is no .stl, then it needs to search for a .step file and do the same. Code shouldn't delete subfolders.
cd Test
directory = dir(fullfile('D:\AM+ML\Test', '\*.zip'));
for k = 1:numel(directory)
unzip(directory(k).name,'files')
cd files
Extracts = dir(fullfile('D:\AM+ML\Test\files'));
for i = 1:numel(Extracts)
if Extracts(i) '\*.stl' %This is where I get stuck. I get "Conversion to logical from struct is not possible" error
movefile *.stl STLS
delete(Extracts(i))
end
for i = 1:numel(Extracts)
if Extracts(i) '\*.step'
movefile *.step CAD_Files
delete(Extracts(i))
end
end
cd Test
end
I know my if statement (if Extracts(i) '\*.stl') isn't correct, I am just unsure of how to code "if file ends in .stl".
Thanks!
0 Comments
Answers (1)
Guillaume
on 4 Feb 2018
I know my if statement (if Extracts(i) '\*.stl') isn't correct
No idea where you've seen something remotely similar to
if Extracts(i) '\*.stl'
which is complete nonsense. There's no comparison operator there and you're not even referencing the .name field of your structure.
I am just unsure of how to code "if file ends in .stl".
if endsWith(extracts(i).name, '\*.stl')
Note that you shouldn't be using cd. Instead build the paths as you've done on the previous lines.
Unfortunately, there are more problems with your code. For example you're iterating over each stl file one at a time but each time move all the stl files at once. The iteration is a but pointless. Plus you're moving them in the directory you're deleting. I'm not sure exactly what you're really wanting to do. The lack of absolute paths does not help.
0 Comments
See Also
Categories
Find more on File Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!