重命名文件时 VBScript Object Required 错误

VBScript Object Required error when renaming files

我正在尝试编写一个重命名多个 Excel 文件的脚本。如果文件名中有 "TEMP",我想将 "TEMP" 更改为输入框中的日期。

在我到达 For 循环之前,脚本一直有效。我得到一个

Object Required :'C:\users\jspfu\sourc' error on line 23, Code 800A01A8

错误。我真的是 VBScript 的新手,我基本上是从 YouTube 视频中复制这段代码,无论如何都是 For-loop 部分。

到目前为止,这是我的脚本:

Option Explicit
Dim FSO,FileLoc, UsrDate, Msg, File, Oldname, NewFileName, FoldName

Set FSO = CreateObject("Scripting.FileSystemObject")
FileLoc = "C:\Users\jspfu\source\XL_Files"
UsrDate= Inputbox("Enter the Date", "Date")  
FoldName = FSO.GetFolder(FileLoc)

If IsNumeric(UsrDate) and UsrDate<>"" then
Msg = Msgbox ("is this " & UsrDate & " Correct",VBYesNo, "Verify Date")
end if  

Do While Msg <> 6
  If Msg = 7 Then
    UsrDate= Inputbox("Enter the Date", "Date")
        Msg = Msgbox ("is this " & UsrDate & " Correct",VBYesNo, "Verify Date")

    ElseIf Not IsNumeric(UsrDate) or UsrDate= "" then
        UsrDate= Inputbox("Enter the Date", "Date")

End If  
Loop

For each File in FoldName.files
    Oldname = FileLOC & File.name
    NewFileName = replace(Fname, "TEMP", Date)
    FSO.MoveFile Fname, NewFileName
Next

感谢您的帮助。

您的代码中存在一些问题:

  1. 使用Set获取FoldName的对象引用:Set FoldName = FSO.GetFolder(FileLoc)

  2. 使用 File.Name 而不是 MoveFile 直接重命名文件

你的循环应该是这样的:

For Each File In FoldName.Files
    NewFileName = Replace(File.Name, "TEMP", UsrDate)
    If NewFileName <> File.Name Then
        File.Name = NewFileName
    End If
Next