How to delete all *.xlsx files in a folder that contain a specific string within the file?
5 views (last 30 days)
Show older comments
I would like to read each *.xlsx file (~70) in a folder and delete each respective file containing a specific string while leaving the *.xlsx files that don't contain the string in the folder.
0 Comments
Accepted Answer
dpb
on 8 Jun 2017
Edited: dpb
on 8 Jun 2017
You'll have to simply iterate over the collection of files, reading each and searching for the string and then delete that file if found. The <FAQ> is helpful; particularly for something like the above the dir solution is quite useful.
d=dir('*.xlsx'); % list of the files
for i=1:length(d)
[~,txt]=xlsread(d(i).name,1,'M6'); % read the text
if strcmp(txt,'DepthLHS')
delete(d(i).name)
end
end
If DepthLHS is a variable instead of a literal string, make the obvious substitution. NB: This is essentially duplicate of at least two others (I deleted one as being identical other than the file extension which is totally immaterial); the other has a question of how to modify a document.
As for that secondary question, on Windows you use the AciveX/COM interface for the given application; specific commands for that are part of the documentation for the application and are NOT a Matlab question; you'll have better luck on those with a MS-specific forum.
There are some examples of working with Word/Excel in the Matlab documentation that may get you started in that direction.
3 Comments
dpb
on 8 Jun 2017
Edited: dpb
on 8 Jun 2017
I fixed up the Answer to fix that case for Excel.
Sorry, I mistyped the curlies instead of regular parens for the directory structure array reference.
As for Word documents, "dunno"; I'm pretty sure they're stored in some compressed, nontext format but no idea what it actually is. Well, let's see--if I look at a local .doc file in a file dump utility I see that it looks like the text of the letter is all strung together with all the formatting info arround it and then a "MERGEFORMAT" meta-command at the end of the block.
So, I suppose you could read the file as stream into character array and do the search for the string if it is truly unique.
But, to do something like delete a page or the like you'll just have to interact with the document and I know of no programmatic way outside the COM interface or macros or the like.
Image Analyst
on 8 Jun 2017
Make it easy for people to help you, not hard. Attach at least two files, one that has the string and needs to be deleted, and one that doesn't have it and should remain.
More Answers (0)
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!