PB利用API-SHFileOperation 文件操作函数 实现多文件及文件夹的删除复制等操作

        PB的文件操作能力比较弱,自带的一些函数功能太简单,一遇上些稍微复杂些的需求(如多文件删除,文件夹的删除等)并不是非常好用,所以利用windows的API来实现相应的功能就成了最好的选择,而这其中,SHFileOperation又是最好用的,其实我说的这些并不新鲜,网上也有相关的例子,但是网上现在能找到的例程都是PB10以前的,由于从PB10开始,编码从ANSI改成了UNICODE,所以在参数的传递上,以前的一些东西要稍作下改变才可以继续使用,见到有些朋友对此有疑问,特封装了一对象(PB10以后适用),免得大家重复劳动,浪费时间,而曾经尝试过类似的工作但没有成功的朋友,请注意代码中BLOB,BLOBEDIT,LENA的使用方法,希望对大家理解ansi和unicode之间的转换能有些帮助。



*******************nvo_fileoperation,用户对象,封装了文件操作的相关函数**************************************

forward

global type nvo_fileoperation from nonvisualobject
end type
type str_operation from structure within nvo_fileoperation
end type
type str_fileoperation from structure within nvo_fileoperation
end type
end forward

type str_fileoperation from structure
        unsignedlong            hwnd
        unsignedlong            wfunc
        blob            pfrom
        blob            pto
        unsignedinteger         fflags
        unsignedlong            banyoperationsaborted
        unsignedlong            hnamemappings
        string          lpszprogresstitle
end type

global type nvo_fileoperation from nonvisualobject
end type
global nvo_fileoperation nvo_fileoperation

type prototypes
//文件操作,SHFileOperation在 Vista 及以后版本中应替换为iFileOperation
Function long  SHFileOperation(ref str_fileoperation lpFileOp)  Library  "shell32.dll"  Alias FOR "SHFileOperationA;ansi"  
end prototypes

type variables
Private  Constant int FO_MOVE  = 1 // &H1
Private  Constant int FO_COPY  = 2 //&H2
Private  Constant int FO_DELETE  =3  //&H3
Private  Constant int FO_RENAME  =4  //&H4
Private  Constant int FOF_NOCONFIRMATION  =16  //&H10
Private  Constant int FOF_SILENT  = 4 //&H4
Private  Constant int FOF_NOERRORUI  = 1024 //&H400  
end variables

forward prototypes
public function integer of_delete (string as_sourcefile[], window aw_requestor, long aui_flags)
public function integer of_copy (string as_sourcefile[], string as_target, window aw_requestor, long aui_flags)
public function integer of_move (string as_sourcefile[], string as_target, window aw_requestor, long aui_flags)
public function integer of_rename (string as_sourcefile, string as_targetfile, window aw_requestor, long aui_flags)
end prototypes

public function integer of_delete (string as_sourcefile[], window aw_requestor, long aui_flags);//====================================================================
// 函数: nvo_fileoperation::of_delete
//--------------------------------------------------------------------
// 描述:
//--------------------------------------------------------------------
// 参数:
//     string    as_sourcefile[]    --需要删除的文件名
//     window    aw_requestor --当前窗口名          
//     long    aui_flags --UI参数(是否显示,是否与用户互动等,意义参见百度SHFileOperation结构参数说明)             
//--------------------------------------------------------------------
// 返回值:  integer
//--------------------------------------------------------------------
// 作者:    sjlion        日期: 2012.01.11
//--------------------------------------------------------------------
// 修改历史:
//    
//====================================================================

str_fileoperation lstr_FileOp
Integer li_rc,li_bound,i
blob lb_source,lb_temp

lstr_FileOp.hWnd                  = Handle(aw_requestor)
lstr_FileOp.wFunc                 = FO_DELETE

li_bound = upperbound(as_sourcefile[])

for i = 1 to li_bound
        lb_temp = blob(as_sourcefile[i]+space(1),EncodingANSI!)
        blobedit(lb_temp,Lena( as_SourceFile[i] ) + 1, Char(0),EncodingANSI!)
        lb_source = lb_source + lb_temp
next

lb_source = lb_source+blob(char(0),EncodingANSI!)

lstr_FileOp.pFrom                 =lb_source

//blob(as_sourcefile,EncodingANSI!)   +   blob(char(0),EncodingANSI!)   +   blob(char(0),EncodingANSI!)//

//lstr_FileOp.pto                 =  Blob( as_SourceFile + Space(2) )
//BlobEdit( lstr_FileOp.pto, Lena( as_SourceFile ) + 1, Char(0) )
//BlobEdit( lstr_FileOp.pto, Lena( as_SourceFile ) + 2, Char(0) )

