How to delete files ending with odd number?

Hi,
I would like to delete each files having its filename ending by an odd number. Example: file0 file1 file2 file3 file4 After: file0 file2 file4
Thank you for your help :)

 Respuesta aceptada

Simon Henin
Simon Henin el 13 de Ag. de 2018
Editada: Simon Henin el 13 de Ag. de 2018
You could looop through all the files and delete ones that have an odd number using regexp, regular expression search
files = dir('*'); % find all files in current directory
for i=1:length(files),
fileno = str2double(cell2mat(regexp(files(i).name, '[0-9]{1,}', 'match')));
if mod(fileno, 2) == 1, % check if fileno modulo 2 == 1
delete(files(i).name);
end
end

7 comentarios

Morgane Flament
Morgane Flament el 13 de Ag. de 2018
Thank you very much. And at the same time, do you think I can rename them with increment number (from 0 to infinite)?
I'm not sure I understand. Do you mean rename instead of delete? And auto-increment the renamed files?
you could rename the file within the loop:
newfilename = sprintf('deletedfile_%02i.ext', counter);
movefile(files(i).name, newfilename);
counter = counter+1
Morgane Flament
Morgane Flament el 13 de Ag. de 2018
Editada: Morgane Flament el 13 de Ag. de 2018
Thank you. I meant, now that I have erased the odd files, I want to rename the even files that are left with an increment number at the end. I guess I can type:
files = dir('/.../file*');
file_name={files.name}; % I don't know if this line is useful or could be used
for i=1:length(files)
fileno = str2double(cell2mat(regexp(files(i).name, '[0-9]{1,}', 'match ')));
if mod(fileno, 2) == 1, % check if number, and if fileno % 2 == 1
delete(files(i).name );
end
newfilename = sprintf('file_even', counter );
movefile(files(i).name, newfilename );
counter = counter+1
end
That won't work, because files(i).name file was just deleted.
The main thing you should worry about is overwriting a file, since dir command will usually sort files by the leading digit. for example: file1.ext file11.ext file2.ext ...
This is what I would do:
pth = '/path/to/folder/containing/files/';
files = dir([pth 'file*']);
for i=1:length(files),
fileno = str2double(cell2mat(regexp(files(i).name, '[0-9]{1,}', 'match')));
if mod(fileno, 2) == 1, % check if number, and if fileno % 2 == 1
delete([pth files(i).name]);
else
% temporarily rename kept files, to avoid overwriting in the next step
newfile = ['tmp_' files(i).name];
movefile([pth files(i).name], [pth newfile]);
end
end
% now loop through all the tmp_file* files and rename them using an
% the incremented counter, i
files = dir([pth 'tmp_file*']);
for i=1:length(files),
% remove the tmp_ and replace the numbers at the end using regexprep
newfile = regexprep(files(i).name, {'tmp_' '[0-9]{1,}'}, {'' sprintf('%02i', i)}); % will change "tmp_fileXX.ext" -> "file01.ext"
movefile([pth files(i).name], [pth newfile]);
end
Morgane Flament
Morgane Flament el 13 de Ag. de 2018
Thank you very much for your help. There is one thing I am not sure I understand: files(i).name file will be deleted, and this is what I want. But then, if I understand well, the files that are left (the filenames ending with even number) cannot be renamed them in the loop. But not because they have been deleted (because they haven't), but because of the overwriting issue?
No, it could be done within the same loop (with proper accounting). It is just much easier to use a separate loop to make sure nothing gets overwritten.
Simple example.
Imagine:
files = {'file1', 'file2', 'file22', file24', file3'}; % this will happen because of how the dir command organizes the returned files
counter = 1;
----
Each time you go through the loop:
1) file1 will be deleted
2) file2 -> file1
3) file22 -> file2
4) file24 -> file3 (here's the issue. file3 hasn't gone through the loop yet, and thus, will be overwritten!)
---
Hope this clear it up.
Thank you very much for your help Simon. It is all clear.
I was also thinking of sorting the files using the function NATSORTFILES: (https://www.mathworks.com/matlabcentral/fileexchange/47434-natural-order-filename-sort)
pth = '/path/to/folder/containing/files/';
files = dir(fullfile(pth,'file*')); % Get the file in the directory
N = natsortfiles({files.name}); % Sort the filename in natural order
for k = 1:numel(N)
fullfile(D,N{k})
end
And then I add the loops. Do you think it is a good idea?
Again, thank you for your time, it helps me a lot.

Iniciar sesión para comentar.

Más respuestas (1)

Morgane Flament
Morgane Flament el 13 de Ag. de 2018

0 votos

@Simon Henin, do you think I can use your code as well, to delete each file ending with number 2, 3, 4, 5, but not with 0 and 1 let's say? (so no function with odd/even number in this case, I would like to delete the file based on its number at the end of the filename).

4 comentarios

Sure, instead of checking if mod(fileno, 2)== 1, you could look at the remainder:
if rem(fileno, 10) == 1 | rem(fileno, 10) == 2,
% then delete the file
end
Morgane Flament
Morgane Flament el 13 de Ag. de 2018
Editada: Morgane Flament el 13 de Ag. de 2018
I am sorry, I am not sure I understood what it does in this case. I feel like it is gonna delete the files ending with 0 and 1, whereas I want to keep them. Well actually, I want to keep the files ending with 0 and 5. So I guess I could just type:
if fileno == 1 | fileno == 2 | fileno == 3 | fileno == 4 | fileno == 6 | fileno == 7 | fileno == 8 | fileno == 9
% then delete the file
end
Also, what does " | " means? I'm guessing it's "and"
Do you know is the difference with the mod() function? I feel like it is the same.
Thank you for your help.
@Morgane,
If you wish to delete only files ending with 2,3,4 or 5, change the character set in the regex:
regexp(files(i).name, '[2345]$', 'match')
Morgane Flament
Morgane Flament el 13 de Ag. de 2018
Thank you for your help!

Iniciar sesión para comentar.

Categorías

Preguntada:

el 13 de Ag. de 2018

Comentada:

el 13 de Ag. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by