Powerbuilder可以计算文件夹中的文件数量吗?

时间:2014-01-16 07:12:04

标签: file count numbers directory powerbuilder

是否有内置函数或其他方法来计算文件夹中的文件数量甚至获取所有文件的名称?

Powerbuilder 11.5

1 个答案:

答案 0 :(得分:2)

是的,有一个内置功能(我个人觉得很糟糕;):您可以使用ListBox方法填充DirList()。您可以指定掩码(例如*.txt)和文件类型(隐藏,系统,子目录,驱动器......)。

ListBox将包含不同的匹配名称。如果您只需要列表进行治疗而不需要显示,则可以将其隐藏在当前窗口中...

真正的非可视替代方法是包装FindFirstFile() / FindNextFile()本机Windows API函数和相关结构。

编辑 - vanilla pbscript目录列表:

以下是我用来获取目录列表的一些代码。它对位操作有一些依赖性,这些依赖于我的代码中的一些本地PBNI扩展,这些扩展很难包含在这里,因此我从PFC提供了一些仅限pbscript(次优)的函数来获得工作代码。

这是一个电话示例:

string lf[]
getfiles("c:\temp", "*.txt", ref lf[])
messagebox("getfiles", "found "+ string(upperbound(lf[]))+ " files")

<强> of_getbit.srf

global type of_getbit from function_object
end type

forward prototypes
global function boolean of_getbit (long al_decimal, unsignedinteger ai_bit)
end prototypes

global function boolean of_getbit (long al_decimal, unsignedinteger ai_bit);//////////////////////////////////////////////////////////////////////////////
//
//  Function:       of_GetBit
//
//  Access:             public
//
//  Arguments:
//  al_decimal      Decimal value whose on/off value needs to be determined (e.g. 47).
//  ai_bit          Position bit from right to left on the Decimal value.
//
//  Returns:        boolean
//                      True if the value is On.
//                      False if the value is Off.
//                      If any argument's value is NULL, function returns NULL.
//
//  Description:   Determines if the nth binary bit of a decimal number is 
//                      1 or 0.
//
//////////////////////////////////////////////////////////////////////////////
//
//  Revision History
//
//  Version
//  5.0   Initial version
// 5.0.03   Fixed problem when dealing with large numbers (>32k)
//              from "mod int" to "int mod"
//
//////////////////////////////////////////////////////////////////////////////
//
/*
 * Open Source PowerBuilder Foundation Class Libraries
 *
 * Copyright (c) 2004-2005, All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted in accordance with the GNU Lesser General
 * Public License Version 2.1, February 1999
 *
 * http://www.gnu.org/copyleft/lesser.html
 *
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals and was originally based on software copyright (c) 
 * 1996-2004 Sybase, Inc. http://www.sybase.com.  For more
 * information on the Open Source PowerBuilder Foundation Class
 * Libraries see http://pfc.codexchange.sybase.com
*/
//
//////////////////////////////////////////////////////////////////////////////

Boolean lb_null

//Check parameters
If IsNull(al_decimal) or IsNull(ai_bit) then
    SetNull(lb_null)
    Return lb_null
End If

//Assumption ai_bit is the nth bit counting right to left with
//the leftmost bit being bit one.
//al_decimal is a binary number as a base 10 long.
If Int(Mod(al_decimal / (2 ^(ai_bit - 1)), 2)) > 0 Then
    Return True
End If

Return False

end function

<强> of_bitwiseand.srf

global type of_bitwiseand from function_object
end type

forward prototypes
global function long of_bitwiseand (long al_value1, long al_value2)
end prototypes

global function long of_bitwiseand (long al_value1, long al_value2);//////////////////////////////////////////////////////////////////////////////
//
//  Function:       of_BitwiseAnd
//
//  Access:             public
//
//  Arguments:
//  al_Value1       The first value to be used in the operation. (e.g. 55)
//  al_Value2       The second value to be used in the operation. (e.g. 44)
//
//  Returns:        Long
//                      The result of the AND operation (e.g. 36)
//                      If either argument's value is NULL, function returns NULL.
//
//  Description:   Performs a bitwise AND operation (al_Value1 && al_Value2),
//                      which ANDs each bit of the values.
//                      (55 && 44) = 36
//
//////////////////////////////////////////////////////////////////////////////
//
//  Revision History
//
//  Version
//  5.0   Initial version
//
//////////////////////////////////////////////////////////////////////////////
//
/*
 * Open Source PowerBuilder Foundation Class Libraries
 *
 * Copyright (c) 2004-2005, All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted in accordance with the GNU Lesser General
 * Public License Version 2.1, February 1999
 *
 * http://www.gnu.org/copyleft/lesser.html
 *
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals and was originally based on software copyright (c) 
 * 1996-2004 Sybase, Inc. http://www.sybase.com.  For more
 * information on the Open Source PowerBuilder Foundation Class
 * Libraries see http://pfc.codexchange.sybase.com
*/
//
//////////////////////////////////////////////////////////////////////////////

