Solved

How can i delete only READ--ONLY files?

Posted on 2004-10-03
3
157 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
ID: 12214648
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
ID: 12215439
You got it... Thank you very much.
0
 
LVL 13

Expert Comment

by:BlackTigerX
ID: 12217870
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

911 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

26 Experts available now in Live!

Get 1:1 Help Now