例子: 1、建立目录,首先要这个目录是否存在,如果存在则会出错 If Dir("d:\Temp", vbDirectory) <> "Temp" Then MkDir "d:\Temp" End If2、删除目录,也要先确定这个目录是否存在,如果不存在则会出错 If Dir("d:\Temp", vbDirectory) = "Temp" Then RmDir "d:\Temp" End If
用MKDIR无法一次建立深层目录需要使用递归来处理。麻烦。但有效
Public Function MakeDir(path As String) As Boolean Dim i%, j% Dim First$, tmp$ On Error Resume Next
Private Declare Function CreateDirectory Lib "kernel32" Alias "CreateDirectoryA" (ByVal lpPathName As String, lpSecurityAttributes As SECURITY_ATTRIBUTES) As Long Private Type SECURITY_ATTRIBUTES nLength As Long lpSecurityDescriptor As Long bInheritHandle As Long End TypePrivate Sub Command1_Click() Dim Security As SECURITY_ATTRIBUTES 'Create a directory Ret& = CreateDirectory("C:\Directory", Security) 'If CreateDirectory returns 0, the function has failed If Ret& = 0 Then MsgBox "对不起,无法创建此目录!", vbCritical + vbOKOnly End Sub
创建一个新的目录或文件夹。语法MkDir path
MkDir "d:\Temp"
1、建立目录,首先要这个目录是否存在,如果存在则会出错
If Dir("d:\Temp", vbDirectory) <> "Temp" Then
MkDir "d:\Temp"
End If2、删除目录,也要先确定这个目录是否存在,如果不存在则会出错
If Dir("d:\Temp", vbDirectory) = "Temp" Then
RmDir "d:\Temp"
End If
Dim i%, j%
Dim First$, tmp$ On Error Resume Next
j = InStr(path, "\")
For i = 1 To Len(path) / 2
First$ = Mid(path, 1, j% - 1)
MkDir First$
tmp$ = Right$(path$, Len(path$) - j%)
j% = j% + InStr(tmp$, "\")
Next
If j% < Len(path$) Then MkDir path$
End Function
三星将军:你说mkdir几层目录建不了,我来试着帮你建,我好像没有看到过有限制。
Private Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End TypePrivate Sub Command1_Click()
Dim Security As SECURITY_ATTRIBUTES
'Create a directory
Ret& = CreateDirectory("C:\Directory", Security)
'If CreateDirectory returns 0, the function has failed
If Ret& = 0 Then MsgBox "对不起,无法创建此目录!", vbCritical + vbOKOnly
End Sub