Public Function fsoFindPathFileNum(ByVal strPath As String, ByVal strExtend As String) As Long
'定义出错信息变量
Dim strErrorMsg As String
strErrorMsg = Trim(strErrorMsg)
Error = ""
'定义返回值
Dim lngResult As Long
Dim strPathFile As String
Dim strFindFile As String
strErrorMsg = IIf(Len(Trim(strPath)) = 0, "指定文件夹字符串不能为空!", "")
If Len(Trim(strErrorMsg)) = 0 Then strErrorMsg = IIf(Len(Trim(strExtend)) = 0, "指定文件扩展名字符串不能为空!", "")
strPathFile = strPath + "\" + "*." + strExtend
If Len(Trim(strErrorMsg)) = 0 Then
lngResult = 0
strFindFile = Dir(strPathFile, vbNormal) ' 找寻第一项。
Do While strFindFile <> "" ' 开始循环
lngResult = lngResult + 1
strFindFile = Dir ' 查找下一个文件
Loop
If lngResult = 0 Then
strErrorMsg = "指定目录不存在指定类型的文件!"
Else
strErrorMsg = ""
End If
End If
If Len(Trim(strErrorMsg)) = 0 Then
fsoFindPathFileNum = lngResult
Exit Function
Else
Error = strErrorMsg
fsoFindPathFileNum = -1
Exit Function
End If
On Error GoTo ErrorHandler
ErrorHandler:
fsoFindPathFileNum = -1
Error = Err.Description
End Function
Public Function fsoFindPathFile(ByVal strPath As String, ByVal strExtend As String) As String()
'定义返回值
Dim Result() As String
Dim lngFileNum As Long
Dim lngFNum As Long
Dim strPathFile As String
Dim strFindFile As String
lngFileNum = fsoFindPathFileNum(strPath, strExtend)
ReDim Result(lngFileNum)
strPathFile = strPath + "\" + "*." + strExtend
lngFNum = 0
strFindFile = Dir(strPathFile, vbNormal) ' 找寻第一项。
Do While strFindFile <> "" ' 开始循环
Result(lngFNum) = strFindFile
strFindFile = Dir ' 查找下一个文件
lngFNum = lngFNum + 1
Loop
fsoFindPathFile = Result()
End Function
通过以上可以获得所有的文件名数组result(),然后调用
Public Function fsoRenFile(ByVal strOldFile As String, ByVal strNewFile As String) As Boolean
'定义出错信息变量
Dim strErrorMsg As String
strErrorMsg = Trim(strErrorMsg)
Error = ""
'定义临时变量
Dim strNewFileNameCheck As String
strNewFileNameCheck = strNewFile
strErrorMsg = IIf(fso.FileExists(strOldFile), "", "指定要改名的文件不存在!")
If Len(Trim(strErrorMsg)) = 0 Then strErrorMsg = IIf(Len(Trim(strNewFile)) = 0, "指定的新文件名不能为空", "")
If Len(Trim(strErrorMsg)) = 0 Then strErrorMsg = IIf(fsoCheckFileName(strNewFileNameCheck), "", "指定的新文件名出错!")
If Len(Trim(strErrorMsg)) = 0 Then
'定义临时变量
Dim strOldFileExtension As String
Dim strOldFilePath As String
Dim filOld As File
Dim strNewFileExtension As String
Dim strNewFileAbsolutePath As String
Set filOld = fso.GetFile(strOldFile)
strOldFileExtension = "." + fsoGetSourceFileExtension(strOldFile)
strNewFileExtension = fsoGetSourceFileExtension(strNewFile)
strOldFilePath = fsoGetSourcePath(strOldFile)
strNewFileAbsolutePath = strOldFilePath + "\" + strNewFile + IIf(Len(Trim(strNewFileExtension)) = 0, strOldFileExtension, "")
strNewFile = strNewFile + IIf(Len(Trim(strNewFileExtension)) = 0, strOldFileExtension, "")
strErrorMsg = IIf(fso.FileExists(strNewFileAbsolutePath), "指定的新文件名称已存在!", "")
End If
If Len(Trim(strErrorMsg)) = 0 Then
filOld.Name = strNewFile
fsoRenFile = True
Exit Function
Else
Error = strErrorMsg
fsoRenFile = False
Exit Function
End If
On Error GoTo ErrorHandler
ErrorHandler:
fsoRenFile = False
Error = Err.Description
End Function
就可以实现文件的改名了,我有fso使用的一个详细的例程,几乎包含了所有的文件操作,组合起来可以实现所有的文件操作,如果要的话留个mail