i created a script that creating a schedueld task from command line on task manager, the problem is that some proccess are launched without a full path on the command line from task manager, i managed to find the location for the process with this line:
location = psutil.Process(pid=int(pid)).exe()
but the problem is that some commands are printed with the location like this:
C:\Users\jacob\AppData\Local\slack\app-4.3.4\slack.exe --type=gpu-process
and some of them like this:
slack.exe --type=gpu-process
how can i strip the file path from every command and add it from the location variable from this script:
import psutil
import os
os.system("""powershell -command ""Unregister-ScheduledTask -TaskPath "\Lsports Tasks\" -a""")
commands = []
for proc in psutil.process_iter(['pid','name']):
process = proc.info
if process['name'] == 'slack.exe':
pid = process['pid']
cmds = psutil.Process(pid=int(pid)).cmdline()
cmda = ' '.join(cmds)
cmd=os.path.split(cmda)
print(cmd)
commands.append(cmd)
location = psutil.Process(pid=int(pid)).exe()
print(location)
i = 0
for c in commands:
i+= 1
task = os.system(f"""SCHTASKS /CREATE /SC ONSTART /RU system /TN "Lsports Tasks\Lsports Runner{i}" /TR "{c}""".format(i,c=c))