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

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
rngAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Wim ten BrinkSelf-employed developerCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rngAuthor Commented:
DeleteFile is the only command which can delete files ??
I think there is a command that can accept wildcards.
Thanks.

rng
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

kretzschmarCommented:
you may use the SHFileOperation - API

guess someone can provide a sample, or?

i myself -> no time, no equipment

meikl ;-)
0
Wim ten BrinkSelf-employed developerCommented:
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
rngAuthor Commented:
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
Wim ten BrinkSelf-employed developerCommented:
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
rngAuthor Commented:
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
Wim ten BrinkSelf-employed developerCommented:
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
geobulCommented:
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
Wim ten BrinkSelf-employed developerCommented:
Geo is right. You must make sure you use the right options. One bug could do more damage than you would expect...
0
rngAuthor Commented:
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
Wim ten BrinkSelf-employed developerCommented:
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
rngAuthor Commented:
> 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
Wim ten BrinkSelf-employed developerCommented:
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
rngAuthor Commented:
> 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
Wim ten BrinkSelf-employed developerCommented:
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
rngAuthor Commented:
Ok, thanks again!!

rng
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.