Vbscript 如何转换weekday命令,以便手动选择每个任务的日期,而不是自动检测它?
当我开始做这件事的时候,我并不知道我必须能够手动选择每个日常任务,并在我想的时候选择每个任务,我正试图找出如何将其转换为手动输入,这样我就不必重复整个过程,请记住我对vbscript非常陌生,所以如果有明显的解决方案,我很抱歉。我仍在本周晚些时候完成这项工作Vbscript 如何转换weekday命令,以便手动选择每个任务的日期,而不是自动检测它?,vbscript,Vbscript,当我开始做这件事的时候,我并不知道我必须能够手动选择每个日常任务,并在我想的时候选择每个任务,我正试图找出如何将其转换为手动输入,这样我就不必重复整个过程,请记住我对vbscript非常陌生,所以如果有明显的解决方案,我很抱歉。我仍在本周晚些时候完成这项工作 dtmToday = Date() dtmDayOfWeek = DatePart("w", dtmToday) 'Select case to pickup the value of day of the week
dtmToday = Date()
dtmDayOfWeek = DatePart("w", dtmToday)
'Select case to pickup the value of day of the week and call procedure
Select Case dtmDayOfWeek
Case 1
Call Sunday()
Case 2
Call Monday()
Case 3
Call Tuesday()
Case 4
Call Wednesday()
Case 5
Call Thursday()
Case 6
Call Friday()
Case 7
Call Saturday()
End Select
'Sunday procedure will execute from select case
sub Sunday()
'defining variables
dim wshShell
dim path
dim fso
'setting up the environment to run vbscript
Set fso = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")
' Execute the command and append to the text file
WShShell.run "cmd /c ping -n 10 youtube.com >> ping.txt", hidden
wscript.quit
End sub
'Monday procedure will execute from select case
sub Monday()
'defining variables
dim wshShell
dim path
dim fso
'setting up the environment to run vbscript
Set fso = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")
' to execute the command and append to the text file using >> if you want to text to be overriden use >
WShShell.run "cmd /c netstat >> netstat.txt", hidden
wscript.quit
End sub
'Tuesday procedure will execute from select case
sub Tuesday()
'defining variables
dim wshShell
dim path
dim fso
'setting up the environment to run vbscript
Set fso = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")
' to execute the command and append to the text file using >> if you want to text to be overriden use >
WShShell.run "cmd /c arp -a >> arp.txt", hidden
wscript.quit
End sub
sub Wednesday()
'defining variables
dim wshShell
dim path
dim fso
'setting up the environment to run vbscript
Set fso = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")
WShShell.run "cmd /c nbstat -n >> nbstat.txt", hidden
wscript.quit
End Sub
sub Thursday()
'defining variables
dim wshShell
dim path
dim fso
'setting up the environment to run vbscript
Set fso = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")
WShShell.run "cmd /c tracert -n 10 youtube.com >> nbstat.txt", hidden
wscript.quit
End Sub
您需要首先优化代码以避免大量重复,正如@Lankymart在他的评论中提到的那样,编写一个函数并在需要时调用它,并将所有命令存储到一个数组中,以便通过它们的索引轻松访问 因此,您的代码可以这样编写:
Option Explicit
' We define our Global variables
Dim Title,ArrCommands,strcmd,dtmDayOfWeek,IndexCommand,UserInput
Title = "Run command line based on the Day Of Week"
'----------------------------------------------------------------------------------
' We define and store our commands lines into an array
ArrCommands = Array(_
"ping -n 10 youtube.com >> ping.txt",_
"netstat >> netstat.txt",_
"arp -a >> arp.txt",_
"Color 0A & Title Running nbtstat command & nbtstat -n",_
"Color 0A & Title Running Tracert command & tracert youtube.com",_
"Color 0A & Title Running Ipconfig command & Ipconfig /all",_
"Color 0A & Title Running netstat command & netstat -ano"_
)
'-------------------------------Main Program---------------------------------------
Do While Not IsDate(UserInput)
UserInput = InputBox("Type a date here example 24/06/2020",Title,"24/06/2020")
dtmDayOfWeek = MyWeekday(UserInput)
IndexCommand = dtmDayOfWeek - 1
Loop
MsgBox "Day of the Week = "& dtmDayOfWeek & vbCrlf &_
"The command will be executed is : "& ArrCommands(IndexCommand),vbInformation,Title
'Select case to pickup the value of day of the week
Select Case dtmDayOfWeek
Case 1
Call Execute(ArrCommands(IndexCommand),0)
Case 2
Call Execute(ArrCommands(IndexCommand),0)
Case 3
Call Execute(ArrCommands(IndexCommand),0)
Case 4
Call Execute(ArrCommands(IndexCommand),1)
Case 5
Call Execute(ArrCommands(IndexCommand),1)
Case 6
Call Execute(ArrCommands(IndexCommand),1)
Case 7
Call Execute(ArrCommands(IndexCommand),1)
End Select
'MsgBox "Command line is done",vbInformation,Title
'----------------------------------------------------------------------------------
Function MyWeekday(MyDate)
If MyDate = "" Then MyDate = Date()
If IsDate(MyDate) Then
MyWeekDay = Weekday(MyDate)
Exit Function
End If
End Function
'----------------------------------------------------------------------------------
Sub Execute(StrCmd,Console)
Dim ws,MyCmd
Set ws = CreateObject("wscript.Shell")
'The console = 0 means will be running in hidden mode
If Console = 0 Then
MyCmd = "CMD /C " & StrCmd & " "
ws.run MyCmd,Console,True
End If
'The console = 1 means will be running in not hidden mode
If Console = 1 Then
MyCmd = "CMD /K " & StrCmd & " "
ws.run MyCmd,Console,True
End If
End Sub
'----------------------------------------------------------------------------------
这是一些严重的代码重复。
dtmDayOfWeek
只是一个数字,因此您可以使用它来询问一周中1-7天的数字。很好的努力,我们不会为他们重写它。唯一担心的是,如果他们对VBScript或一般编程都不熟悉,那么重构可能会太多。谢谢@Hackoo你是个救命恩人,有没有可能在这项任务上得到更多的帮助?实际上差不多完成了,只是星期五和星期六的命令有点不同,星期五我需要运行ipconfig并在屏幕上显示输出,星期六我需要运行hostname并在屏幕上显示输出。在我完成之后,我只是不知道如何将其添加到您制作的阵列中。很明显,对于Tracert来说,-n不是一个公认的命令?我不太清楚那是什么意思either@SirRevenant您想在星期五运行此命令吗?周六是什么命令?对不起,我应该澄清@Hackoo,因为周六我需要主机名,然后在屏幕上显示结果,我的老师对这是什么有点模糊。但我认为这是计算机的名称,类似于WScript.Echo CreateObject(“WScript.Network”)。ComputerName是否有某种主机名命令我不知道?@SirRevenant这听起来越来越像是家庭作业。