我已经为vbs脚本添加了一个确认对话框(由第三方提供),但我不需要单击“确定”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
答案 0 :(得分:1)
RenameItem()
是sub(routine),即可以重复使用的代码单元。每次调用sub时,它都会执行其中的所有内容,包括MsgBox
。
将此行移到sub:
之外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