VBGood网站全文搜索 Google

搜索VBGood全站网页(全文搜索)
首页 - 经验之谈 - 如何用程序来Delete Copy Move Rename File/Directory
发表评论(0)作者:不详, 平台:VB6.0+Win98, 阅读:12043, 日期:2001-09-16
如何用程序来Delete Copy Move Rename File/Directory


作者: 王国荣 

Private Type SHFILEOPSTRUCT
        hwnd As Long
        wFunc As Long
        pFrom As String
        pTo As String
        fFlags As Integer
        fAnyOperationsAborted As Long
        hNameMappings As Long
        lpszProgressTitle As String 注释:  only used if FOF_SIMPLEPROGRESS
End Type
注释:wFunc 的设定值
注释:FO_COPY     Copies the files specified by pFrom to the location specified by pTo.
注释:FO_DELETE   Deletes the files specified by pFrom (pTo is ignored).
注释:FO_MOVE     Moves the files specified by pFrom to the location specified by pTo.
注释:FO_RENAME   Renames the files specified by pFrom.

注释:fFlag的设定
注释:FOF_ALLOWUNDO           Preserves undo information, if possible.
注释:FOF_FILESONLY           Performs the operation only on files if a wildcard filename
注释:                        (*.*) is specified.
注释:FOF_MULTIDESTFILES      Indicates that the pTo member specifies multiple destination
注释:                        files (one for each source file) rather than one directory where
注释:                        all source files are to be deposited.
注释:FOF_NOCONFIRMATION      Responds with "yes to all" for any dialog box that is displayed.
注释:FOF_NOCONFIRMMKDIR      Does not confirm the creation of a new directory if
注释:                        the operation requires one to be created.
注释:FOF_RENAMEONCOLLISION   Gives the file being operated on a new name (such as
注释:                        "Copy #1 of...") in a move, copy, or rename operation
注释:                        if a file of the target name already exists.
注释:FOF_SILENT              Does not display a progress dialog box.
注释:FOF_SIMPLEPROGRESS      Displays a progress dialog box, but does not show the
注释:                        filenames.
注释:FOF_WANTMAPPINGHANDLE   Fills in the hNameMappings member. The handle must be
注释:                        freed by using the SHFreeNameMappings function.

Const FO_MOVE = &H1
Const FO_COPY = &H2
Const FO_DELETE = &H3
Const FOF_NOCONFIRMATION = &H10
Const FOF_NOCONFIRMMKDIR = &H200
Const FOF_ALLOWUNDO = &H40
Const FOF_SILENT = &H4


Private Declare Function SHFileOperation Lib "shell32.dll" Alias _
                "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long

注释:删除 test目录及其底下的子目录到资源回收桶
Private Sub Command1_Click()
    Dim SHFileOp As SHFILEOPSTRUCT

    SHFileOp.wFunc = FO_DELETE
    SHFileOp.pFrom = "c:\test" + Chr(0)
    注释:不出现档案删除的动态AVI,且不Confirm
    SHFileOp.fFlags = FOF_SILENT + FOF_ALLOWUNDO + FOF_NOCONFIRMATION
    注释:若没有 FOF_ALLOWUNDO 则不会到资源回收桶
    Call SHFileOperation(SHFileOp)
End Sub

注释:同时删除多档到资源回收桶
Private Sub Command2_Click()
    Dim SHFileOp As SHFILEOPSTRUCT
    Dim Files As String
    注释:Files = "c:\test.txt" + Chr(0)
    Files = "c:\test1.txt" + Chr(0) + "c:\test2.txt" + Chr(0) + _
            "c:\test3.txt" + Chr(0)
    SHFileOp.wFunc = FO_DELETE
    SHFileOp.pFrom = Files
    注释:删至资源回收桶,且不Confirm
    SHFileOp.fFlags = FOF_ALLOWUNDO + FOF_NOCONFIRMATION
    Call SHFileOperation(SHFileOp)
End Sub

注释:将 c:\temp 整个目录复制到 c:\temp2
Private Sub Command3_Click()
    Dim SHFileOp As SHFILEOPSTRUCT

    SHFileOp.wFunc = FO_COPY
    SHFileOp.pFrom = "c:\temp\*.*"
    SHFileOp.pTo = "c:\temp2\*.*"
    SHFileOp.fFlags = FOF_ALLOWUNDO + FOF_NOCONFIRMMKDIR
    Call SHFileOperation(SHFileOp)
End Sub

注释:将 c:\test4.txt 快速移到 c:\temp 目录
Private Sub Command4_Click()
    Dim SHFileOp As SHFILEOPSTRUCT

    SHFileOp.wFunc = FO_MOVE
    SHFileOp.pFrom = "c:\test4.txt" + Chr(0)
    SHFileOp.pTo = "c:\temp"
    SHFileOp.fFlags = FOF_ALLOWUNDO + FOF_NOCONFIRMATION
    Call SHFileOperation(SHFileOp)
End Sub