【问题标题】:VBScript Object Required error when renaming files重命名文件时需要 VBScript 对象错误
【发布时间】:2019-10-11 17:42:44
【问题描述】:

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

脚本一直有效,直到我进入 For 循环。我得到一个

需要对象:'C:\users\jspfu\sourc' 第 23 行错误,代码 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

感谢您的帮助。

【问题讨论】:

    标签: vbscript


    【解决方案1】:

    你的代码有几个问题:

    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
    

    【讨论】:

    • 谢谢@Etienne Laneville!它有效,而且太棒了!再次感谢您。
    • 很高兴它有效。如果有机会,请将问题标记为已回答!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-07
    相关资源
    最近更新 更多