Code to rename fiiles

조회 수: 8 (최근 30일)
Michael Cimorelli
Michael Cimorelli 2016년 8월 10일
댓글: Image Analyst 2016년 8월 11일
I am looking to rename files that are titled "Untitled.0001.jpg" to 1,2,3...,n.jpg
I have a bunch of files that get spit out from a program in that format, but just want to name them by a number. How would I go about doing that?

답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2016년 8월 10일
편집: Azzi Abdelmalek 2016년 8월 10일
f='Untitled.0001.jpg'
nf=regexprep(f,'(\S+\.)(\d+)(\.jpg)','${num2str(str2num($2))}$3')
Or
f='Untitled4.0201.jpg'
nf=regexp(f,'[1-9]\d+\>.jpg','match','once')
To rename the file
folder='C:\Users\you\Documents\MATLAB' % folder where your jpg are located
filetype=fullfile(folder,'*.jpg')
file=dir(filetype)
filename={file.name}
for k=1:numel(file)
f=filename{k}
nf=regexp(f,'[1-9]\d+\>.jpg','match','once')
move(fullfile(folder,f),fullfile(folder,nf))
end
  댓글 수: 3
Michael Cimorelli
Michael Cimorelli 2016년 8월 11일
This is my first time ever using MATLAB... So I input the code as a script and set the directory to the NameChange folder... What do I have to do to get this to run?
Completely unrelated, do you have any sights or anything to help learn MATLAB? I need it for school and really want to get proficient in it.
Image Analyst
Image Analyst 2016년 8월 11일
To run, type the F5 key or click the green Run triangle on the tool ribbon.

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

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by