Renaming Files After Unzip
15 views (last 30 days)
Show older comments
Oktavian Jason
on 2 Mar 2020
Commented: Guillaume
on 2 Mar 2020
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
I'd use userpath (which is most likely 'C:\Users\yourusername\My Documents\Matlab' on windows):
selpath = uigetdir(userpath);
Accepted Answer
Walter Roberson
on 2 Mar 2020
files = fullfile(matlabroot, '\toolbox');
So files is a character vector at that point.
files = dir(projectdir);
Eventually you assign the result of dir() over top of the character vector. But before that, it is a character vector.
[~, f] = fileparts(files(id).name);
And that line is before you assign the result of dir() to files
unzip(fullfile(dinfo(K).folder,dinfo(K).name),selpath);
That tells unzip to write the files into the directory indicated by selpath, which is not likely to be the current directory.
[~, f] = fileparts(files(id).name);
If you were to replace files with a variable that you had assigned the results of a dir() call into, then that line in itself would potentially be valid. It is completely valid to use fileparts() on a character vector that refers to a file that is not in the current directory.
movefile(files(id).name, rf);
If you were to replace files with a variable etc., etc., then the line would still need work.That line requires that the given file is located in the current directory, which will not be the case -- the file is over in selpath
3 Comments
Walter Roberson
on 2 Mar 2020
Are you sure? I thought you wanted to examine the unzipped files, and those are in selpath not in projectdir
More Answers (0)
See Also
Categories
Find more on Animation 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!