Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
829 views
in Technique[技术] by (71.8m points)

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

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:

This is where I see the task running, as you can see I selected the options for high privileges

This is the folder where I have my batch file

Conditions

Settings

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

When scheduling tasks to run even if no user is logged on, the working directory becomes C:Windowssystem32. 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...