Reputation: 7242
I have a problem I cannot understand so I hope im putting it on the right section of StackNetwork.
I have a batch file.
It dumps data, zips a file and then removes the source folder that was just zipped.
Here are the last steps
CD "C:\Backup\%DATE%\"
REM ----- NOW ZIP THE FILE AND DELETE THE SOURCE -----
"C:\Program Files\WinRAR\WinRAR.exe" a -r -afzip "C:\Backup\%DATE%.zip"
CD "C:\BackUp\"
RMDIR /s /q "C:\BackUp\%DATE%\"
This line of code, deletes the folder.
RMDIR /s /q "C:\BackUp\%DATE%\"
The problem is:
When I manually execute the Scheduled task, by right clicking the task and selecting RUN
, the batch file runs excellent and deletes the folder in the end.
But when the task runs automatically at night, all goes well, except, the folder is not deleted.
I assume it has to do with priviliges as it does work when I manually RUN
the task, but whats funny is that the folder %DATE%
is being created whitin the batch file.
The task is set to run with highest priviliges, even if the the user is logged in.
Upvotes: 3
Views: 3799
Reputation: 49186
Before I write about my ideas for possible reasons on directory deletion failure, I suggest to use in batch file:
RMDIR /s /q "C:\BackUp\%TODAY%\" 2>"C:\BackUp\RemoveDirError.log"
Look on "C:\BackUp\RemoveDirError.log"
after next scheduled task run which should contain the error message being hopefully helpful on finding the reason for failed deletion of the directory.
The value of environment variable DATE is always the current date.
The value of DATE at beginning of batch file would be different to value on end of the batch file if this batch file is executed over midnight.
Example:
The batch file is started on 23:30:00 every day and needs 50 minutes to complete.
The batch code on execution would be in this case for example:
CD "C:\Backup\2014-09-08\"
REM ----- NOW ZIP THE FILE AND DELETE THE SOURCE -----
"C:\Program Files\WinRAR\WinRAR.exe" a -r -afzip "C:\Backup\2014-09-08.zip"
CD "C:\Backup\"
RMDIR /s /q "C:\Backup\2014-08-10\"
Note: Format of date string depends on Windows language settings for date.
The result is the error message:
The system cannot find the file specified.
The solution for this problem is to assign value of DATE to a new variable at beginning of the batch file and reference this variable instead of DATE in entire batch file.
set TODAY=%DATE%
CD "C:\Backup\%TODAY%\"
REM ----- NOW ZIP THE FILE AND DELETE THE SOURCE -----
"C:\Program Files\WinRAR\WinRAR.exe" a -r -afzip "C:\Backup\%TODAY%.zip"
CD "C:\BackUp\"
RMDIR /s /q "C:\BackUp\%TODAY%\"
The environment variable TODAY contains here the date string definitely not changed during the execution of the batch file.
Another problem could be that WinRAR is a Windows application and not a console application and could be therefore executed in a separate process executed parallel to the batch file with C:\BACKUP\%DATE%\
as current working directory.
The batch job would continue execution immediately after string WinRAR in this case and this is the reason deleting the directory is prevented by Windows.
The solution is to use command start
with option /wait
to force running WinRAR as separate process but halt execution of batch job until WinRAR has terminated itself after finishing compression.
set TODAY=%DATE%
CD "C:\Backup\%TODAY%\"
REM ----- NOW ZIP THE FILE AND DELETE THE SOURCE -----
start "Backup Compression" /wait /min "C:\Program Files\WinRAR\WinRAR.exe" a -r -afzip "C:\Backup\%TODAY%.zip"
CD "C:\BackUp\"
RMDIR /s /q "C:\BackUp\%TODAY%\"
But this is most likely not the reason as in this case the deletion of the folder would fail also on running the scheduled task manually.
It is possible that WinRAR detects a problem during compression and opens a message prompt with a question to answer by the user. As on scheduled task execution nobody answers it, WinRAR holds compression for an unusual long time.
The solution would be adding -y
to WinRAR command line in batch file.
start "Backup Compression" /wait /min "C:\Program Files\WinRAR\WinRAR.exe" a -r -afzip -y "C:\Backup\%TODAY%.zip"
Again I think, this is not very likely for not deleting the directory.
I have no more ideas. We would need to see complete batch file code if nothing above helps to find the reason for error on deletion of the directory on running the batch file as automatically executed scheduled task.
Upvotes: 6