Board index » delphi » Getting file size of a read only file

Getting file size of a read only file

The usual method for obtaining the size of a file does not work if it
has the read only bit set.  How do you get the file size in this case?
You could always clear the read only bit, get the file size and then set
it back again but this won't work on a CD.  The way I would normally
measure it is basically (leaving out try/except etc.)-

f: file of byte;

AssignFile(f, strFilename);
Reset(f);//crashes here
iFileSize := FileSize(f);
CloseFile(f);

Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.

 

Re:Getting file size of a read only file


hi,

i think the GetFileSize WinApi might be of use for you - but I dont
know exactly how to call it.

Good Luck,

Philipp Traeder

Quote
th...@my-deja.com wrote:

> The usual method for obtaining the size of a file does not work if it
> has the read only bit set.  How do you get the file size in this case?
> You could always clear the read only bit, get the file size and then set
> it back again but this won't work on a CD.  The way I would normally
> measure it is basically (leaving out try/except etc.)-

> f: file of byte;

> AssignFile(f, strFilename);
> Reset(f);//crashes here
> iFileSize := FileSize(f);
> CloseFile(f);

> Sent via Deja.com http://www.deja.com/
> Share what you know. Learn what you don't.

Re:Getting file size of a read only file


I had no problem with the code you posted. Nor did I have a problem with

var    strm : tFileStream;

strm := tFileStream.Create (strFileName, fmReadOnly);
iFileSize := strm.Size;
strm.Free;

I tested the code with on a CD.

Quote
<th...@my-deja.com> wrote in message news:7s6ahe$536$1@nnrp1.deja.com...
> The usual method for obtaining the size of a file does not work if it
> has the read only bit set.  How do you get the file size in this case?
> You could always clear the read only bit, get the file size and then set
> it back again but this won't work on a CD.  The way I would normally
> measure it is basically (leaving out try/except etc.)-

> f: file of byte;

> AssignFile(f, strFilename);
> Reset(f);//crashes here
> iFileSize := FileSize(f);
> CloseFile(f);

> Sent via Deja.com http://www.deja.com/
> Share what you know. Learn what you don't.

Re:Getting file size of a read only file


th...@my-deja.com schrieb:

Quote

> The usual method for obtaining the size of a file does not work if it
> has the read only bit set.  How do you get the file size in this case?
> You could always clear the read only bit, get the file size and then set
> it back again but this won't work on a CD.  The way I would normally
> measure it is basically (leaving out try/except etc.)-

> f: file of byte;

> AssignFile(f, strFilename);
> Reset(f);//crashes here
> iFileSize := FileSize(f);
> CloseFile(f);

Use one of the parallel suggestions or, if you want to do this your way,
set the global variable FileMode to 0 (readonly) before Reset(f) and
back to the original state (commonly 2, readwrite) after it. See
FileMode in the OH for more details.

-Michael

Re:Getting file size of a read only file


In article <37E72D30.DDD64...@aquila.de>,
  Michael Winter <m...@aquila.de> wrote:
Quote
> th...@my-deja.com schrieb:

> > The usual method for obtaining the size of a file does not work if
it
> > has the read only bit set.  How do you get the file size in this
case?
> > You could always clear the read only bit, get the file size and then
set
> > it back again but this won't work on a CD.  The way I would normally
> > measure it is basically (leaving out try/except etc.)-

> > f: file of byte;

> > AssignFile(f, strFilename);
> > Reset(f);//crashes here
> > iFileSize := FileSize(f);
> > CloseFile(f);

> Use one of the parallel suggestions or, if you want to do this your
way,
> set the global variable FileMode to 0 (readonly) before Reset(f) and
> back to the original state (commonly 2, readwrite) after it. See
> FileMode in the OH for more details.

> -Michael

the files I was looking at were on a network so the problem could have
been caused by not having sufficient rights to those files.  Doesn't
matter anyway, I worked around it by measuring the size of the file
after I have taken a copy - I had to copy it anyway!

Trevor

Sent via Deja.com http://www.deja.com/
Before you buy.

Other Threads