Changing multiple filenames of text files
1 次查看(过去 30 天)
显示 更早的评论
Hi all, I need to change a bunch of file names because they are too long, and they all have the time at the end, so currently the code I'm using to change the file names is the following:
filesStruct = dir('*.txt');
files = {filesStruct.name}';
j=0;
maxdat=size(files,1);
while( j < maxdat )
j
%ENTER PATH OF LOCATION OF TEXT FILES
cd ('FOLDERPATH');
j=j+1;
filename=files{j,1};
filstr=sprintf(filename);
g=1;
StrLen=1;
StrLen=length(filstr);
Strstart=StrLen-14;
btest='';
while(g<=StrLen)
if(g>Strstart)
btest=[btest,filstr(g)];
end
g=g+1;
end
old = filename;
new = btest;
copyfile(old,new);
end
Basically, I'm just leaving behind the time of the file. However I discovered that the times are not in HH:mm:ss, for example, it might say 10-15-25 for 10:15:25, but for 01:15:01 it will just be 1-15-1, so the leading 0's are omitted, which screws up my format by a few digits. Is there a way I can read in the filename, scan for a specific letter/number/symbol combination, and then truncate everything before it or after it?
For example, if I have the following string: 'FILENAME.ABC_123_ABCD_10352723-3.103-11_1_26'
and what I want is '103-11_1_26', how can I get it to truncate everything up to and including the 2nd period? The problem I have is that if I go by period count, it doesn't work because some files have different number of periods before the time I want. Can I use the .###- as a string locator even though the numbers between the period and - are different?
0 个评论
回答(1 个)
Walter Roberson
2012-5-11
Consider just searching for the last period.
find(filename == '.', 1, 'last')
If you need to go more specific, consider regexp()
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!