Solved

How can i delete only READ--ONLY files?

Posted on 2004-10-03
3
156 Views
Last Modified: 2012-05-05
Hi files,

I need to implement something that DELETE only READ-ONLY files... So, if a have FILE1.txt and FILE2.txt, but just FILE2.txt is read-only, i just will DELETE FILE2.txt.

Anyone imagine how could i do this?

Best regards.
0
Comment
Question by:joelsilva
  • 2
3 Comments
 
LVL 13

Accepted Solution

by:
BlackTigerX earned 20 total points
Comment Utility
this procedure will delete any files with read only:

procedure DeleteReadOnlyFiles(const Dir, Mask:string);
var
  S:TSearchRec;
begin
  if (FindFirst(Dir+Mask, faAnyFile-faDirectory, S)=0) then
  repeat
    if (S.Attr and faReadOnly)>0 then
    begin
      SetFileAttributes(PChar(Dir+s.Name), FILE_ATTRIBUTE_NORMAL);
      DeleteFile(Dir+S.Name)
    end
  until (FindNext(S)<>0);
  FindClose(S)
end;


call it like:

DeleteReadOnlyFiles('c:\', '*.txt')

will delete all read only files from the c:\ that match *.txt
0
 

Author Comment

by:joelsilva
Comment Utility
You got it... Thank you very much.
0
 
LVL 13

Expert Comment

by:BlackTigerX
Comment Utility
you can go further and improve the procedure adding the attributes you want to delete files for

procedure DeleteReadOnlyFiles(const Dir, Mask:string; const Attrs=faReadOnly);
var
  S:TSearchRec;
begin
  if (FindFirst(Dir+Mask, faAnyFile-faDirectory, S)=0) then
  repeat
    if (S.Attr and Attrs)>0 then
...

then the default would still be the same, but now you could call it with other attributes if you wanted

DeleteReadOnlyFiles('c:\', '*.txt', faHidden)

of course you would have to rename the procedure name and give it a more appropiate name

best regards
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now