Azzera filtri
Azzera filtri

Matlab code to delete contents of a subfolders

2 visualizzazioni (ultimi 30 giorni)
Gururaghavendra
Gururaghavendra il 3 Giu 2024
Risposto: Stephen23 il 4 Giu 2024
I have multiple project folder and under each project folder I have a "Inputs" folder. I need to delete contents of the "Inputs" folder only by from all project folder,retaining the empty folder as is. Please can someone help me with the code.

Risposte (2)

Image Analyst
Image Analyst il 3 Giu 2024
Try this. At the bottom of the loop, put in a call to delete or whatever you want:
% Start with a folder and get a list of all subfolders. Use with R2016b and later.
% It's done differently with R2016a and earlier, with genpath().
% Finds and prints names of all files in that folder and all of its subfolders.
% Similar to imageSet() function in the Computer Vision System Toolbox: http://www.mathworks.com/help/vision/ref/imageset-class.html
% Initialization steps:
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
% Define a starting folder.
start_path = fullfile(matlabroot, '\toolbox');
if ~exist(start_path, 'dir')
start_path = matlabroot;
end
% Ask user to confirm the folder, or change it.
uiwait(msgbox('Pick a starting folder on the next window that will come up.'));
topLevelFolder = uigetdir(start_path);
if topLevelFolder == 0
return;
end
fprintf('The top level folder is "%s".\n', topLevelFolder);
% Specify the file pattern.
% Get ALL files using the pattern *.*
% Note the special file pattern. It has /**/ in it if you want to get files in subfolders of the top level folder.
% filePattern = sprintf('%s/**/*.m; %s/**/*.xml', topLevelFolder, topLevelFolder);
filePattern = sprintf('%s/**/*.m', topLevelFolder);
allFileInfo = dir(filePattern);
% % Uncomment this if you want to get two patterns.m and .fig files.
% % Get m files.
% filePattern = sprintf('%s/*.m', topLevelFolder);
% allFileInfo = dir(filePattern);
% % Add on FIG files.
% filePattern = sprintf('%s/*.fig', topLevelFolder);
% allFileInfo = [allFileInfo; dir(filePattern)];
% Uncomment this if you want to get image files.
% % Get PNG files.
% filePattern = sprintf('%s/*.png', thisFolder);
% baseFileNames = dir(filePattern);
% % Add on TIF files.
% filePattern = sprintf('%s/*.tif', thisFolder);
% baseFileNames = [baseFileNames; dir(filePattern)];
% % Add on JPG files.
% filePattern = sprintf('%s/*.jpg', thisFolder);
% baseFileNames = [baseFileNames; dir(filePattern)];
% Throw out any folders. We want files only, not folders.
isFolder = [allFileInfo.isdir]; % Logical list of what item is a folder or not.
% Now set those folder entries to null, essentially deleting/removing them from the list.
allFileInfo(isFolder) = [];
% Get a cell array of strings. We don't really use it. I'm just showing you how to get it in case you want it.
listOfFolderNames = unique({allFileInfo.folder});
numberOfFolders = length(listOfFolderNames);
fprintf('The total number of folders to look in is %d.\n', numberOfFolders);
% Get a cell array of base filename strings. We don't really use it. I'm just showing you how to get it in case you want it.
listOfFileNames = {allFileInfo.name};
totalNumberOfFiles = length(listOfFileNames);
fprintf('The total number of files in those %d folders is %d.\n', numberOfFolders, totalNumberOfFiles);
% Process all files in those folders.
totalNumberOfFiles = length(allFileInfo);
% Now we have a list of all files, matching the pattern, in the top level folder and its subfolders.
if totalNumberOfFiles >= 1
for k = 1 : totalNumberOfFiles
% Go through all those files.
thisFolder = allFileInfo(k).folder;
thisBaseFileName = allFileInfo(k).name;
fullFileName = fullfile(thisFolder, thisBaseFileName);
% fprintf(' Processing file %d of %d : "%s".\n', k, totalNumberOfFiles, fullFileName);
[~, baseNameNoExt, ~] = fileparts(thisBaseFileName);
fprintf('%s\n', baseNameNoExt);
end
else
fprintf(' Folder %s has no files in it.\n', thisFolder);
end
fprintf('\nDone looking in all %d folders!\nFound %d files in the %d folders.\n', numberOfFolders, totalNumberOfFiles, numberOfFolders);
  2 Commenti
Gururaghavendra
Gururaghavendra il 4 Giu 2024
Hello, Thanks a lot for this.
I have added a delete function at the end of the loop as mentioned by you. However the files doesnot get deleted. I maily have to delete the excel sheets, pdf and .msg file formats.
Image Analyst
Image Analyst il 4 Giu 2024
You did put in this call like I told you to, right?
delete(fullFileName);
That should do it. If it doesn't then it will probably throw up an error message in the command window. What error message did you see?
Put a breakpoint on the delete line and look at the file in File Explorer. Then execute that line and see if it goes away. I assume you did that and it was still there, right?
But I'm still thinking the most likely answer is you didn't read my response and put in the call to the delete function.

Accedi per commentare.


Stephen23
Stephen23 il 4 Giu 2024
P = 'absolute or relative path to where the project folders are';
S = dir(fullfile(P,'**','Inputs','*.*'));
for k = 1:nunel(S)
F = fullfile(S(k).folder,S(k).name);
delete(F)
end

Categorie

Scopri di più su COM Component Integration in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by