How to delete all files with a certain extension?

I have a directory with several .png files that I would like to delete in a script. I have the following script that I have modified from a script I found in the Matlab forums:
d = dir(file_path);
file_names = {d.name}';
count = 1;
for jj = 1:length(file_names)
pngFilePattern = char(fullfile(file_path,'*.png'));
pngFiles = dir(pngFilePattern);
count = 1;
for kk = 1:length(pngFiles)
pngBaseFileName = pngFiles(kk).name;
pngFullFileName = fullfile(pngFiles(kk).folder,pngBaseFileName);
delete(pngFullFileName)
end
count = count + 1;
end
I've simplied this script by just using "file_path" in this forum post instead of the full path of the file. This code seems to work (all my .png files get deleted), but I think I may have a problem in this script because I can't get "count" to equal the number of files in the directory that I want to delete. The final value of "count" is several multiples of the number of .png files that need to be deleted.
Can someone please tell me how I can modify (and hopefully simplify) this script, so I can make sure that only the .png files that to be deleted are deleted?
Thank you!

 Accepted Answer

dpb
dpb on 11 Oct 2022
Edited: dpb on 12 Oct 2022
Because your count variable is being incremented in the outer loop which is running over a dir() list of all files (including subdirectory names and the two "dot" directory entries), not just over the *.png files.
If you're only worried about the single directory, you don't need (and don't want) the outer loop; just use
pngFiles=dir(fullfile(file_path,'*.png');
count = numel(pngFiles);
for i=1:count
delete(fullfile(pngFiles(i).folder,pngFiles(i).name);
end
But, you don't need to iterate over all files, simply
delete *.png
will do the trick in the current directory or
delete(fullfile(fullfile(file_path,'*.png'))
wipes 'em all out in the subject directory.

3 Comments

Thank you for your help!
I'm trying your last command, but I'm having trouble. I've tried the following from the Matlab Command Window:
>> fullfile(pwd,input_all_single_test{1}.raw_data_daily_directory,input_all_single_test{1}.daily_test_directories(k))
ans =
1×1 cell array
{'F:\Projects 3-12-21\Pittsburgh - Upper Ohio\FE System\Technical\Model Operation and Data\Plan C\20220607\MNTFEPLANC-224-202206071441'}
>> char(fullfile(pwd,input_all_single_test{1}.raw_data_daily_directory,input_all_single_test{1}.daily_test_directories(k)))
ans =
'F:\Projects 3-12-21\Pittsburgh - Upper Ohio\FE System\Technical\Model Operation and Data\Plan C\20220607\MNTFEPLANC-224-202206071441'
>> delete(char(fullfile(pwd,input_all_single_test{1}.raw_data_daily_directory,input_all_single_test{1}.daily_test_directories(k))),'*.png')
Warning: 'F:\Projects 3-12-21\Pittsburgh - Upper Ohio\FE System\Technical\Model Operation and Data\Plan C\20220607\MNTFEPLANC-224-202206071441' is a directory. Use
rmdir to delete directories.
The .png files I want to delete are definitely in the MNTFEPLANC-224-202206071441 directory, but I can't figure out what I'm doing wrong. Do you see what I'm messing up?
Thank you!
"Do you see what I'm messing up?"
You omitted '*.png' from the FULLFILE command.
(it is not an input to the DELETE command as you attempted)
That fixed my problem! Thank you!

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2021b

Asked:

on 11 Oct 2022

Commented:

on 13 Oct 2022

Community Treasure Hunt

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

Start Hunting!