Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Rename files

Subject: Rename files

From: Branko

Date: 12 Jun, 2009 12:31:01

Message: 1 of 6

Hi,

I was wondering if there is more effective way of reanaming files. In my example I have around 20 files :
fnam = [{'PG1a.txt' 'PG2a.txt' 'PG3a.txt' 'PG4a.txt' 'PG5a.txt' 'PG6a.txt' 'PG7a.txt'
    'PG8a.txt' 'PG9a.txt' 'PG10a.txt' 'PG11a.txt' 'PG12a.txt' 'PG13a.txt'}];

and I would like to rename PG1a.txt to route001.txt; PG2a.txt to route002.txt....

Here is code but problem becomes evident if you have more then 100 files.

for i= 1:length(fnam)
    fileName{i} = fnam{1,i};
% Replace the name of the file
    num{i} = regexprep(fileName{i},'[^\d]*','');
    if length(num{i})== 1
        Num{i}=num2str(mod(str2num(num{i}),10));
        filename{i}= ['route00',Num{i},'.txt'];
    else
        Num{i}=num2str(mod(str2num(num{i}),10));
        filename{i}= ['route01',Num{i},'.txt'];
    end
 % Write data from old to new files
 % infid{i} = fopen(fileName{i});
 % outfid{i} = fopen(filename{i},'wt');
 % while ~feof(infid{i})
 % s = fgetl(infid{i});
 % fprintf(outfid{i},'%s\n',s);
 % end
 % fclose(infid{i});
 % fclose(outfid{i});
end

Thanks ,

Branko

Subject: Rename files

From: Jonathan

Date: 12 Jun, 2009 13:00:17

Message: 2 of 6

"Branko " <bogunovic@mbss.org> wrote in message <h0thq5$ddu$1@fred.mathworks.com>...
> Hi,
>
> I was wondering if there is more effective way of reanaming files. In my example I have around 20 files :
> fnam = [{'PG1a.txt' 'PG2a.txt' 'PG3a.txt' 'PG4a.txt' 'PG5a.txt' 'PG6a.txt' 'PG7a.txt'
> 'PG8a.txt' 'PG9a.txt' 'PG10a.txt' 'PG11a.txt' 'PG12a.txt' 'PG13a.txt'}];
>
> and I would like to rename PG1a.txt to route001.txt; PG2a.txt to route002.txt....
>
> Here is code but problem becomes evident if you have more then 100 files.
>
> for i= 1:length(fnam)
> fileName{i} = fnam{1,i};
> % Replace the name of the file
> num{i} = regexprep(fileName{i},'[^\d]*','');
> if length(num{i})== 1
> Num{i}=num2str(mod(str2num(num{i}),10));
> filename{i}= ['route00',Num{i},'.txt'];
> else
> Num{i}=num2str(mod(str2num(num{i}),10));
> filename{i}= ['route01',Num{i},'.txt'];
> end
> % Write data from old to new files
> % infid{i} = fopen(fileName{i});
> % outfid{i} = fopen(filename{i},'wt');
> % while ~feof(infid{i})
> % s = fgetl(infid{i});
> % fprintf(outfid{i},'%s\n',s);
> % end
> % fclose(infid{i});
> % fclose(outfid{i});
> end
>
> Thanks ,
>
> Branko

Try this out. Also, you shouldn't open the file and then write the file as Matlab is horribly slow with these types of operations. Just move the file.

for i = 1:length(fnam)
    fileName = fnam{1,i};
    num = regexprep(fileName{i},'[^\d]*','');
  
    % I have created a simple function to pad the numbers (numPad)
    num = numPad(num,3);
    newName = ['route',num,'.txt']

    movefile(fileName,newName)
end

% Recursive function to pad numbers
function num = numPad(num,len)
    if isnumeric(num)
        num = num2str(num);
    end
    
    if length(num) < len
        num = numPad(['0',num],len);
    end
end

Hope this helps.
-Jonathan

Subject: Rename files

From: Jos

Date: 12 Jun, 2009 13:09:02

Message: 3 of 6

