Results 1 to 3 of 3

Thread: SHFileOperation

  1. #1

    Thread Starter
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    780

    SHFileOperation

    I was reading up on SHFileOperation tonight I never realy used it, before just seen it in posts so I decided to read about it pretty much you can do some cool things So to same my self repeated code I made a little mod and decided to share it with you. there not really an example just the mod code but it pretty easy to work out. I am thinking of makeing a small app to maybe backup my VB folder at midnight that what got me reseaching it. anyway hope you find it of some use.

    modFileOp.bas

    Code:
    Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (ByRef lpFileOp As SHFILEOPSTRUCT) As Long
    Private Declare Function GetDesktopWindow Lib "user32.dll" () As Long
    
    Private Const FO_COPY As Long = &H2
    Private Const FO_DELETE As Long = &H3
    Private Const FO_MOVE As Long = &H1
    Private Const FO_RENAME As Long = &H4
    
    Enum TFileFlags
        FOF_ALLOWUNDO = &H40
        FOF_FILESONLY = &H80
        FOF_MULTIDESTFILES = &H1
        FOF_NOCONFIRMATION = &H10
        FOF_NOCONFIRMMKDIR = &H200
        FOF_SIMPLEPROGRESS = &H100
        FOF_SILENT = &H4
    End Enum
    
    Private Type SHFILEOPSTRUCT
        hWnd As Long
        wFunc As Long
        pFrom As String
        pTo As String
        fFlags As Integer
        fAborted As Long
        hNameMaps As Long
        sProgress As String
    End Type
    
    Public Function SH_Copy(src As String, dest As String, cFlags As TFileFlags) As Long
    Dim shFileOp As SHFILEOPSTRUCT
        'Copy files or folders
        shFileOp.hWnd = GetDesktopWindow
        shFileOp.wFunc = FO_COPY
        shFileOp.pFrom = src
        shFileOp.pTo = dest
        shFileOp.fFlags = cFlags
        
        SH_Copy = SHFileOperation(shFileOp)
        
    End Function
    
    Public Function SH_Move(src As String, dest As String, cFlags As TFileFlags) As Long
    Dim shFileOp As SHFILEOPSTRUCT
        'Move files or folders
        shFileOp.hWnd = GetDesktopWindow
        shFileOp.wFunc = FO_MOVE
        shFileOp.pFrom = src
        shFileOp.pTo = dest
        shFileOp.fFlags = cFlags
        
        SH_Move = SHFileOperation(shFileOp)
        
    End Function
    
    Public Function SH_Rename(src As String, dest As String, cFlags As TFileFlags) As Long
    Dim shFileOp As SHFILEOPSTRUCT
        'Rename files or folders
        shFileOp.hWnd = GetDesktopWindow
        shFileOp.wFunc = FO_RENAME
        shFileOp.pFrom = src
        shFileOp.pTo = dest
        shFileOp.fFlags = cFlags
        
        SH_Rename = SHFileOperation(shFileOp)
        
    End Function
    
    Public Function SH_Delete(src As String, cFlags As TFileFlags) As Long
    Dim shFileOp As SHFILEOPSTRUCT
        'Delete files or folders
        shFileOp.hWnd = GetDesktopWindow
        shFileOp.wFunc = FO_DELETE
        shFileOp.pFrom = src
        shFileOp.fFlags = cFlags
        
        SH_Delete = SHFileOperation(shFileOp)
        
    End Function
    Last edited by BenJones; Nov 14th, 2024 at 02:04 AM.

  2. #2
    Lively Member
    Join Date
    Aug 2020
    Location
    Victoria Texas 77904
    Posts
    104

    Re: SHFileOperation

    Ben ,Just used this in a small project I was working on. Works great ,only bug I found in the above code is the underscore is missing on the copy and move functions. Keep up the good work.

  3. #3

    Thread Starter
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    780

    Re: SHFileOperation

    Thanks KFrosty just updated the code.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width