Changing the names of text files in a directory

조회 수: 7 (최근 30일)
Danielle Leblanc
Danielle Leblanc 2011년 7월 26일
댓글: Walter Roberson 2019년 2월 10일
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?

채택된 답변

Fangjun Jiang
Fangjun Jiang 2011년 7월 27일
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

추가 답변 (2개)

Nathan Greco
Nathan Greco 2011년 7월 27일
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
Walter Roberson
Walter Roberson 2019년 2월 10일
rui gao comments
Absolutely a good answer. Thanks.

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


Walter Roberson
Walter Roberson 2011년 7월 27일
Use movefile() in conjunction with the information in this FAQ

카테고리

Help CenterFile Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by