Getting file information

Discussions related to database technologies, file handling, directories and storage
simong42
Posts: 5
Joined: Tue 05 Mar 2019, 20:47

Getting file information

Post by simong42 »

I'm trying to access information about a specific file - in particular, the creation date/time.
I can do this by generating a directory listing (thanks to this forum for their help in resolving my "dir /s" query), but for some reason, I get different date/times when looking at the file on different media (rounding / truncation maybe?).

I looked in the online help, and found information about reading through the contents of a directory, and deriving such attributes as creation date/time, but could not see how to get this for a *specific* file.

(And after looking at other threads in this forum, I found this wiki - http://www.bbcbasic.co.uk/wiki/doku.php ... nd_20files - and bookmarked it - but couldn't see an answer to this question there.)

DDRM
Posts: 170
Joined: Mon 02 Apr 2018, 18:04

Re: Getting file information

Post by DDRM »

Hi Simon,

The technique shown in the manual under "Listing the disk directory" should allow you to find what you need?

Best wishes,

D

DDRM
Posts: 170
Joined: Mon 02 Apr 2018, 18:04

Re: Getting file information

Post by DDRM »

Hi Simon,

Sorry to be so long getting back on this. With a bit of playing, and help from Richard, I've come up with this, which may help. It's a bit fiddly because some data gets returned in 16 bits, so an integer picks up two, while others are returned in 64 bits, so you need a long integer. Weirdly, the file size is 64 bits, but the "wrong way round", so you need to read it as high and low sections.

I'll have a think about making a function which takes a filetime and a systemtime structure and returns the requested bit of information, but I'll have to swot up a bit on passing structures to functions....

Best wishes,

D

Code: Select all

      DIM FileAttributes{atts%,creation%%,last_access%%,last_write%%,fsize_High%,fsize_Low%}
      DIM SystemTime{Y_M%,DoW_D%,Hr_Min%,Sec_MS%}

      f$=@dir$+"test_for_file_attributes.bbc"

      SYS "GetFileAttributesExA",f$,0,FileAttributes{}
      PRINT "File size: ";FileAttributes.fsize_Low%
      PRINT "Creation time:"; FileAttributes.creation%%
      PRINT "Last access time:"; FileAttributes.last_access%%
      PRINT"For me, the last access time comes up the same as creation time!"
      PRINT "Last write time:"; FileAttributes.last_write%%
      SYS "FileTimeToSystemTime",^FileAttributes.creation%%,SystemTime{} TO flag%
      IF flag%=0 THEN PRINT "FAILED!"
      PRINT "Created in year: ";SystemTime.Y_M% AND &FFFF
      PRINT "Created in month: ";SystemTime.Y_M% >>16
      PRINT "Created on DoW: ";SystemTime.DoW_D% AND &FFFF
      PRINT "Created on Day: ";SystemTime.DoW_D% >>16
      PRINT "Created at Hr: ";SystemTime.Hr_Min% AND &FFFF
      PRINT "Created at Min: ";SystemTime.Hr_Min% >>16

      SYS "FileTimeToSystemTime",^FileAttributes.last_write%%,SystemTime{} TO flag%
      IF flag%=0 THEN PRINT "FAILED!"

      PRINT "Modified in year: ";SystemTime.Y_M% AND &FFFF
      PRINT "Modified in month: ";SystemTime.Y_M% >>16
      PRINT "Modified on DoW: ";SystemTime.DoW_D% AND &FFFF
      PRINT "Modified on Day: ";SystemTime.DoW_D% >>16
      PRINT "Modified at Hr: ";SystemTime.Hr_Min% AND &FFFF
      PRINT "Modified at Min: ";SystemTime.Hr_Min% >>16

Edja
Posts: 26
Joined: Tue 03 Apr 2018, 12:07

Re: Getting file information

Post by Edja »

Hi D. ,

On 22 Mar 2019 you 've posted some prototype code that deals with the FileAttributes.
I think you were still trying to solve a few details. I 've just tried the code you 've posted and it runs but doesn't produce any correct data.
Did you follow through on this ? If yes, would you be prepared to share the result ?
I could use a function that returns file attributes for a specific file (creation date, ...)

regards,
Edja

RichardRussell
Posts: 312
Joined: Tue 15 Oct 2019, 09:10

Re: Getting file information

Post by RichardRussell »

Edja wrote:
Sat 24 Aug 2019, 13:24
I 've just tried the code you 've posted and it runs but doesn't produce any correct data.
DDRM's code returns correct data for me:

Code: Select all

Created in year: 2020
Created in month: 2
Created on DoW: 3
Created on Day: 26
Created at Hr: 16
Created at Min: 24
Modified in year: 2020
Modified in month: 2
Modified on DoW: 3
Modified on Day: 26
Modified at Hr: 16
Modified at Min: 24
The only way in which the result might be considered 'surprising' is that the creation and modification times are given as '16:24' by the program but as '17.24' by the dir command in Windows. This is because David has used SYS "FileTimeToSystemTime", which returns a UTC date and time, rather than FileTimeToLocalFileTime followed by FileTimeToSystemTime, which is effectively what dir does.

Of course on the date that the file was created Daylight Saving Time was not in effect (in the UK) so it really was created at 16:24 by the clock, but the conversion to 'local time' is done according to the time zone now, not when the file was created!
If you have a comment about the style or tone of this message please report it to the moderators by clicking the exclamation mark icon, rather than complaining on the public forum.