Reputation: 362
I am trying to make a ntfs disk unwriteable by using up all the empty space. Is there a way I can create a file and set the file size to the max size of disk and then reset it to a less value to make it writeable again? If not what is the fastest way to create a file? I am using delphi. Also the fastest way to remove it will be needed too. Thanks.
Upvotes: 1
Views: 913
Reputation: 27296
You can create a TFileStream
and set its size using its Size
property. Something like this...
var
F: TFileStream;
begin
F:= TFileStream.Create('C:\BigFile', fmCreate);
try
F.Size:= 50000000000;
finally
F.Free;
end;
This would create a file C:\BigFile
with a size of about 46GB.
You would still have to calculate how big of a file to create based on the amount of free space. Keep in mind that this doesn't actually write any data, it only instructs the file system that the file is a certain size.
To delete the file, just do the same only set the size to 0...
var
F: TFileStream;
begin
F:= TFileStream.Create('C:\BigFile', fmCreate);
try
F.Size:= 0;
finally
F.Free;
end;
DeleteFile('C:\BigFile');
EDIT
During my test of large files, I realized that you have to use Int64
instead of Integer
in order to accommodate for these large sizes.
Also bear in mind that there are files constantly changing, especially on the Windows system drive. Even two sequential calls to DiskFree
and TFileStream.Size
could result in an error of not enough disk space available. I'd suggest to write a file for example 100MB less than the free space, then add 1MB at a time until it fails, then add 1KB at a time until it fails, then add 1 byte at a time until it fails. Then you'll know you can guarantee that all space is taken without just raising a single error and completely failing.
This would begin with something like this...
var
F: TFileStream;
begin
F:= TFileStream.Create(FILENAME, fmCreate);
try
F.Size:= DiskFree(3) - (1024 * 1024 * 100);
while 1 = 1 do begin
try
F.Size:= F.Size + (1024 * 1024);
except
Break;
end;
end;
while 1 = 1 do begin
try
F.Size:= F.Size + 1024;
except
Break;
end;
end;
while 1 = 1 do begin
try
F.Size:= F.Size + 1;
except
Break;
end;
end;
finally
F.Free;
end;
Note this could be constructed better (as far as error handling), but this should give you a good start.
Upvotes: 2