0

我已经向 vbs 脚本(由第 3 方提供)添加了一个确认对话框,但我不需要单击“确定”3 次,并且希望确认提示只显示一次......在我的(有限的)知识中我做没办法做到这一点,有人可以纠正这个代码吗?

Set fso = CreateObject("Scripting.FileSystemObject")

a = "C:\Path to file 1\File 1.txt"
b = "C:\Path to file 2\File 2.txt"

RenameItem a, a & ".bak"
RenameItem b, a
RenameItem a & ".bak", b

Sub RenameItem(oldname, newname)
  If fso.FileExists(oldname) Or fso.FolderExists(oldname) Then
If fso.FileExists(oldname) Then Set f = fso.GetFile(oldname)
If fso.FolderExists(oldname) Then Set f = fso.GetFolder(oldname)
f.Name = fso.GetFileName(newname)
  End If
Msgbox "Files were toggled successfully" & vbNewLine & vbNewLine & "(click the ""OK"" button to exit)"
End Sub
4

1 回答 1

1

RenameItem()是一个子(例程),即可以重用的代码单元。每次调用 sub 时,它都会执行其中的所有内容,包括MsgBox.

将此行移到子之外:

Msgbox "Files were toggled successfully" & vbNewLine & vbNewLine & "(click the ""OK"" button to exit)"

完整代码

Set fso = CreateObject("Scripting.FileSystemObject")

a = "C:\Path to file 1\File 1.txt"
b = "C:\Path to file 1\File 1.txt"

RenameItem a, a & ".bak"
RenameItem b, a
RenameItem a & ".bak", b

Msgbox "Files were toggled successfully" & vbNewLine & vbNewLine & "(click the ""OK"" button to exit)"

Sub RenameItem(oldname, newname)
    If fso.FileExists(oldname) Or fso.FolderExists(oldname) Then
        If fso.FileExists(oldname) Then Set f = fso.GetFile(oldname)
        If fso.FolderExists(oldname) Then Set f = fso.GetFolder(oldname)
        f.Name = fso.GetFileName(newname)
    End If
End Sub
于 2012-10-19T10:56:02.560 回答