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
0 件のコメント
採用された回答
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
2017 年 2 月 1 日
new_file = fullfile( projectdir, this_subdir, [this_subdir '-' this_file] );
その他の回答 (1 件)
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
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 Center および File Exchange で Characters and Strings についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!