lstr_FileOp.fFlags                = aui_flags //16 无提示 32 需确认
lstr_FileOp.hNameMappings         = 0
lstr_FileOp.lpszProgressTitle     = Space(10)

li_rc = SHFileOperation( lstr_FileOp )
IF li_rc <> 0 THEN
    IF NOT IsNull( lstr_FileOp ) THEN
        IF lstr_FileOp.bAnyOperationsAborted = 1 THEN
            RETURN 0
        END IF
    END IF
ELSE
    RETURN    -1
END IF

RETURN 1

end function

public function integer of_copy (string as_sourcefile[], string as_target, window aw_requestor, long aui_flags);//====================================================================
// 函数: nvo_fileoperation::of_copy
//--------------------------------------------------------------------
// 描述:
//--------------------------------------------------------------------
// 参数:
//     string    as_sourcefile[]    --需要复制的文件名
//     window    aw_requestor --当前窗口名          
//     long    aui_flags --UI参数(是否显示,是否与用户互动等,意义参见百度SHFileOperation结构参数说明)             
//--------------------------------------------------------------------
// 返回值:  integer
//--------------------------------------------------------------------
// 作者:    sjlion        日期: 2012.01.11
//--------------------------------------------------------------------
// 修改历史:
//    
//====================================================================

str_fileoperation lstr_FileOp
Integer li_rc,li_bound,i
blob lb_source,lb_temp,lb_target

lstr_FileOp.hWnd                  = Handle(aw_requestor)
lstr_FileOp.wFunc                 = FO_COPY

li_bound = upperbound(as_sourcefile[])

for i = 1 to li_bound
        lb_temp = blob(as_sourcefile[i]+space(1),EncodingANSI!)
        blobedit(lb_temp,Lena( as_SourceFile[i] ) + 1, Char(0),EncodingANSI!)
        lb_source = lb_source + lb_temp
next

lb_source = lb_source+blob(char(0),EncodingANSI!)

lstr_FileOp.pFrom =lb_source

lstr_FileOp.pto  =  Blob( as_target + Space(2),EncodingANSI!)
BlobEdit( lstr_FileOp.pto, Lena( as_target ) + 1, Char(0),EncodingANSI!)
BlobEdit( lstr_FileOp.pto, Lena( as_target) + 2, Char(0),EncodingANSI!)

lstr_FileOp.fFlags                = aui_flags //16 无提示 32 需确认
lstr_FileOp.hNameMappings         = 0
lstr_FileOp.lpszProgressTitle     = Space(10)

li_rc = SHFileOperation( lstr_FileOp )
IF li_rc <> 0 THEN
    IF NOT IsNull( lstr_FileOp ) THEN
        IF lstr_FileOp.bAnyOperationsAborted = 1 THEN
            RETURN 0
        END IF
    END IF
ELSE
    RETURN    -1
END IF

RETURN 1

end function

public function integer of_move (string as_sourcefile[], string as_target, window aw_requestor, long aui_flags);//====================================================================
// 函数: nvo_fileoperation::of_move
//--------------------------------------------------------------------
// 描述:
//--------------------------------------------------------------------
// 参数:
//     string    as_sourcefile[]    --需要移动的文件名
//     window    aw_requestor --当前窗口名          
//     long    aui_flags --UI参数(是否显示,是否与用户互动等,意义参见百度SHFileOperation结构参数说明)             
//--------------------------------------------------------------------
// 返回值:  integer
//--------------------------------------------------------------------
// 作者:    sjlion        日期: 2012.01.11
//--------------------------------------------------------------------
// 修改历史:
//    
//====================================================================

str_fileoperation lstr_FileOp
Integer li_rc,li_bound,i
blob lb_source,lb_temp

lstr_FileOp.hWnd                  = Handle(aw_requestor)
lstr_FileOp.wFunc                 = FO_MOVE

li_bound = upperbound(as_sourcefile[])

for i = 1 to li_bound
        lb_temp = blob(as_sourcefile[i]+space(1),EncodingANSI!)
        blobedit(lb_temp,Lena( as_SourceFile[i] ) + 1, Char(0),EncodingANSI!)
        lb_source = lb_source + lb_temp
next

lb_source = lb_source+blob(char(0),EncodingANSI!)

lstr_FileOp.pFrom                 =lb_source

lstr_FileOp.pto  =  Blob( as_target + Space(2),EncodingANSI!)
BlobEdit( lstr_FileOp.pto, Lena( as_target ) + 1, Char(0),EncodingANSI!)
BlobEdit( lstr_FileOp.pto, Lena( as_target) + 2, Char(0),EncodingANSI!)