"Branko " <bogunovic@mbss.org> wrote in message <h0thq5$ddu$1@fred.mathworks.com>...
> Hi,
>
> I was wondering if there is more effective way of reanaming files. In my example I have around 20 files :
> fnam = [{'PG1a.txt' 'PG2a.txt' 'PG3a.txt' 'PG4a.txt' 'PG5a.txt' 'PG6a.txt' 'PG7a.txt'
> 'PG8a.txt' 'PG9a.txt' 'PG10a.txt' 'PG11a.txt' 'PG12a.txt' 'PG13a.txt'}];
>
> and I would like to rename PG1a.txt to route001.txt; PG2a.txt to route002.txt....
>
> Here is code but problem becomes evident if you have more then 100 files.
>
> for i= 1:length(fnam)
> fileName{i} = fnam{1,i};
> % Replace the name of the file
> num{i} = regexprep(fileName{i},'[^\d]*','');
> if length(num{i})== 1
> Num{i}=num2str(mod(str2num(num{i}),10));
> filename{i}= ['route00',Num{i},'.txt'];
> else
> Num{i}=num2str(mod(str2num(num{i}),10));
> filename{i}= ['route01',Num{i},'.txt'];
> end
> % Write data from old to new files
> % infid{i} = fopen(fileName{i});
> % outfid{i} = fopen(filename{i},'wt');
> % while ~feof(infid{i})
> % s = fgetl(infid{i});
> % fprintf(outfid{i},'%s\n',s);
> % end
> % fclose(infid{i});
> % fclose(outfid{i});
> end
>
> Thanks ,
>
> Branko

Here's an approach:

fnam = [{'PG1a.txt' 'PG2a.txt' 'PG3a.txt' 'PG4a.txt' 'PG5a.txt' 'PG6a.txt' 'PG7a.txt'
    'PG8a.txt' 'PG9a.txt' 'PG10a.txt' 'PG11a.txt' 'PG12a.txt' 'PG13a.txt'}];

N = numel(fnam) ;
% loop over files
for ii=1:numel(fnam),
  newname = sprintf('route%03d.txt',ii)
  % BE CAREFULL, better use copyfile than movefile
  copyfile(fnam{ii},newname);
end

hth
Jos

Subject: Rename files

From: Branko

Date: 12 Jun, 2009 13:25:04

Message: 4 of 6

Thanks, Jonathan and Jos for help.

Branko

"Jos " <#10584@fileexchange.com> wrote in message <h0tk1e$82u$1@fred.mathworks.com>...
> "Branko " <bogunovic@mbss.org> wrote in message <h0thq5$ddu$1@fred.mathworks.com>...
> > Hi,
> >
> > I was wondering if there is more effective way of reanaming files. In my example I have around 20 files :
> > fnam = [{'PG1a.txt' 'PG2a.txt' 'PG3a.txt' 'PG4a.txt' 'PG5a.txt' 'PG6a.txt' 'PG7a.txt'
> > 'PG8a.txt' 'PG9a.txt' 'PG10a.txt' 'PG11a.txt' 'PG12a.txt' 'PG13a.txt'}];
> >
> > and I would like to rename PG1a.txt to route001.txt; PG2a.txt to route002.txt....
> >
> > Here is code but problem becomes evident if you have more then 100 files.
> >
> > for i= 1:length(fnam)
> > fileName{i} = fnam{1,i};
> > % Replace the name of the file
> > num{i} = regexprep(fileName{i},'[^\d]*','');
> > if length(num{i})== 1
> > Num{i}=num2str(mod(str2num(num{i}),10));
> > filename{i}= ['route00',Num{i},'.txt'];
> > else
> > Num{i}=num2str(mod(str2num(num{i}),10));
> > filename{i}= ['route01',Num{i},'.txt'];
> > end
> > % Write data from old to new files
> > % infid{i} = fopen(fileName{i});
> > % outfid{i} = fopen(filename{i},'wt');
> > % while ~feof(infid{i})
> > % s = fgetl(infid{i});
> > % fprintf(outfid{i},'%s\n',s);
> > % end
> > % fclose(infid{i});
> > % fclose(outfid{i});
> > end
> >
> > Thanks ,
> >
> > Branko
>
> Here's an approach:
>
> fnam = [{'PG1a.txt' 'PG2a.txt' 'PG3a.txt' 'PG4a.txt' 'PG5a.txt' 'PG6a.txt' 'PG7a.txt'
> 'PG8a.txt' 'PG9a.txt' 'PG10a.txt' 'PG11a.txt' 'PG12a.txt' 'PG13a.txt'}];
>
> N = numel(fnam) ;
> % loop over files
> for ii=1:numel(fnam),
> newname = sprintf('route%03d.txt',ii)
> % BE CAREFULL, better use copyfile than movefile
> copyfile(fnam{ii},newname);
> end
>
> hth
> Jos

