使用VBScript更改文件名

时间:2014-03-12 17:50:14

标签: vbscript

您好我使用此代码成功使用VBS更改文件名:

Set fso = CreateObject("Scripting.FileSystemObject")

set oFldr = fso.getfolder("C:\v\t_TEST\")

for each ofile in oFldr.Files
 if lcase(fso.GetExtensionName(ofile.Name)) = "txt" then
  ofile.name = "index.txt"
  Exit for
 end if
Next

现在我想将每个.txt文件放在文件夹" v" (子文件夹)也改名。 或者要定义一个更改路径(" C:\ v \ t_XXX),所以每个文件夹以t_开头,在此路径中将更改所有以.txt结尾的文件。

我该怎么做? 谢谢!

1 个答案:

答案 0 :(得分:0)

我相信这就是你要找的......

Set fso = CreateObject("Scripting.FileSystemObject")

Set oFldr = fso.getfolder("C:\v\")
For Each fld In ofldr.SubFolders
    If Left(fld.name, 2) = "t_" Then
        Set ofls = fso.GetFolder("C:\v\" & fld.name & "\")
        For Each ofile In ofls.Files
            If LCase(fso.GetExtensionName(ofile.Name)) = "txt" Then
                ofile.name = "index.txt"
                Exit For
            End If
        Next
    End If
Next