lstr_FileOp.fFlags                = aui_flags  //16 无提示 32 需确认
lstr_FileOp.hNameMappings         = 0
lstr_FileOp.lpszProgressTitle     = Space(10)

li_rc = SHFileOperation( lstr_FileOp )
IF li_rc <> 0 THEN
    IF NOT IsNull( lstr_FileOp ) THEN
        IF lstr_FileOp.bAnyOperationsAborted = 1 THEN
            RETURN 0
        END IF
    END IF
ELSE
    RETURN    -1
END IF

RETURN 1

end function

public function integer of_rename (string as_sourcefile, string as_targetfile, window aw_requestor, long aui_flags);//====================================================================
// 函数: nvo_fileoperation::of_rename
//--------------------------------------------------------------------
// 描述:
//--------------------------------------------------------------------
// 参数:
//     string    as_sourcefile[]    --需要重新的文件名列表
//     window    aw_requestor --当前窗口名          
//     long    aui_flags --UI参数(是否显示,是否与用户互动等,意义参见百度SHFileOperation结构参数说明)             
//--------------------------------------------------------------------
// 返回值:  integer
//--------------------------------------------------------------------
// 作者:    sjlion        日期: 2012.01.11
//--------------------------------------------------------------------
// 修改历史:
//    
//====================================================================

str_fileoperation lstr_FileOp
Integer li_rc,li_bound,i
blob lb_source,lb_temp,lb_target

lstr_FileOp.hWnd                  = Handle(aw_requestor)
lstr_FileOp.wFunc                 = FO_RENAME

lstr_FileOp.pfrom  =  Blob( as_sourcefile + Space(2),EncodingANSI!)
BlobEdit( lstr_FileOp.pfrom, Lena( as_sourcefile ) + 1, Char(0),EncodingANSI!)
BlobEdit( lstr_FileOp.pfrom, Lena( as_sourcefile) + 2, Char(0),EncodingANSI!)


lstr_FileOp.pto  =  Blob( as_targetfile + Space(2),EncodingANSI!)
BlobEdit( lstr_FileOp.pto, Lena( as_targetfile ) + 1, Char(0),EncodingANSI!)
BlobEdit( lstr_FileOp.pto, Lena( as_targetfile) + 2, Char(0),EncodingANSI!)


lstr_FileOp.fFlags                = aui_flags//16 无提示 32 需确认
lstr_FileOp.hNameMappings         = 0
lstr_FileOp.lpszProgressTitle     = Space(10)

li_rc = SHFileOperation( lstr_FileOp )
IF li_rc <> 0 THEN
    IF NOT IsNull( lstr_FileOp ) THEN
        IF lstr_FileOp.bAnyOperationsAborted = 1 THEN
            RETURN 0
        END IF
    END IF
ELSE
    RETURN    -1
END IF

RETURN 1

end function

on nvo_fileoperation.create
call super::create
TriggerEvent( this, "constructor" )
end on

on nvo_fileoperation.destroy
TriggerEvent( this, "destructor" )
call super::destroy
end on

********************nvo_fileoperation,用户对象,封装了文件操作的相关函数***********************************************




/====================================================================
// 事件: fileoperation对象应用例程
//--------------------------------------------------------------------
// 描述: delete -- 可删除非空文件夹,可多文件或文件夹操作
//           copy ---  可多文件或文件夹件操作
//           move--  可多文件或文件夹操作
//           rename -- 只能单文件操作
//--------------------------------------------------------------------
// 参数:         
//--------------------------------------------------------------------
// 返回值:  long
//--------------------------------------------------------------------
// 作者:    sjlion        日期: 2012.01.11
//--------------------------------------------------------------------
// 修改历史:
//    
//====================================================================


nvo_fileoperation luo_fileoperation

luo_fileoperation = Create nvo_fileoperation
string ls_copy_from[],ls_copy_to,ls_delete_from[],ls_move_from[],ls_move_to,ls_rename_from,ls_rename_to

ls_copy_from[1] = 'c:\复制1'
ls_copy_from[2] ="c:\复制2"
ls_copy_to = 'd:\'
luo_fileoperation.of_copy(ls_copy_from,ls_copy_to,this,32)

ls_delete_from[1]='c:\复制1'
ls_delete_from[2]='c:\复制2'
luo_fileoperation.of_delete(ls_delete_from,this,32)

ls_move_from[1]='d:\复制1'
ls_move_from[2]='d:\复制2'
ls_move_to = 'f:\'
luo_fileoperation.of_move(ls_move_from,ls_move_to,this,32)

ls_rename_from = 'f:\复制1'
ls_rename_to = 'f:\复制_复制'
luo_fileoperation.of_rename(ls_rename_from,ls_rename_to,this,32)







已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页