Re-naming Text files in many folders using matlab

조회 수: 1 (최근 30일)
ahmed obaid
ahmed obaid 2017년 2월 1일
댓글: ahmed obaid 2017년 2월 2일
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] );
ahmed obaid
ahmed obaid 2017년 2월 2일
thank you very much ... very great code..

댓글을 달려면 로그인하십시오.

추가 답변 (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
ahmed obaid
ahmed obaid 2017년 2월 2일
Thank you very much... its working fine..

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by