Re-naming Text files in many folders using matlab

1 次查看(过去 30 天)
Dear all
i have many folders, where every folder contain text files, i need to parse the whole folders and then re-naming exist files to name of parent folder along with its names as in follow:
for example in folder1 where text files are ={ text1.txt, name.txt, vbar.txt, ....etc} hope to be = { folder1-text1.txt, folder1-name1.txt, etc...}
thanks for any suggestion

采纳的回答

Walter Roberson
Walter Roberson 2017-2-1
projectdir = 'name of main directory goes here';
dinfo = dir(projectdir);
dinfo(~[dinfo.isdir]) = []; %remove non-folders
dinfo(ismember({dinfo.name}, {'.', '..'}) = []; %remove . and .. directories
num_subdir = length(dinfo);
for S = 1 : num_subdir
this_subdir = dinfo(S).name;
subdinfo = dir( fullfile(projectdir, this_subdir, '*.txt') );
num_subfile = length(subdinfo);
for F = 1 : num_subfile
this_file = subdinfo(F).name;
old_file = fullfile( projectdir, this_subdir, this_file );
new_file = fullfile( projectdir, [this_subdir '-' this_file] );
try
rename(old_file, new_file);
catch ME
fprintf('Failed to rename "%s" to "%s"\n', old_file, new_file );
end
end
end
  3 个评论
Walter Roberson
Walter Roberson 2017-2-1
new_file = fullfile( projectdir, this_subdir, [this_subdir '-' this_file] );

请先登录,再进行评论。

更多回答(1 个)

Thibaut Jacqmin
Thibaut Jacqmin 2017-2-1
Use the following function GetAllSubDirAndFiles(main_folder) where main_folder is the path to the folder containing all the subfolders containing the files you want to rename. The output contains all the folders and files. Then you can rename the files
% Example :
[subfolders, files] = GetAllSubDirAndFiles(main_folder);
Then files{1} is a cell containing all files that are in the subfoler subfolders{1} and so on for files{2}, etc.
Here are the two functions you need
function [sub,fls] = GetAllSubDirAndFiles(main_folder)
[sub, fls] = subfolder(main_folder,'','');
function [sub,fls] = subfolder(main_folder,sub,fls)
tmp = dir(main_folder);
tmp = tmp(~ismember({tmp.name},{'.' '..'}));
for i = {tmp([tmp.isdir]).name}
sub{end+1} = [main_folder '\' i{:}];
if nargin==2
sub = subfolder(sub{end},sub);
else
tmp = dir(sub{end});
fls{end+1} = {tmp(~[tmp.isdir]).name};
[sub, fls] = subfolder(sub{end},sub,fls);
end% if
end% for
  3 个评论
Thibaut Jacqmin
Thibaut Jacqmin 2017-2-1
You should add the renaming part. So the total code is :
[subfolders, files] = GetAllSubDirAndFiles(main_folder);
for k = 1:length(subfolders)
for i = files{k}
old_filepath = fullfile(subfolders{k}, '/', strjoin(i));
ind = max(strfind(subfolders{k}, '\'));
new_filepath = fullfile(subfolders{k}, '\', [subfolders{k}(ind+1:end), '_', strjoin(i)])
movefile(old_filepath, new_filepath);
end
end
function [sub,fls] = GetAllSubDirAndFiles(main_folder)
[sub, fls] = subfolder(main_folder,'','');
function [sub,fls] = subfolder(main_folder,sub,fls)
tmp = dir(main_folder);
tmp = tmp(~ismember({tmp.name},{'.' '..'}));
for i = {tmp([tmp.isdir]).name}
sub{end+1} = [main_folder '\' i{:}];
if nargin==2
sub = subfolder(sub{end},sub);
else
tmp = dir(sub{end});
fls{end+1} = {tmp(~[tmp.isdir]).name};
[sub, fls] = subfolder(sub{end},sub,fls);
end
end

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Develop Apps Using App Designer 的更多信息

标签

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by