Filter löschen
Filter löschen

Changing multiple filenames of text files

1 Ansicht (letzte 30 Tage)
Ying
Ying am 11 Mai 2012
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?

Antworten (1)

Walter Roberson
Walter Roberson am 11 Mai 2012
Consider just searching for the last period.
find(filename == '.', 1, 'last')
If you need to go more specific, consider regexp()

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by