Re-naming Text files in many folders using matlab
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
ahmed obaid
el 1 de Feb. de 2017
Comentada: ahmed obaid
el 2 de Feb. de 2017
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 comentarios
Respuesta aceptada
Walter Roberson
el 1 de Feb. de 2017
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 comentarios
Walter Roberson
el 1 de Feb. de 2017
new_file = fullfile( projectdir, this_subdir, [this_subdir '-' this_file] );
Más respuestas (1)
Thibaut Jacqmin
el 1 de Feb. de 2017
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 comentarios
Thibaut Jacqmin
el 1 de Feb. de 2017
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
Ver también
Categorías
Más información sobre Characters and Strings en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!