0

I can't find a way to create a new folder with win32com in the Windows Task Scheduler, i can only open an already created one such has

import win32com.client as win32
service = win32.gencache.EnsureDispatch('Schedule.Service')
service.Connect()
root_folder = service.GetFolder("\\")

is there way to create a new one without going into the scheduler itself?

TylerH
  • 20,799
  • 66
  • 75
  • 101
Steven G
  • 16,244
  • 8
  • 53
  • 77

1 Answers1

0

Looks like you need to cast it to ITaskFolder first an then acess the CreateFolder method

import win32com.client as win32

service = win32.gencache.EnsureDispatch('Schedule.Service')
service.Connect()
root_folder = service.GetFolder("\\")
f = win32.CastTo(root_folder, "ITaskFolder")
f.CreateFolder('test_name')
Steven G
  • 16,244
  • 8
  • 53
  • 77
  • Do you think you need the cast? https://learn.microsoft.com/en-us/windows/win32/api/taskschd/nf-taskschd-itaskservice-getfolder suggests that `ITaskService::GetFolder()` already returns a `ITaskFolder` interface. I haven’t tested this to be fair. – DS_London Jun 12 '22 at 07:19
  • @DS_London I think the way you open the service will dictate if you need to cast it or not, with `win32.gencache.EnsureDispatch('Schedule.Service')` the `GetFolder` method is not available – Steven G Jun 12 '22 at 12:54
  • On my system at least (Windows 11), `EnsureDispatch('Schedule.Service')` returns an object of type: ``. The gen_py file `ITaskService.py` includes a `GetFolder()` method, which returns a Dispatch interface with Clsid `{8CFAC062-A080-4C15-9A88-AA7C2AF80DFC}`, which is the IID for `ITaskFolder`, and `ITaskFolder.py` contains the `CreateFolder()` method. Or put more succinctly, your code , on my machine at least, runs fine and creates a new folder without the `CastTo`. – DS_London Jun 12 '22 at 17:33
  • To follow on ... where you DO need CastTo is when you work with the Tasks themselves, as in this answer: https://stackoverflow.com/questions/70963769/how-to-access-windows-scheduler-actions-with-python-and-win32com/70971108#70971108 – DS_London Jun 12 '22 at 17:37