用fso怎样实现批理更改文件名?

icehell 2004-07-18 01:05:44
比如一个文件夹pic下有若干文件,如下:
test_xyz_com_1.gif
test_xyz_com_2.gif
test_xyz_com_3.gif

能否用fso批量实现文件名的更改,使其改成:
www_xyz_com_1.gif
www_xyz_com_2.gif
www_xyz_com_3.gif

----------------------
当然,前提是不去服务器端手工操作,但不知道vbs能否调用shell来实现?
...全文
178 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
cuixiping 2004-07-18
  • 打赏
  • 举报
回复
如Jaron所说,只有遍历所有文件,循环改名.
你需要的关键是根据原文件名得出新文件名,用正则去替换吧.写个函数


pfc001 2004-07-18
  • 打赏
  • 举报
回复
vbs能调用shell来实现
sunjiujiu 2004-07-18
  • 打赏
  • 举报
回复

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
Jaron 2004-07-18
  • 打赏
  • 举报
回复
遍历所有文件,循环改名
jervis82 2004-07-18
  • 打赏
  • 举报
回复
这个我正想知道。

28,391

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