Clear Filters
Clear Filters

Following is my code for unizipping a '.zip' file. However, I get a error when I use it for a compressed folder (.zip). Can you help me rectify the error? If not, can you also suggest some way to directly parse the zip folder name and access the dir?

1 view (last 30 days)
dt_opr = '05-29-2016';
output_folder = [mainpath datestr(dt_opr, 'yyyy-mm-dd') '\' ];
if exist([output_folder], 'dir') ~= 7
mkdir(output_folder);
end
zip_files = dir([mainpath '*RPT.00013070*' datestr(dt_opr, 'yyyymmdd') '*.zip']);
unzip(zip_files.name, output_folder);

Accepted Answer

Walter Roberson
Walter Roberson on 20 Jun 2016
A) I suggest you re-write using fullfile()
B) You refer to zip_files.name but you constructed zip_files as a string. It looks to me as if at some point you had a dir() in there. But remember that if you dir() then you need to loop around the results, and remember that the .name fields does not include the directory, so you would normally need to fullfile() to reconstruct the complete path. Which is a problem if you are using wildcards on the directory because you would not know which directory you were in.
  3 Comments
Mihir Gadkari
Mihir Gadkari on 21 Jun 2016
Edited: Mihir Gadkari on 21 Jun 2016
Walter, It worked with using a fullfile() function before the unzip(). Thank you so much for the input!
zip_files = dir([mainpath '*RPT.00013070*' datestr(dt_opr, 'yyyymmdd') '*.zip']);
f = fullfile(mainpath, zip_files.name);
unzip(f, output_folder);
Stephen23
Stephen23 on 21 Jun 2016
@Mihir Gadkari: often it is better to use fullpath for every filepath:
str = datestr(dt_opr, 'yyyymmdd');
str = sprintf('*RPT.00013070*%s*.zip',str);
str = fullpath(mainpath,str);
zip_files = dir(str);

Sign in to comment.

More Answers (0)

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!