Batch File - Delete Command Failing when run from Task Scheduler

Hi

I have a batch file (text below) that fully works as expected when manually executed, but when executed by the Task Scheduler the Delete command fails - where the Copy and Rename commands within the batch both work correctly. It's just the Delete command that fails.

NOTE:
The copy and rename commands work, the Deleted doesn't

Everything works when run manually
I have tried adding 'ECHO Y | ' before the Del command - no joy
I have tried adding ' /Q' after the Del command - no joy
The E:\ drive is an actual drive on the server - not a mapped drive
I am running Windows Server 2008 R2

-=-=-=-=-=-=-=-=-=-=-=-=-
@ECHO OFF
E:
cd Mm_BAKs
CLS
ECHO ***********************************************
ECHO This deletes the old files
ECHO ***********************************************
ECHO.
ECHO Deleting old bak files
Del E:\Mm_BAKs\*.bak
ECHO.
ECHO ***********************************************
ECHO This imports the latest Live backup
ECHO ***********************************************
ECHO.
ECHO Copying L_Live file - this may take a while!
Copy "\\SOME-SERVER\DB Backup Folder\L_backup*.bak" E:\M_BAKs
ECHO.
ECHO Renaming L_backup file to L_Live_backup.bak
REN E:\Mm_BAKs\L_backup*.bak L_Live_backup.bak
Echo.
Echo.
ECHO **********************************************
ECHO This imports the latest FS backup
ECHO **********************************************
ECHO.
ECHO Copying new FS file
Copy "\\SOME-SERVER\DB Backup Folder\FS_backup*.bak" E:\M_BAKs
ECHO.
ECHO Renaming FS file to FS_backup.bak
REN E:\Mm_BAKs\FS_backup*.bak FS_backup.bak
-=-=-=-=-=-=-=-=-=-=-=-=-=-

Any help really appreciated.

LoveToSpod
LoveToSpodAsked:
Who is Participating?
 
Steve KnightIT ConsultancyCommented:
Agreed, permissions does sounds likely.  Is this running as a different user under task scheduler to running it manually (I would presume you have a special account for tasks scheduled?).

What is the error, "access denied", presumably?

Could it be the file is open/in-use otherwise?

The script refers to two directories too, m_baks and mm_baks, is that just a slip of the sanitising copy/paste for here I presume?

Steve
0
 
ComputerAidNZCommented:
Sounds suspiciouosly like a permissions issue here, you are able to read, write, but not delete - chjeck permissions first.
0
 
Steve KnightIT ConsultancyCommented:
Add in before the delete:

dir e:\mm_baks > e:\logfile.txt
del e:\mm_baks\*.bak >> e:\logfile.txt 2>>&1

to capture the output of what happens perhaps into a logfile.

Steve
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LoveToSpodAuthor Commented:
Hi, thanks for the quick response!

If I execute the file manually it will be using my permissions. The scheduler runs from a domain account which belongs to the Administrators group on the Server - therefore [I would have thought] has the permission to delete.
The two directories is a typo from sanitising!
Just trying out the logfile to check the output.
0
 
LoveToSpodAuthor Commented:
I have enabled full-control permissions for the account that runs the scheduled task on the source copy folder (where the original files are) and the destination copy folder. When I run it now from the scheduler IT WORKS!! . Permissions it is!!

Thanks for your help,

LoveToSpod


0
 
LoveToSpodAuthor Commented:
Top notch, thanks!
0
 
Steve KnightIT ConsultancyCommented:
No problem, all too easy to happen with NTFS permissions... the other classic being an account not in the correct share permissions (e.g. because you use a specific group or "domain users".

Steve
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.