Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Deleting a File in disk using partial search

Posted on 2003-03-08
5
Medium Priority
?
197 Views
Last Modified: 2010-04-04
hi,

I want to delete all files with the substring Salesxx
for instance SalesxxWed092003,SalesxxTue032003, from my disk(c),

hint: Ability to do a partial match

please Assist...
0
Comment
Question by:Muthenya
4 Comments
 
LVL 2

Accepted Solution

by:
steve_hsk earned 176 total points
ID: 8093585
Hi Muthenya ...

There are two parts to your problem :-
1. Find the files you want to delete,
2. Deleting these files to the Recycle Bin.

Due to the ease of being able to delete your entire hard-drive in seconds, I would suggest finding the necessary files first, displaying these files, checking they're correct, and finally deleting them to the Recycle bin so they can be emyptied or restored at a later date.

Part 1:
Finding the files can be written yourself, or done very easily by using one the following components, which include source code, use of wildcards for partial matches, recursive searches for sub-folders etcetc :
http://www.torry.net/search.htm

** This is not a complex task if broken down neatly ;-)

The partial search is normally undertaken by putting a wrapper around TSearchRec, and the undocumented IsWild function, defined in StrUtils.Pas

Part 2:
Deleting the files to the Recycle bin : code snippet for ya :

uses ShellApi, etc, etc ...

procedure TBtchOp_Delete.SendToRecycleBin (FileName: string);
var
  SHF: TSHFileOpStruct;
begin
  with SHF do begin
    Wnd := Application.Handle;
    wFunc := FO_DELETE;
    pFrom := PChar(FileName);
    fFlags := FOF_SILENT or FOF_ALLOWUNDO;
  end;
  SHFileOperation(SHF);
end;

PS : I use TFileFinder.Pas by William Yang (his homepage no lopnger exists, but if you can't find this on the web-I'll email it to you)

Hope this helps,
Steve
0
 
LVL 5

Assisted Solution

by:Lukasz Lach
Lukasz Lach earned 172 total points
ID: 8093658
procedure DeleteMatch(const dir, path, name: String);
var
        SR : TSearchRec;
        Found : Integer;
        source : String;
begin
        source := dir + path;
        Found := FindFirst(source+'\'+name, faAnyFile, SR );
        try
                while  (Found = 0)  do
                begin
                        if (SR.Name<>'.') and (SR.Name <> '..') then
                        begin
                                if (SR.Attr and faDirectory) <> 0 then
                                    DeleteMatch( dir, path+'\'+SR.Name )
                                else
                                begin
                                        FileSetAttr(
                                        source+'\'+SR.Name, FileGetAttr(source+'\'+SR.Name)
                                        and
                                        not (faReadOnly or faHidden) );
                                        DeleteFile( PChar(source+'\'+SR.Name) )
                                end;
                        end;
                        Found := FindNext( SR );
                end;
        finally
                Sysutils.FindClose(SR);
        end;
end;

use:
DeleteMatch('C:\', '', 'sales??Wed092003.*');
0
 

Expert Comment

by:CleanupPing
ID: 9316889
Muthenya:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 5

Expert Comment

by:snehanshu
ID: 10090409
Hi!
No comment has been added lately and this question is therefore classified abandoned.

If asker wishes to close the question, then refer to
http://www.experts-exchange.com/help/closing.jsp

Otherwise, I will leave a recommendation in the Cleanup topic area that this question is:

Split between steve_hsk and anAKiN

Please leave any comments here within the next seven days. It is assumed that any participant not responding to this request is no longer interested in its final disposition.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

...Snehanshu
EE Cleanup Volunteer
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Integration Management Part 2
Loops Section Overview

578 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