问题
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