0

I create task with VBS, task create and run on time perfect

dim fso, f, pather, objShell
set objShell = Wscript.CreateObject("WScript.Shell")
Set fso = createObject("Scripting.FileSystemObject")
Set f = fso.GetFile(WScript.ScriptFullName)
pather = fso.GetParentFolderName(f)

controlTask = "schtasks /create /tn ""Controler"" /tr " & CHR(34) & pather & "\myFile.txt" & CHR(34) & " /sc minute /mo 50"
objShell.Run controlTask

And when I try to delete task

Set oShell = WScript.CreateObject("WScript.Shell")
oShell.Run "schtasks /delete /tn ""Controler"" /f"

the task is not deleted. Why is what?

If I create task with direct path to the file ("schtasks /create /tn ""Controler"" /tr ""c:user\meme\myFile.txt"" /sc minute /mo 50"), task create and delete. But my code assumes that "myFile.txt" travel with vbs file.

Geert Bellekens
  • 12,788
  • 2
  • 23
  • 50
  • The `GetParentFolderName()` method does not get you the full path which `/tr` will be expecting so it can’t locate `myfile.txt` hence the error. – user692942 Nov 01 '20 at 20:25
  • Try this command to find if exist the taskname Control `schtasks /query | find /I "control"` – Hackoo Nov 01 '20 at 20:45
  • The [`/tr` argument](https://learn.microsoft.com/en-us/windows/win32/taskschd/schtasks) expects a full file path of the task file to run which [`GetParentFolderName()`](https://learn.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/scripting-articles/22dyy47c(v=vs.84)) does not provide. That is the reason for the error. Before trying to make it run from VBScript go into the command prompt and run `schtasks` yourself and check the output there, including VBScript just adds an extra layer of complexity that is unnecessary until the issue is corrected. – user692942 Nov 01 '20 at 21:21
  • I have no problem creating task, i have a problem deleting it –  Nov 02 '20 at 17:01
  • Did you try both a create then delete from the command prompt as suggested? Whether the create in VBScript works is irrelevant because the path you pass into it isn’t a full file path. – user692942 Nov 03 '20 at 09:04

1 Answers1

0

ok, this is the way. i could not delete it and i disable it

 oShell.Run "SCHTASKS /CHANGE /TN ""Control"" /DISABLE"