[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Delete files ("C:\Temp\*.*" ), how??

Posted on 2004-11-03
19
Medium Priority
?
8,268 Views
Last Modified: 2010-08-05
Hello,

I would like to delete 'C:\Temp\*.*' permanently (without sending to recycle bin) by delphi's code.
DeleteFile doesn't work for wildcards. Which function can I use ?

Thanks.

rng
0
Comment
Question by:rng
  • 8
  • 7
  • 2
  • +2
19 Comments
 
LVL 17

Accepted Solution

by:
Wim ten Brink earned 800 total points
ID: 12481536
var SearchRec:TSearchRec;
begin
  if (FindFirst('C:\Temp\*.*', faAnyfile-faDirectory, SearchRec) = 0) then begin
    repeat
      DeleteFile('C:\Temp\'+SearchRec.Name);    
    until (FindNext(SearchRec) <> 0);
    FindClose(SearchRec);
  end;
end;

Above code is not tested so it might have a compiler error but it gives you an idea of a possible solution...
0
 

Author Comment

by:rng
ID: 12481586
DeleteFile is the only command which can delete files ??
I think there is a command that can accept wildcards.
Thanks.

rng
0
Technology Partners: 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!

 
LVL 27

Expert Comment

by:kretzschmar
ID: 12481779
you may use the SHFileOperation - API

guess someone can provide a sample, or?

i myself -> no time, no equipment

meikl ;-)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 12481797
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 12481808
DeleteFile is indeed the only function in Delphi that deletes files without using the recycle bin. Unless you want to use the SHFileOperation() function in the ShellAPI unit.

uses ShellAPI;

var
  FileOp: TSHFileOpStruct;
begin
  FileOp.Wnd := GetDesktopWindow;
  FileOp.wFunc := FO_DELETE;
  FileOp.pFrom := 'C:\Temp\*.*'#0;
  FileOp.pTo := nil;
  FileOp.fFlags := FOF_NOCONFIRMATION;
  SHFileOperation( FileOp );
end;

Above code will do the same, using a wildcard. It will NOT move them to the recycle bin. But personally, I just dislike this method since it invokes the shell to delete the files. It will also show a dialog box unless you specify that it should not do this. And I just feel I don't have enough control with this function. Even error handling is a bit troublesome, since it will show a messagebox with an error text on an error, unless you add FOF_NOERRORUI to the flags. I also dislike the additional #0 that you have to add to the from parameter, because this function expects a list of names here, separated by a #0.
Then again, you could use:
  FileOp.pFrom := 'C:\Temp\*.cfg'#0'C:\Temp\*.dcu'#0'C:\Temp\*.dpr'#0'C:\Temp\*.dfm'#0'C:\Temp\*.pas'#0;
to delete just multiple files with different filemasks. Makes it a bit more powerful than my first solution. So sometimes it does have it's advantages. But in general I just dislike it.

And it's about as many lines of code as the other solution...
0
 

Author Comment

by:rng
ID: 12484041
I am a bit confused. Actually I am new to delphi and I thought deleting files is just a simple thing....

Could you provide a simple and work for sure solution ?

Thanks.

rng
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 12484118
Well, both examples I've given will work if used correctly. You already have two examples. The problem is that Delphi does NOT have a simple function for deleting files using a wildcard. You would have to create that function yourself.
The problem isn't Delphi, btw. It's just the Windows API that just lacks this kind of functionality. My first example is more reliable, the second version just uses the shell (explorer) to delete files permanently.
0
 

Author Comment

by:rng
ID: 12484226
Ok, I will try it myself and let you know the result.
BTW, why do you think it is no good to use the shell to delete files??

Thanks.

rng
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 12484452
I just dislike to use the shell functions because they take away a bit of the control I have. With my first solution I could check the return value of DeleteFile since it's a boolean function. If the deletion failed, I could retrieve extended error information by checking SysErrorMessage(GetLastError) and add this string result to a stringlist that logs all errors while I just continu to delete all other files. Once I'm done deleting I could display the user a list of files that could not be deleted.
With the shell function I won't be able to get the same level of error handling...

I dislike the lack of control you have. Furthermore, you have to make sure to use the right flags and make sure the filename ends with a #0 at the end. (Actually, two #0 characters but since I assign the value to a PChar type, Delphi will add the second one for me.) The shell function is a bit too complicated to my liking since it can also be used to move or copy or rename multiple files. I don't like functions that are overloaded this much... One mistake and this function might do something unexpected.
0
 
LVL 17

Expert Comment

by:geobul
ID: 12485480
About that SHFileOperation call: I'd add FOF_FILESONLY to the flags if I don't want any subfolders to be deleted also:

FileOp.fFlags := FOF_NOCONFIRMATION or FOF_FILESONLY or FOF_SILENT;

Otherwise (as  Workshop_Alex already pointed out ;-) you might experience unexpected results.

Regards, Geo
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 12488757
Geo is right. You must make sure you use the right options. One bug could do more damage than you would expect...
0
 

Author Comment

by:rng
ID: 12490435
Hi Alex,

I have tried your 1st code, it works great.
But it showed two warning messages during compile:

[Warning] Main.pas(917): Symbol 'faSysFile' is specific to a platform
[Warning] Main.pas(917): Symbol 'faHidden' is specific to a platform

Do you know how to get rid of them ?
Thanks.

rng
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 12492749
Well, simple. Delphi warns you that those values are invalid if you're going to compile your project for .NET so what you have to do is just disable the platform dependency warnings. :-) Or just ignore them. I don't think you need to use faSysFile, though, unless you want to exclude them too... faAnyfile-faDirectory-faSysFile would exclude folders AND system files.
0
 

Author Comment

by:rng
ID: 12493093
> compile your project for .NET ??
I am using Delphi 7, not Delphi for .NET, why will it show this warning?

> I don't think you need to use faSysFile
Yes, I think it is unsafe to delete system files, so I exclude them.

Thanks.

rng

0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 12495290
Well, Delphi 7 has some additional warnings that you can disable with the project options. The reason for this is because Delphi 7 wants to make developers aware of possible compatibility problems when you upgrade to Delphi 8. These are mostly pointer-related or related to some other functionality that are defined as being platform-dependant.
It's not just for .NET that Delphi includes these warnings. It's also used in case you want to create code that also needs to compile with Kylix. Am using Delphi 5 right now so can't look for which settings you need to turn off to remove these platform dependency warnings. You don't need to be warned about them because you're only compiling for a single platform.

(I wonder how Borland has solved this in Delphi 2005, which should be available on the market around now...)
0
 

Author Comment

by:rng
ID: 12500216
> You don't need to be warned about them because you're only compiling for a single platform.
"single platform" means all existing Windows, right ?

Thanks for your info.

rng
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 12502398
Yep. All existing windows versions. Or all existing Linux/BSD versions if you use Kylix. Or all .NET versions if you use Delphi 8 for .NET. Delphi is supported on three platforms now, although it seems that the Linux support is slowly disappearing again.
0
 

Author Comment

by:rng
ID: 12502419
Ok, thanks again!!

rng
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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…
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…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses

834 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