Renaming Files After Unzip
Show older comments
Hello,
I have the following code below that works just fine for unzipping and deleting files.
files = fullfile(matlabroot, '\toolbox');
if ~exist(files, 'dir')
files = matlabroot;
end
uiwait(msgbox('Pick a folder on the next window that will come up.'));
selpath = uigetdir(files);
if selpath == 0
return;
end
projectdir = selpath;
dinfo = dir( fullfile( projectdir, '**', '*.zip') ); %find all .zip underneath the projectdir.
%then
for K = 1 : length(dinfo)
unzip(fullfile(dinfo(K).folder,dinfo(K).name),selpath);
end
files = dir(projectdir);
deletedfiles = 0;
for itr = 1:length(files)
if files(itr).bytes<500000 && ~files(itr).isdir
files.name
delete(fullfile(files(itr).folder, files(itr).name))
deletedfiles=deletedfiles+1;
end
end
deletedfiles
But, after I want to rename the files, so after the unzip loop, I made this changes.
files = fullfile(matlabroot, '\toolbox');
if ~exist(files, 'dir')
files = matlabroot;
end
uiwait(msgbox('Pick a folder on the next window that will come up.'));
selpath = uigetdir(files);
if selpath == 0
return;
end
projectdir = selpath;
dinfo = dir( fullfile( projectdir, '**', '*.zip') ); %find all .zip underneath the projectdir.
%then
for K = 1 : length(dinfo)
unzip(fullfile(dinfo(K).folder,dinfo(K).name),selpath);
end
for id = 1:length(files)
% Get the file name (minus the extension)
[~, f] = fileparts(files(id).name);
% write the rename file
rf = strcat('Flight_10001',ext) ;
% rename the file
movefile(files(id).name, rf);
end
files = dir(projectdir);
deletedfiles = 0;
for itr = 1:length(files)
if files(itr).bytes<500000 && ~files(itr).isdir
files.name
delete(fullfile(files(itr).folder, files(itr).name))
deletedfiles=deletedfiles+1;
end
end
deletedfiles
and got this following errror
Struct contents reference from a non-struct array object.
Error in UnzipnDelete (line 18)
[~, f] = fileparts(files(id).name);
Any ideas how to fix this? I just want to rename it to "Flight 10001, Flight 10002, and so on"
4 Comments
Guillaume
on 2 Mar 2020
Important: You should not be working in any directory under matlabroot. It's a sure fire way to completely mess up your matlab installation. These directories are for matlab own use.
You should use your working folder, specified by userpath.
Stephen23
on 2 Mar 2020
files = fullfile(matlabroot, '\toolbox');
% ^^^^^^^^^^ Do NOT save files in any application's installation folders!
Oktavian Jason
on 2 Mar 2020
Guillaume
on 2 Mar 2020
I'd use userpath (which is most likely 'C:\Users\yourusername\My Documents\Matlab' on windows):
selpath = uigetdir(userpath);
Accepted Answer
More Answers (0)
Categories
Find more on Environment and Settings 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!