OptionExplicit 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'只有在 FOF_SIMPLEPROGRESS 时用 End Type
Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA"(lpFileOp As SHFILEOPSTRUCT) As Long
'如果存在就算了,不存在就创建 '验证方式,无目录,隐藏目录,系统目录,只读目录... PublicSub 创建(路径 As String) If Dir(路径, vbDirectory)=""Then MkDir 路径 EndIf EndSub
'移动文件或者目录 PublicSub ApiMove(P1 As String, P2 As String) Dim SHFileOp As SHFILEOPSTRUCT ' 移动 SHFileOp.wFunc = FO_MOVE SHFileOp.pFrom = P1 +Chr(0) SHFileOp.pTo = P2 SHFileOp.fFlags = FOF_ALLOWUNDO + FOF_NOCONFIRMATION Call SHFileOperation(SHFileOp) EndSub
'复制文件或者目录 PublicSub ApiCopy(P1 As String, P2 As String, Optional Fok As Boolean =False) Dim SHFileOp As SHFILEOPSTRUCT ' 拷贝 SHFileOp.wFunc = FO_COPY SHFileOp.pFrom = P1 SHFileOp.pTo = P2 If Fok =TrueThen SHFileOp.fFlags = FOF_ALLOWUNDO + FOF_NOCONFIRMMKDIR + FOF_NOCONFIRMATION Else SHFileOp.fFlags = FOF_ALLOWUNDO + FOF_NOCONFIRMMKDIR EndIf Call SHFileOperation(SHFileOp) EndSub
'返回子目录列表 PublicFunction Dirs(Path As String) As String() OnError GoTo E100 Dim S() As String, I As Long Dim MyName As String MyName = Dir(Path, vbDirectory Or vbHidden Or vbSystem Or vbReadOnly)' 找寻第一项。 ReDim S(250) S(0)= MyName DoWhile MyName <>""' 开始循环。 If MyName <>"."And MyName <>".."Then
If(GetAttr(Path & MyName)And vbDirectory)= vbDirectory Then S(I)= MyName I = I + 1 If I >UBound(S)Then ReDim Preserve S(I + 250) EndIf EndIf
EndIf E100: MyName = Dir Loop
ReDim Preserve S(I - 1) Dirs = S EndFunction
'返回子目录列表 PublicFunction Files(Path As String) As String() OnError GoTo E100 Dim S() As String, I As Long Dim MyName As String MyName = Dir(Path, vbHidden Or vbSystem Or vbReadOnly)' 找寻第一项。 ReDim S(250) S(0)= MyName DoWhile MyName <>""' 开始循环。 If MyName <>"."And MyName <>".."Then
If(GetAttr(Path & MyName)And vbDirectory)<> vbDirectory Then S(I)= MyName I = I + 1 If I >UBound(S)Then ReDim Preserve S(I + 250) EndIf EndIf
EndIf E100: MyName = Dir Loop
ReDim Preserve S(I - 1) Files = S EndFunction
'判断文件或者文件夹是否存在 PublicFunction 存在(Path As String) As Boolean IfRight(Path, 1)="\"Then Path =Left(Path,Len(Path)- 1) 存在 =False IfLen(Trim(Path))< 1 ThenExitFunction '判断是否存在 If Dir(Path, vbDirectory Or vbHidden Or vbSystem Or vbNormal Or vbReadOnly)<>""Then '判断是否文件夹 If(GetAttr(Path)And vbDirectory)= vbDirectory Then 存在 =True'是文件夹 EndIf EndIf EndFunction
'返回目录名称 PublicFunction DirName(Path As String) As String