gpt4 book ai didi

vbscript - VBScript-使用错误处理来确定任务是否存在

转载 作者:行者123 更新时间:2023-12-03 08:26:39 31 4
gpt4 key购买 nike

使用这些信息,我学会了herehere
我在下面创建了短代码。

当我运行代码并有意识地输入任务计划程序库的“\”文件夹中存在的任务时,我收到“任务不存在” msgbox。

我尝试在传递给函数的变量周围加上双引号,但没有成功。非常感谢任何指导。

Option Explicit
Dim strtaskn, strtaskf, sso, result

Call start
Sub start
strtaskn = ""
strtaskf = ""
Do While Len(strtaskn) = 0
strtaskn = inputbox("Please enter task name to search for:", "Task Exist?")
If isempty(strtaskn) Then
WScript.Quit()
End If
Loop

strtaskf = InputBox("Please enter task folder to search in:" & vbCrLf & vbCrLf & "Leave blank for default folder", "Task Exist?")
If Len(strtaskf) = 0 Then strtaskf = "\"
If IsEmpty(strtaskf) Then
WScript.Quit()
End If

'WScript.Echo strtaskn
'WScript.Echo strtaskf

Call taskexist(strtaskn, strtaskf)
End Sub

Function taskexist(taskname, taskfolder)
Dim rootfolder, task

Set sso = CreateObject("Schedule.Service")
Call sso.Connect()

On Error Resume Next
Set rootfolder = sso.GetFolder(taskfolder)
If Err.Number <> 0 Then
result = MsgBox("Task folder does not exist " & Err.Description & vbCrLf & vbCrLf & "Do you wish to try another search?", vbYesNo+vbCritical, "Task Exist?")
Select Case result
Case vbYes
Err.Clear
Call start
Case vbNo
WScript.Quit()
End Select
Err.Clear
End If

Set task = rootfolder.GetTasks(taskname)
If Err.Number <> 0 Then
result = MsgBox("Task does not exist " & Err.Description & vbCrLf & vbCrLf & "Do you wish to try another search?", vbYesNo+vbCritical, "Task Exist?")
Select Case result
Case vbYes
Err.Clear
Call start
Case vbNo
WScript.quit()
End Select
Err.Clear
End If
On Error Goto 0

result = MsgBox("Task exists" & vbCrLf & vbCrLf & "Do you wish to try another search?", vbYesNo+vbInformation, "Task Exist?")
End Function

更新

使用我的第一个 link中显示的示例,我可以看到另一种实现相同目标的方法,即将集合中找到的每个任务(按.name)加载到数组中,然后将元素与我正在搜索的任务名进行比较,或简单地使用迭代循环。

更新

在学习我的原始代码中的错误之前,感谢Ansgar。我开始编写替代版本的脚本,我相信与社区共享是正确的选择。
option explicit
dim strtaskn, strtaskf, sso, rootfolder, taskcollect, numtasks, intcomp, taskreg, result

call start
sub start
strtaskn=""
strtaskf=""
do while len(strtaskn)=0
strtaskn = inputbox("Please enter task name to search for:","Task Exist?")
if isempty(strtaskn) then
wscript.quit()
end if
loop

strtaskf = inputbox("Please enter task folder to search in:" &vbcrlf&vbcrlf& "Leave blank for default folder","Task Exist?")
if len(strtaskf)=0 then strtaskf = "\"
if isempty(strtaskf) then
wscript.quit()
end if

wscript.echo strtaskn
wscript.echo strtaskf

set sso = createobject("schedule.service")
call sso.connect()

on error resume next
set rootfolder = sso.getfolder(strtaskf)
if err.number <> 0 then
result = msgbox("Task folder does not exist " &err.description &vbcrlf&vbcrlf& "Do you wish to try another search?", vbyesno+vbcritical, "Task Exist?")
select case result
case vbyes
err.clear
call start
case vbno
wscript.quit()
end select
err.clear
end if
on error goto 0

set taskcollect = rootfolder.gettasks(0)

numtasks = taskcollect.count

if numtasks = 0 then
wscript.echo "No tasks are present in " &strtaskf& " folder"
else
for each taskreg in taskcollect
intcomp = strcomp(strtaskn, taskreg.name, vbtextcompare)
if intcomp = 0 then
result = msgbox("Task exists" &vbcrlf&vbcrlf& "Do you wish to try another search?", vbyesno+vbinformation, "Task Exist?")
select case result
case vbyes
call start
case vbno
wscript.quit()
end select
end if
next
result = msgbox("Task does not exist " &vbcrlf&vbcrlf& "Do you wish to try another search?", vbyesno+vbcritical, "Task Exist?")
select case result
case vbyes
call start
case vbno
wscript.quit()
end select
end if
end sub

最佳答案

Set task = rootfolder.GetTasks(taskname)


上面的陈述并没有按照您的想法做。 GetTasks 方法/属性(注意结尾的“s”)返回给定文件夹中的所有任务。它的参数是一个整数标志,指示是否应将隐藏任务包括在返回列表中(1→包括隐藏任务,0→不包括隐藏任务)。

您真正想要的是 GetTask 属性:
Set task = rootfolder.GetTask(taskname)

关于vbscript - VBScript-使用错误处理来确定任务是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39379627/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com