필터 지우기
필터 지우기

How do i delete the contents of a folder?

조회 수: 678 (최근 30일)
shru s
shru s 2017년 6월 11일
댓글: Markus 2024년 1월 26일
could anyone tell me how its done please? I have a number of figures saved in the folder which i want to delete using Matlab. Thank you. the figures are named as figure1 , figure2 and so on. the number of figures i have in my folder changes everytime.

채택된 답변

Image Analyst
Image Analyst 2017년 6월 11일
편집: Image Analyst 2017년 6월 11일
% Specify the folder where the files live.
myFolder = 'C:\whatever';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.fig'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now deleting %s\n', fullFileName);
delete(fullFileName);
end
Or use 'figure*.PNG' if you saved PNG images of your figures instead of .fig files.
  댓글 수: 2
shru s
shru s 2017년 6월 11일
편집: shru s 2017년 6월 11일
Thank you so so much! You are awesome!!
sarah zizou
sarah zizou 2019년 9월 24일
Such a useful script, thanks a lot. I have used to delete my (.mat) files.

댓글을 달려면 로그인하십시오.

추가 답변 (4개)

Damiano Schirinzi
Damiano Schirinzi 2018년 3월 21일
Another quicker solution could be to just use:
delete('PathToFolder\*')
This will delete all the files in the directory specified by PathToFolder, where PathToFolder could be either the absolute or relative path.
If you just want to delete figures with know extension of you could use:
delete('PathToFolder\*.fig')
This will delete all figures in the directory specified by PathToFolder.
Or you could use:
delete('PathToFolder\figure*')
This will delete all files with names starting with figure in the directory specified by PathToFolder.
  댓글 수: 3
Renan Deuter
Renan Deuter 2021년 2월 3일
This doesn't work for me. I suggest it's a problem with the permission windows gives Matlab. Do you know where I can check this?
Jan
Jan 2021년 2월 4일
Please explain "does not work" with any details: Do you get an error message? Does the result differ from your expectations?

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2017년 6월 11일
편집: Jan 2018년 12월 17일
If you want to delete the folder itself along with everything inside it, then you can use rmdir with the 's' option.
rmdir('directory_I_dont_want', 's')
If you want the directory to be kept, then
which_dir = 'directory_to_delete_files_from';
dinfo = dir(which_dir);
dinfo([dinfo.isdir]) = []; %skip directories
filenames = fullfile(which_dir, {dinfo.name});
delete( filenames{:} )
Note: this will not delete any subdirectories or their contents: I exclude them specifically, but delete would refuse to delete them anyhow.
To be honest, what I would probably actually do is
!rm directory_to_delete_files_from/*
but I am using a Unix system and "rm" is not an MS Windows command.
  댓글 수: 1
shru s
shru s 2017년 6월 11일
Thank you so much for taking the time to answer my question. This is very useful. Thank you

댓글을 달려면 로그인하십시오.


Ajay Kumar
Ajay Kumar 2023년 11월 24일
편집: Ajay Kumar 2023년 11월 24일
I have written a simple function for clearing the contents of a folder, please check it out: cleardir. Thanks!
  댓글 수: 1
Markus
Markus 2024년 1월 26일
Thx, does exactly what I needed.

댓글을 달려면 로그인하십시오.


Pit Hoffmann
Pit Hoffmann 2020년 10월 9일
Not sure if anyone needs to delete a folder with subfolders and contents in several of these folders. Here is a small function using rmdir() to delete the hole folder. It's very important to close all data of the folder to delete.
function removeFolderWithContent(path)
% Remove folder and its complete content.
% All data of the folder has to be closed.
%
% path -> path to folder as string
%% get folder content
content = dir(path);
for iContent = 3 : numel(content)
if ~content(iContent).isdir
%% remove files of folder
delete(sprintf('%s\\%s',path,content(iContent).name));
else
%% remove subfolder
removeFolderWithContent(sprintf('%s\\%s',path,content(iContent).name));
end
end
rmdir(path);
end
  댓글 수: 4
Image Analyst
Image Analyst 2021년 1월 27일
That would be a nice idea, if you mean if your functions are shadowed, because lots of built-in toolbox functions have multiple versions and that's ok. For example
>> which -all find
Jan
Jan 2021년 1월 28일
@Jürgen: See:

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by