필터 지우기
필터 지우기

How to delete all *.xlsx files in a folder that contain a specific string within the file?

조회 수: 6 (최근 30일)
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.

채택된 답변

dpb
dpb 2017년 6월 8일
편집: dpb 2017년 6월 8일
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
dpb
dpb 2017년 6월 8일
편집: dpb 2017년 6월 8일
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
Image Analyst 2017년 6월 8일
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.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Use COM Objects in MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by