Subject: Rename files

From: Laiso Lai

Date: 28 Nov, 2010 06:36:03

Message: 5 of 6

you can simply invoke java.io.file in matlab

here is an example
java.io.File('test.txt ').renameTo(java.io.File('test.test '));
and details in
http://laiso1983.blogspot.com/2010/11/how-to-quickly-rename-file-in-matlabhow.html


"Branko " <bogunovic@mbss.org> wrote in message <h0thq5$ddu$1@fred.mathworks.com>...
> Hi,
>
> I was wondering if there is more effective way of reanaming files. In my example I have around 20 files :
> fnam = [{'PG1a.txt' 'PG2a.txt' 'PG3a.txt' 'PG4a.txt' 'PG5a.txt' 'PG6a.txt' 'PG7a.txt'
> 'PG8a.txt' 'PG9a.txt' 'PG10a.txt' 'PG11a.txt' 'PG12a.txt' 'PG13a.txt'}];
>
> and I would like to rename PG1a.txt to route001.txt; PG2a.txt to route002.txt....
>
> Here is code but problem becomes evident if you have more then 100 files.
>
> for i= 1:length(fnam)
> fileName{i} = fnam{1,i};
> % Replace the name of the file
> num{i} = regexprep(fileName{i},'[^\d]*','');
> if length(num{i})== 1
> Num{i}=num2str(mod(str2num(num{i}),10));
> filename{i}= ['route00',Num{i},'.txt'];
> else
> Num{i}=num2str(mod(str2num(num{i}),10));
> filename{i}= ['route01',Num{i},'.txt'];
> end
> % Write data from old to new files
> % infid{i} = fopen(fileName{i});
> % outfid{i} = fopen(filename{i},'wt');
> % while ~feof(infid{i})
> % s = fgetl(infid{i});
> % fprintf(outfid{i},'%s\n',s);
> % end
> % fclose(infid{i});
> % fclose(outfid{i});
> end
>
> Thanks ,
>
> Branko

Subject: Rename files

From: Jan Simon

Date: 28 Nov, 2010 13:28:03

Message: 6 of 6

Dear Branko,

> "Laiso Lai wrote:
> java.io.File('test.txt ').renameTo(java.io.File('test.test '));

Very fast!
I've created a C-Mex, which calls _wrename(), which is about 50% faster than java.io.File().renameTo(). But the time measurements are very tricky: The OS and the hard disk cache the write access. Sometimes the C-mex is 4 times faster than Matlab's MOVEFILE, sometimes it's 20 times faster.
Anyhow, I'll post it in the FEX today and you can perform your own tests. Perhaps "FileRename" appears tomorrow.

Some comments:
> > fnam = [{'PG1a.txt' 'PG2a.txt' 'PG3a.txt' 'PG4a.txt' 'PG5a.txt' 'PG6a.txt' 'PG7a.txt'
> > 'PG8a.txt' 'PG9a.txt' 'PG10a.txt' 'PG11a.txt' 'PG12a.txt' 'PG13a.txt'}];

What are the square brackets used for here?!

> > and I would like to rename PG1a.txt to route001.txt to route002.txt....
As mentioned already, your conversion is far from being efficient:

> > for i= 1:length(fnam)
> > fileName{i} = fnam{1,i};
> > % Replace the name of the file
> > num{i} = regexprep(fileName{i},'[^\d]*','');
> > if length(num{i})== 1
> > Num{i}=num2str(mod(str2num(num{i}),10));
> > filename{i}= ['route00',Num{i},'.txt'];
> > else
> > Num{i}=num2str(mod(str2num(num{i}),10));
> > filename{i}= ['route01',Num{i},'.txt'];
> > end

What about this:
  oldName = {'PG1a.txt' 'PG2a.txt' 'PG3a.txt' 'PG4a.txt' 'PG5a.txt', ...
                  'PG6a.txt' 'PG7a.txt' 'PG8a.txt' 'PG9a.txt' 'PG10a.txt', ...
                  'PG11a.txt' 'PG12a.txt' 'PG13a.txt'};
  newName = cell(size(oldName));
  for iName = 1:numel(oldName)
     n = sscanf(oldName{iName}, 'PG%d', 1);
     newName{iName} = sprintf('route%.3d.txt', n);
  end

Jan

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us