Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
832 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...