Re-naming Text files in many folders using matlab

1 visualizzazione (ultimi 30 giorni)
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

Risposta accettata

Walter Roberson
Walter Roberson il 1 Feb 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 Commenti
Walter Roberson
Walter Roberson il 1 Feb 2017
new_file = fullfile( projectdir, this_subdir, [this_subdir '-' this_file] );
ahmed obaid
ahmed obaid il 2 Feb 2017
thank you very much ... very great code..

Accedi per commentare.

Più risposte (1)

Thibaut Jacqmin
Thibaut Jacqmin il 1 Feb 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 Commenti
Thibaut Jacqmin
Thibaut Jacqmin il 1 Feb 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
ahmed obaid
ahmed obaid il 2 Feb 2017
Thank you very much... its working fine..

Accedi per commentare.

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by