任务删除一个目录运行成功,但目录仍然在那里

我在Windows 2012文件服务器上的任务计划程序中有一个任务。 任务必须在每个星期天晚上删除一个临时的一般访问目录的内容。 它在预定的时间运行并且每次都报告成功,但是目录内容没有被删除。

任务包括这个batch file

D: cd D:\Shared\Temp\ del /Q /F /S "D:\Shared\Temp\*.*" 

这是任务的XML:

 <?xml version="1.0" encoding="UTF-16"?> <Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task"> <RegistrationInfo> <Date>2014-06-16T12:13:45.8524361</Date> <Author>OFFICE\Administrator</Author> <Description>Weekly deletes the contents of the Temp directory</Description> </RegistrationInfo> <Triggers> <CalendarTrigger> <StartBoundary>2014-06-22T20:00:00</StartBoundary> <Enabled>true</Enabled> <ScheduleByWeek> <DaysOfWeek> <Sunday /> </DaysOfWeek> <WeeksInterval>1</WeeksInterval> </ScheduleByWeek> </CalendarTrigger> </Triggers> <Principals> <Principal id="Author"> <UserId>OFFICE\Administrator</UserId> <LogonType>Password</LogonType> <RunLevel>LeastPrivilege</RunLevel> </Principal> </Principals> <Settings> <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy> <DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries> <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries> <AllowHardTerminate>true</AllowHardTerminate> <StartWhenAvailable>false</StartWhenAvailable> <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable> <IdleSettings> <StopOnIdleEnd>true</StopOnIdleEnd> <RestartOnIdle>false</RestartOnIdle> </IdleSettings> <AllowStartOnDemand>false</AllowStartOnDemand> <Enabled>true</Enabled> <Hidden>false</Hidden> <RunOnlyIfIdle>false</RunOnlyIfIdle> <WakeToRun>true</WakeToRun> <ExecutionTimeLimit>P3D</ExecutionTimeLimit> <Priority>7</Priority> </Settings> <Actions Context="Author"> <Exec> <Command>"D:\IT\tempdelete.bat"</Command> </Exec> </Actions> </Task> 

我不明白为什么这个任务不能做它应该做的事情。 有更多经验的人可以对此有所了解吗?

2014年8月11日更新

还有另一个答案在这里build议在我的批处理命令(我不知道为什么它已被删除)下面的编辑:

 D: cd D:\Shared\Temp\ del /F /S "D:\Shared\Temp\*.*" 1> D:\my.log 2>&1 

这个周末我跑了这个脚本。 任务计划程序仍然声称工作成功完成,但这次我有一个矛盾的my.log文件:

 The system cannot find the path specified. 

我在脚本中看不到path定义错误。 指定的path是正确的。

应该改变目录作为joeqwerty状态?
要么
我是否应该在括号中定义目录更改?
或者是什么???

我可以想到两种可能性。

1)在计划的任务完成后添加文件。 但我怀疑你正在检查时间戳,看到任务之前的date。 所以这可能不是问题。

2)一个或多个文件可能被另一个进程locking,这将阻止删除。

不幸的是DEL命令如果失败就不会返回错误码。 它向stderr输出一个错误信息,但是返回的ERRORLEVEL总是0。

您可以通过使用2>somePath\someFile.log将您的DEL命令的stderrredirect到文件来确认locking的文件导致了问题。 当出现故障时,您应该看到类似The process cannot access the file because it is being used by another process.的错误消息The process cannot access the file because it is being used by another process. 在日志文件中。

如果要检测并在一个或多个文件被locking时采取措施,则可以使用如何在del失败时停止批处理脚本的变体。

 set "locked=" for /r "D:\Shared\Temp" %%F in (.) do dir /ad "%F" >nul 2>nul && (ren "%F\*" * || set locked=1) if defined locked echo Take some action because files are locked. 

如果一个或多个文件被locking,则将文件重命名为原始名称将失败。 但是,如果给定文件夹中没有文件,则不应该进行testing。 DIR / AD命令检测给定的文件夹是否包含文件。 FOR / R命令迭代所有文件夹,包括指定的根目录。

编辑

我应该阅读我进一步引用的链接。 接近底部的答案提供了一个更直接的方式来检测失败的删除,而不使用额外的命令来检测打开的文件。 只需检查DEL命令的stderr输出即可。 卫生署!

 del /Q /F /S "D:\Shared\Temp\*.*" 2>&1 1>nul | findstr "^" >nul && echo Take action because one or more files were locked and could not be deleted 

这个解决scheme比较好,因为它直接检测DEL错误。 如果文件在locking检查后被locking,但在删除之前,将locking的文件作为单独的命令查找可能会失败。