Re: file properties with DIR function : Bug ?

new topic     » goto parent     » topic index » view thread      » older message » newer message

On Mon, 21 Aug 2006 06:44:07 -0700, Robert Craig
<guest at RapidEuphoria.com> wrote:

>I simply retrieve a C structure, and have to multiply the "seconds" 
>field by 2, since for some reason Microsoft doesn't store the "seconds" 
>but rather the "seconds divided by two". Maybe it's a Y2K bug  smile 
That's odd. On my win98 box here, using kernel32 api, I just peek2u
the seconds and it is spot on. Try this:
-- dir() clone using windows api:
include dll.e
include machine.e

if platform()!=2 then ?9/0 end if   --Win32 rqd (for kernel32!)

constant 
    -- FILETIME structure:
--  FTdwLowDateTime     = 0,    --  DWORD dwLowDateTime
--  FTdwHighDateTime    = 4,    --  DWORD dwHighDateTime
    FTsize              = 8,
    -- WIN32_FIND_DATA structure:
    FDwFileAttributes   = 0,    --  DWORD dwFileAttributes
--  FDtCreationTime     = 4,    --  FILETIME ftCreationTime
--  FDtLastAccessTime   = 12,   --  FILETIME ftLastAccessTime
    FDtLastWriteTime    = 20,   --  FILETIME ftLastWriteTime
    FDnFileSizeHigh     = 28,   --  DWORD    nFileSizeHigh
    FDnFileSizeLow      = 32,   --  DWORD    nFileSizeLow
--  FDwReserved0        = 36,   --  DWORD    dwReserved0
--  FDwReserved1        = 40,   --  DWORD    dwReserved1
    FDcFileName         = 44,   --  TCHAR    cFileName[ MAX_PATH(=260)
]
--  FDcAltFileName      = 304,  --  TCHAR    cAlternateFileName[ 14 ]
    FDsize              = 318,
    -- SYSTEMTIME structure:
    STwYear             = 0,    --  WORD wYear
    STwMonth            = 2,    --  WORD wMonth
--  STwDayOfWeek        = 4,    --  WORD wDayOfWeek
    STwDay              = 6,    --  WORD wDay
    STwHour             = 8,    --  WORD wHour
    STwMinute           = 10,   --  WORD wMinute
    STwSecond           = 12,   --  WORD wSecond
--  STwMillisecs        = 14,   --  WORD wMilliseconds
    STsize = 16,

    INVALID_HANDLE_VALUE = -1,
    
    kernel32 = open_dll("kernel32.dll"),
    C_PTR = C_POINTER,
    xFindFirstFile =
define_c_func(kernel32,"FindFirstFileA",{C_PTR,C_PTR},C_PTR),
    xFindNextFile =
define_c_func(kernel32,"FindNextFileA",{C_PTR,C_PTR},C_INT),
    xFindClose = define_c_proc(kernel32,"FindClose",{C_PTR}),
    xFileTimeToLocalFileTime =
define_c_func(kernel32,"FileTimeToLocalFileTime",{C_PTR,C_PTR},C_INT),
    xFileTimeToSystemTime =
define_c_func(kernel32,"FileTimeToSystemTime",{C_PTR,C_PTR},C_INT),

    xFindData = allocate(FDsize),
    xLocalFileTime = allocate(FTsize),
    xSystemTime = allocate(STsize),

    attrbits={#01,#02,#04,#00,#10,#20}, -- no volume_id
    attrchar={'r','h','s','v','d','a'}

function peek_string(atom addr)
atom last
    last = addr
    while peek(last) do
        last += 1
    end while
    if addr != last then
        return peek( {addr, last - addr} )
    else
        return ""
    end if
end function

function peek2u(atom addr)
    return peek(addr) + peek(addr+1)*256
end function

function ConvertAttributes(integer c)
--
-- Convert the bitmap of file attributes into a text string
--
sequence res
    res=""
    for i=1 to length(attrbits) do
        if and_bits(c,attrbits[i]) then
            res&=attrchar[i]
        end if
    end for
    return res
end function

global function apidir(sequence path)
integer d
atom lpPath, h
sequence res
    while 1 do
        --
        -- Force consistent use of '\\' vs '/'.
        --
        d=find('/',path)
        if d=0 then exit end if
        path[d]='\\'
    end while
    if length(path) and path[length(path)]='\\' then
        path&="*.*"
    elsif not find('*',path) and not find('?',path) then
        --
        -- Check if the passed path is a directory
        --
        lpPath = allocate_string(path)
        h = c_func(xFindFirstFile,{lpPath,xFindData})
        if h != INVALID_HANDLE_VALUE then
            if and_bits(peek(xFindData+FDwFileAttributes),#10) then
                path &= "\\*.*"
            end if
            c_proc(xFindClose,{h})
        end if
        free(lpPath)
    end if
    lpPath = allocate_string(path)
    h = c_func(xFindFirstFile,{lpPath,xFindData})
    free(lpPath)
    if h = INVALID_HANDLE_VALUE then return -1 end if
    res={}
    while 1 do
        if
c_func(xFileTimeToLocalFileTime,{xFindData+FDtLastWriteTime,xLocalFileTime})
then end if
        if c_func(xFileTimeToSystemTime,{xLocalFileTime,xSystemTime})
then end if
        res=append(res,{peek_string(xFindData+FDcFileName),

ConvertAttributes(peek4u(xFindData+FDwFileAttributes)),

peek4s(xFindData+FDnFileSizeHigh)*#100000000+peek4u(xFindData+FDnFileSizeLow),
                        peek2u(xSystemTime+STwYear),
                        peek2u(xSystemTime+STwMonth),
                        peek2u(xSystemTime+STwDay),
                        peek2u(xSystemTime+STwHour),
                        peek2u(xSystemTime+STwMinute),
                        peek2u(xSystemTime+STwSecond)})
        if not c_func(xFindNextFile,{h,xFindData}) then exit end if
    end while
    c_proc(xFindClose,{h})
    return res      
end function

include ppp.e	-- or use pretty_print().
include file.e
pp(dir("C:\\*.bak"))
pp(apidir("C:\\*.bak"))
if getc(0) then end if


Regards,
Pete

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu