필터 지우기
필터 지우기

how to change a filename

조회 수: 29 (최근 30일)
Pritha Pande
Pritha Pande 2017년 5월 23일
답변: Walter Roberson 2017년 5월 24일
What command should i give to change file names in one go; as of now i am giving
mypath='C:\Users\Pritha\Desktop\matalb\1996bc\g4.timeAvgMap.M2TMNXAER_5_12_4_BCEXTTAU.199';
names=dir(mypath);
fileNames = {names(~[names.isdir]).name};
for iFile = 1:(fileNames) %# Loop over the file names
newName = sprintf('%d.nc',iFile); %# Make the new name
f=['C:\Users\Pritha\Desktop\matalb\1996bc\g4.timeAvgMap.M2TMNXAER_5_12_4_BCEXTTAU.199',num2str(newName)];
g=['C:\Users\Pritha\Desktop\matalb\1996bc\g4.timeAvgMap.M2TMNXAER_5_12_4_BCEXTTAU.199',num2str(fileNames{iFile})];
movefile(g,f); %# Rename the file
end
g4.timeAvgMap.M2TMNXAER_5_12_4_BCEXTTAU.19960101-19960130.180W_90S_180E_90N.nc
g4.timeAvgMap.M2TMNXAER_5_12_4_BCEXTTAU.19960201-19960231.180W_90S_180E_90N.nc
g4.timeAvgMap.M2TMNXAER_5_12_4_BCEXTTAU.19960301-19960331.180W_90S_180E_90N.nc
g4.timeAvgMap.M2TMNXAER_5_12_4_BCEXTTAU.19960401-19960431.180W_90S_180E_90N.nc
I want them to be name as 1.nc, 2.nc, 3.nc, 4.nc.
  댓글 수: 2
Rik
Rik 2017년 5월 23일
Have you tried googling it? I don't think I'm exaggerating if I say there are hundreds of examples to be found to do this.
One hint: if you are planning on using anything to process your data for which it is useful to keep the original order, use padding. You can do that with sprintf('%04d',n).
Pritha Pande
Pritha Pande 2017년 5월 24일
Ya I know there are. But, I have been trying it for quite long and not able to get the desired result. I wanted to know what is wrong wih my command so, thought of asking it overhere. Anyways,I will try it again with the hint you have given.

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

답변 (1개)

Walter Roberson
Walter Roberson 2017년 5월 24일
You are missing the directory separator between the folder name and the file name.
I recommend that you re-code using fullfile()
mypath = 'C:\Users\Pritha\Desktop\matalb\1996bc\g4.timeAvgMap.M2TMNXAER_5_12_4_BCEXTTAU.199';
names = dir(mypath);
names([names.isdir]) = [];
fileNames = {names.name};
for iFile = 1: length(fileNames) %# Loop over the file names
newName = sprintf('%04d.nc', iFile); %# Make the new name
f = fullfile(mypath, newName);
g = fullfile(mypath, fileNames{iFile});
movefile(g,f); %# Rename the file
end

카테고리

Help CenterFile Exchange에서 Install Products에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by