Changing the names of text files in a directory

4 vues (au cours des 30 derniers jours)
Danielle Leblanc
Danielle Leblanc le 26 Juil 2011
Commenté : Walter Roberson le 10 Fév 2019
I have 400 files in a directory called NAIC that are named like this: 7488311dbf9821f9.txt.001 is it possible to rename using matlab to naic1.txt till naic400.txt?

Réponse acceptée

Fangjun Jiang
Fangjun Jiang le 27 Juil 2011
Copy all your files to a folder named '\test' under current folder, run the following code.
Folder=fullfile(pwd,'test');
Files=dir(Folder);
for k=1:length(Files)
[PathStr,FileName,FileExt]=fileparts(Files(k).name);
if length(FileExt)==4 % Exclude folder . and folder ..
movefile(fullfile(Folder,Files(k).name),...
fullfile(Folder,['naic',FileExt(2:end),'.txt']));
end
end

Plus de réponses (2)

Nathan Greco
Nathan Greco le 27 Juil 2011
Maybe this will do? (Run this under your NAIC directory.)
Assuming you are on a windows machine:
files = dir('*.txt.*');
names = {files.name}
for i=1:length(names)
oldfilename = names{i};
newfilename = sprintf('naic%d.txt',str2num(oldfilename(end-2:end)));
system(['ren ' oldfilename ' ' newfilename]);
end
OR (shouldn't matter what system you're on):
files = dir('*.txt.*');
names = {files.name}
for i=1:length(names)
oldfilename = names{i};
newfilename = sprintf('naic%d.txt',str2num(oldfilename(end-2:end)));
movefile(oldfilename,newfilename);
end
  1 commentaire
Walter Roberson
Walter Roberson le 10 Fév 2019
rui gao comments
Absolutely a good answer. Thanks.

Connectez-vous pour commenter.


Walter Roberson
Walter Roberson le 27 Juil 2011
Use movefile() in conjunction with the information in this FAQ

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by