2个回答
展开全部
可以用 FSO 组件。
Function GetScriptPath()
GetScriptPath = Left(WScript.ScriptFullName, Len(WScript.ScriptFullName) - Len(WScript.ScriptName))
End Function
Class MyFileOperator
Private obj
Private Sub Class_Initialize()
Set obj = CreateObject("Scripting.FileSystemObject")
End Sub
Private Sub Class_Terminate()
Set obj = Nothing
End Sub
' 枚举文件
Public Function ListFiles()
Dim path :path = GetScriptPath()
Dim objFolder :Set objFolder = obj.GetFolder(path)
Dim files()
Dim i :i = 0
For Each objFile in objFolder.Files
ReDim Preserve files(i)
files(i) = objFile.Name
i = i + 1
Next
Set objFolder = Nothing
ListFiles = files
End Function
' 文件改名
Public Sub ChangeFileName(source, dest)
Call obj.MoveFile(source, dest)
End Sub
End Class
Dim fop :Set fop = New MyFileOperator
Dim files :files = fop.ListFiles()
Dim prefix :prefix = InputBox("请输入文件名前缀:")
Dim i
For i = 0 To UBound(files)
Call fop.ChangeFileName(GetScriptPath() & files(i), GetScriptPath() & prefix & files(i))
Next
MsgBox "文件更名完毕!", MB_OK Or MB_ICONQUESTION, "提示:"
Function GetScriptPath()
GetScriptPath = Left(WScript.ScriptFullName, Len(WScript.ScriptFullName) - Len(WScript.ScriptName))
End Function
Class MyFileOperator
Private obj
Private Sub Class_Initialize()
Set obj = CreateObject("Scripting.FileSystemObject")
End Sub
Private Sub Class_Terminate()
Set obj = Nothing
End Sub
' 枚举文件
Public Function ListFiles()
Dim path :path = GetScriptPath()
Dim objFolder :Set objFolder = obj.GetFolder(path)
Dim files()
Dim i :i = 0
For Each objFile in objFolder.Files
ReDim Preserve files(i)
files(i) = objFile.Name
i = i + 1
Next
Set objFolder = Nothing
ListFiles = files
End Function
' 文件改名
Public Sub ChangeFileName(source, dest)
Call obj.MoveFile(source, dest)
End Sub
End Class
Dim fop :Set fop = New MyFileOperator
Dim files :files = fop.ListFiles()
Dim prefix :prefix = InputBox("请输入文件名前缀:")
Dim i
For i = 0 To UBound(files)
Call fop.ChangeFileName(GetScriptPath() & files(i), GetScriptPath() & prefix & files(i))
Next
MsgBox "文件更名完毕!", MB_OK Or MB_ICONQUESTION, "提示:"
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询