How to Schedule a daily task to run a batch file?

别等时光非礼了梦想. 提交于 2019-12-24 19:20:51

问题


I'm trying to schedule a task on Windows Server 2008 to delete some images on a folder. I create a batch file to execute this, my code below:

forfiles /s /m *.jpg /D -10 /C "cmd /c del @path"

(I didn't put the dir because I store the batch file on the same folder where I want to erase the images, so it's no necesary)

My issue is at the time I try to schedule this task to run this automatically. Because when I manually run this file IT ERASE THE IMAGES SUCCESSFULLY, also when I schedule this task with the condition to run it when the administrator is logged in. The problem comes when I specify the condition to run the file either the admin is logged or not and of course with high privileges. I don't know why in this case the task begins running at the time I programmed it, but it never runs the batch file, I can see at the task scheduler that my task is running but nothing's happening. Does somebody have a clue?

I'm including the screenshots of the task scheduler:


回答1:


When scheduling tasks to run even if no user is logged on, the working directory becomes C:\Windows\system32. So you have to jump to the proper directory or otherwise the files won't be found. Further, you have to use cd %~dp0 and not cd %CD% as %CD% will still be the system32 folder whereas %~dp0 will be the folder your batch file is located in.

EDIT: Sorry, I've just realized that Squashman has already given the same answer as a comment.



来源:https://stackoverflow.com/questions/48085250/how-to-schedule-a-daily-task-to-run-a-batch-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!