user1469906
user1469906

Reputation: 31

MATLAB search and Zip

I want to search a directory and sub-directories for all .doc files and zip them all into one file using MatLab. If I use the zip() matlab function it only allows for one root directory. ZIP(ZIPFILENAME, FILES, ROOTDIR) .

1) How do I recursively search the sub directories?

2) How do I add all the zip files into one folder?

Would it be best to search for, and move all .doc files into a temporary folder, then zip them from that location?

Upvotes: 1

Views: 248

Answers (1)

Christian Reimer
Christian Reimer

Reputation: 445

First, use dos(...) to dir for all .doc files, including all subdirectories (option /S), but only display the filenames, no additional information (option /B). The first output argument is the returned status, the second output argument is the string returned on the command line:

[~, filenamesFromDos] = dos('dir *.doc /B/S');

Second, extract the filenames from that command as a cell array. Filenames are seperated by the newline (\n) char and the path could potentially contain spaces, thus 'Whitespace','':

filenames = textscan(filenamesFromDos,'%s','Delimiter','\n','Whitespace','');

filenames is then a <1x1 cell>, containing a <Nx1 cell> of filename strings. With this <Nx1 cell> we can call the builtin matlab function zip(...):

zip('zip_file.zip',filenames{1});

Done.

Upvotes: 1

Related Questions