PDA

View Full Version : deletion of binary files



War Robe
17-02-2006, 03:48 PM
hi everybody,

I've got a computing project to do and so I'm working on a database. Now the problem is that I don't know (neither can't find) how to delete binary files (within the program) that were created in the program.

Can anyone provide me with a sample code if possible pls? I would really appreciate it!

(Oh and as regards Validations [example: checking whether no numbers have been inserted in the 'name' field], any help on those pls aswell! But primarily the deletion of binary files is my major problem)

Thankyou so much in advance,
War Robe

jdarling
17-02-2006, 05:45 PM
Deleteion of any file is simply:

if FlieExists(FileName) then
DeleteFile(FileName);

As for validation, you can achieve it on two levels. For example if your using a standard Edit control (TEdit) then in its OnKeyPress event you can place code to trap and kill invalid characters:

procedure NameKeyPress(Sender:TObject; var Key:Char);
begin
if not (Key in ['A'..'Z','a'..'z']) then // check for anything but a-z
Key := #0; // This kills the key stroke
end;

Take a read through the Delphi help files, and on some basic UI tutorials. Theirs lots of infomraiton out their on this type of stuff. A good resource is Tamaracka.com

War Robe
18-02-2006, 10:00 AM
Thanks so much for ur help jdarling. Regarding the 'file exists' I thought it could only be used in text files. But if it works on binary files, where should I put the coding lines pls? At the begining before creating the file?

Cos I need to do an option that if the user selects it, then he types in the file name to be deleted and so it gets deleted.

Thanks alot,
War Robe

jdarling
20-02-2006, 02:11 PM
Thanks so much for ur help jdarling. Regarding the 'file exists' I thought it could only be used in text files. But if it works on binary files, where should I put the coding lines pls? At the begining before creating the file?

Cos I need to do an option that if the user selects it, then he types in the file name to be deleted and so it gets deleted.

Thanks alot,
War Robe

Well you would place the code where it would seem most appropriate according to your needs. For example if your wanting to delete an existing file before you create or download the new version then you would place the if exists and delete before you create or download.

So for example if you have the following code:

procedure MyCreateNewFile;
if SaveDialog1.Execute then
begin
// Your code here to create
end;
end;

Then you might modify it something to this effect:

procedure MyCreateNewFile;
if SaveDialog1.Execute then
begin
if FileExists(SaveDialog1.FileName) then
DeleteFile(SaveDialog1.FileName);
// Your code here to create
end;
end;

War Robe
20-02-2006, 05:56 PM
But is
DeleteFile a built in code? Like the
FileExists? Beacuse it gives me an 'unknown identifier' error.

Thanks for the rest,
War Robe

JSoftware
20-02-2006, 06:23 PM
deletefile is a procedure in the windows api
fileexists is a function from the sysutils unit

you need to include both in your uses clause(eg. "uses sysutils, windows;")

Regards,
Jeppe

Robert Kosek
20-02-2006, 07:28 PM
War Robe, if you are using the TFileStream object you can overwrite files easier than these workarounds.

michalis
21-02-2006, 10:43 AM
deletefile is a procedure in the windows api


Actually, DeleteFile is also declared in SysUtils unit, al least for FPC and Kylix 3 (so probably also for Delphi >= 6). For portability it's better to not use the version in Windows unit.

So (assuming you use FPC or newer version of Delphi) it should be enough for you to add SysUtils unit to your uses clause.

JSoftware
21-02-2006, 11:03 AM
oh ofcourse :oops:

i would use the sysutils version of deletefile

you don't need the windows unit included... :P

War Robe
21-02-2006, 03:10 PM
Thanks alot for the info! Although I can't fin a website where I can download the unit (since I don't have one in the units folder).

Any suggestions pls?

Robert Kosek
21-02-2006, 03:47 PM
Since it comes with Delphi... just include it.

JSoftware
21-02-2006, 05:23 PM
i sense that you are using fpc?

Have you remembered to set up search paths?

War Robe
21-02-2006, 06:41 PM
sry guys but this is all non-sense to me (sry for that :oops: ) I am using Borland Turbo Pascal Version 0.7 and the unit I requested is not in my folders.

AthenaOfDelphi
21-02-2006, 08:43 PM
Hi War Robe,

Turbo Pascal... that takes me back... IIRC (I haven't used Turbo 7 since about 1996, so I could be wrong), it has no built in deleteFile or fileExists functions, so heres some I made a long time ago. I think these are right, as I said, its a long time since I used Turbo:-



type
string255 = string[255];

procedure deleteFile(fileName:string255);
var
f : file of byte;
r : integer; (* Type of this could be wrong, adjust accordingly *)
begin
assign(f,filename);
{$I-}
erase(f);
{$I+}
r:=IOResult;
end;

function fileExists(fileName:string255):
var
f : file of byte;
r : integer; (* Again, the type could be wrong *)
begin
assign(f,filename);
{$I-}
reset(f);
{$I+}
result:=(IOResult=0);
{$I-}
close(f);
{$I+}
r:=IOResult;
end;


I'm guessing, you've not used too many compiler directives yet, so heres a quick explanation of how these routines work without resulting in a run-time error.

Normally, when an I/O error occurs (file doesn't exist when you open it with reset for example) the program will bomb out with an error dialogue. If that happens, these functions are useless, so we need to turn off I/O error checking temporarily. To do this, you use the {$I-} compiler directive. This turns off I/O error checking. When you do this, you MUST call IOResult to get the error code (if any) that was returned (IIRC, these codes are the same as the I/O Error message code that might normally be displayed). If you don't call IOResult, and you turn I/O error checking on with the {$I+} compiler directive, the next I/O operation will fail.

So, in the case of deleteFile, we turn off error checking and use the 'erase' function. If the file exists, it is deleted. If not, nothing happens. We turn on error checking and then clear the I/O error result buffer by calling IOResult. In the case of fileExists, we turn off error checking, and attempt to open the file. If it exists, the function succeeds and IOResult should return 0. If it doesn't exist, the function fails and IOResult will return a non-zero value. We turn error checking on, check the IOResult value. And just in case we did open the file, we turn off error checking, close the file, turn error checking back on and then read IOResult again to clear the result buffer.

I hope this helps.

War Robe
22-02-2006, 06:58 PM
thanku sooo much AthenaOfDelphi :D that really helped me out!! You literally saved my project! :)

Thanks again,
War Robe

AthenaOfDelphi
23-02-2006, 09:30 AM
You're more than welcome hon, glad it helped.