Integer     li_Cnt
Long            ll_Result
Boolean     lb_Value1[32], lb_Value2[32]

// Check for nulls
If IsNull(al_Value1) Or IsNull(al_Value2) Then
    SetNull(ll_Result)
    Return ll_Result
End If

// Get all bits for both values
For li_Cnt = 1 To 32
    lb_Value1[li_Cnt] = of_getbit(al_Value1, li_Cnt)
    lb_Value2[li_Cnt] = of_getbit(al_Value2, li_Cnt)
Next

// And them together
For li_Cnt = 1 To 32
    If lb_Value1[li_Cnt] And lb_Value2[li_Cnt] Then
        ll_Result = ll_Result + (2^(li_Cnt - 1))
    End If
Next

Return ll_Result

end function

<强> getfiles.srf

type str_filetime from structure
    ulong ul_LowDateTime
    ulong ul_HighDateTime
end type

type str_win32_find_data from structure
    unsignedlong    fileattributes
    str_filetime    creationfile
    str_filetime    lastaccesstime
    str_filetime    lastwritetime
    unsignedlong    filesizehigh
    unsignedlong    filesizelow
    unsignedlong    reserved0
    unsignedlong    reserved1
    character   filename[260]
    character   altfilename[14]
//  character   filename[520]
//  character   altfilename[28]
end type

global type getfiles from function_object
end type

type prototypes
Function ulong FindFirstFile(ref string lpszSearchFile, ref STR_WIN32_FIND_DATA lpffd)  library "kernel32.dll" alias for "FindFirstFileW"
Function long FindNextFile(ulong hfindfile, ref STR_WIN32_FIND_DATA lpffd)  library "kernel32.dll" alias for "FindNextFileW"
Function boolean FindClose(ulong hfindfile) library "kernel32.dll"
end prototypes

forward prototypes
global function long getfiles (string as_path, string as_dos_mask, ref string as_files[])
end prototypes

global function long getfiles (string as_path, string as_dos_mask, ref string as_files[]);/* Wrapper for the Win32 API FindFirst / FindNext / FindClose

as_path         : the directory to search in
as_dos_mask : a generic file joker needed by FindFirst (give '*.*' or empty string for all files)
as_files[] : folder name Array ( output parameter )

returns : the array as_files[] size
*/

ulong lul_handle
str_win32_find_data str_find
string ls_path_and_mask, ls_empty[]
boolean lb_use_rx = false, lb_process = false
boolean lb_isdir
long ll_ret, ll_count = 0

constant long MAX_PATH = 260
constant long ALT_NAME_SIZE = 14
constant ulong INVALID_HANDLE_VALUE = 4294967295 

constant long FILE_ATTRIBUTE_READONLY = 1               //0x0001
constant long FILE_ATTRIBUTE_HIDDEN = 2                 //0x0002
constant long FILE_ATTRIBUTE_SYSTEM = 4                 //0x0004
constant long FILE_ATTRIBUTE_DIRECTORY = 16             //0x0010
constant long FILE_ATTRIBUTE_ARCHIVE = 32               //0x0020
constant long FILE_ATTRIBUTE_DEVICE = 64                //0x0040 - reserved
constant long FILE_ATTRIBUTE_NORMAL = 128               //0x0080
constant long FILE_ATTRIBUTE_TEMPORARY = 256            //0x0100
constant long FILE_ATTRIBUTE_SPARSE_FILE = 512          //0x0200
constant long FILE_ATTRIBUTE_REPARSE_POINT = 1024       //0x0400
constant long FILE_ATTRIBUTE_COMPRESSED = 2048          //0x0800
constant long FILE_ATTRIBUTE_OFFLINE = 4096             //0x1000
constant long FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 8192 //0x2000
constant long FILE_ATTRIBUTE_ENCRYPTED = 16384          //0x4000
constant long FILE_ATTRIBUTE_VIRTUAL = 65536            //0x10000 reserved

as_files[] = ls_empty[]

str_find.filename=space(MAX_PATH)
str_find.altfilename=space(ALT_NAME_SIZE)
if as_dos_mask = "" then as_dos_mask = "*.*"
if right(as_path, 1) <> "\" then as_path += "\"
ls_path_and_mask = as_path + as_dos_mask
lul_handle = FindFirstFile(ls_path_and_mask, str_find)
if lul_handle <> INVALID_HANDLE_VALUE then
    do 
        if of_bitwiseand(str_find.fileattributes, FILE_ATTRIBUTE_DIRECTORY) > 0 then goto label_continue
        as_files[upperbound(as_files[])+1] = str_find.filename
label_continue:
        //remark : the FindNextFile find the next file in the directory order
        // the alphabetical sort is not guaranteed
        ll_ret = FindNextFile(lul_handle, str_find)
    loop while ll_ret > 0
    Findclose(lul_handle)
end if
return upperbound(as_files[])

end function