我有一个关于如何更改某些文件名的问题。问题是我有这种文件,例如:
25614_z.zip
25312_z.zip
25003_z.zip
我想创建它:
lp_25_614.zip
lp_25_312.zip
lp_25_003.zip
所以,我需要自动化这个过程
lp + 25(前两位数字)+ 3 4 和 5 位置的数字)+ 点 zip
有什么办法吗?
编辑
例如,这就是我的条件:
XXXX
if fileNames{k}(3:5) >0 <5 (the interval)= 2
fileNames{k}(3:5)> >5 >10 =10
例如:
文件名{k}(3:5) = 05
在条件之后,它将在重命名中替换为= 2
然后我想将其添加到重命名名称上。
newFileName = ['fiche' fileNames{k} (3) 'pd' fileNames{k}(3:5) 'XXX '.tar.gz'];
这可能吗?
最佳答案
您可以使用movefile
重命名文件:
myPath = 'C:\myFolder\'; % path to your files
fileNames = {
'25614_z.zip'
'25312_z.zip'
'25003_z.zip'
}
for k = 1:length(fileNames)
newFileName = ['lp_' fileNames{k}(1:2) '_' fileNames{k}(3:5) '.zip'];
% renaming
movefile([myPath fileNames{k}], [myPath newFileName]);
end
~编辑~ 关于您的编辑:是的,可以针对不同的情况生成不同的文件名。请注意,下面的示例非常笨拙,仅当您只有少量不同的情况时才应使用。
myPath = 'C:\myFolder\'; % path to your files
fileNames = {
'25614_z.zip'
'25312_z.zip'
'25003_z.zip'
}
for k = 1:length(fileNames)
n = num2str(fileNames{k}(3:5));
if n > 0 && n < 5
fileName = % put your file name for this case here
elseif n >= 5 && n < 10
fileName = % put your file name for this case here
elseif n >= 10 && n < 20
fileName = % put your file name for this case here
end
% renaming
movefile([myPath fileNames{k}], [myPath newFileName]);
end
关于file - 更改文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12893094/