Solved

How can i delete only READ--ONLY files?

Posted on 2004-10-03
3
163 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Suggested Courses
Course of the Month8 days, 5 hours left to enroll

617 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