1.

Solve : SOLVED -- Trouble with script run from Scheduled Tasks?

Answer»

Hi all,

Have a batch script whose intention is to delete files based on the passing of time.

There are two files associated with the script; the script itself and a parameter file.
The problem I'm having is that when the script is run as a scheduled task, it successfully deletes all files on the LOCAL file system. However there are additional entries in the parameter file that refer to a mapped drive (mapped network storage area device). It is not performing the necessary deletion's from this device.

On the flip side, if I logon and execute the same scheduled task or execute the script manually, all applicable files in those locations contained in the parameter file are deleted successfully.

The scheduled task is run under my own UID - and the results have me stumped as I have ADMINISTRATOR priv's.

OS: Windows 2003

Batch Script:
Code: [Select]:: ----------------------------------------------------------------------------------------------------------
:: --
:: -- Script : Delete_Old_Files.bat
:: --
:: -- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
:: --
:: -- Purpose :
:: --
:: -- To delete old/unwanted files from the local and potential member servers.
:: -- The script uses delete_old_files.txt for input to identify files for deletion.
:: --
:: ----------------------------------------------------------------------------------------------------------

@echo off

if not "%1"=="" goto %1

for /F "tokens=1-4 delims=/ " %%i in ('date /t') do (
set DayOfWeek=%%i
set Day=%%j
set Month=%%k
set Year=%%l
set Date=%%l-%%k-%%j
)

for /F %%i in ('time /t') do (
set CurTime=%%i
)

set SleepTime=10
set OutputLog=%~dp0Logs\%~n0-%Date%.log

@echo. >> %OutputLog%
@echo ------------------------------------------------------------------------------ >> %OutputLog%
@echo ------------------------------------------------------------------------------ >> %OutputLog%
@echo Start of batch job %0 on %Date% at %CurTime% >> %OutputLog%
@echo ------------------------------------------------------------------------------ >> %OutputLog%
@echo ------------------------------------------------------------------------------ >> %OutputLog%
@echo. >> %OutputLog%

set Directories=%~dpn0.txt
set Recursive=TRUE

for /F "tokens=1,2,3,4 delims=;" %%i in (%Directories%) do (
set Dir=%%i
set Age=%%j
set Files=%%k
set Recursive=%%l
call %0 deletefiles
)

goto FinishRun

:deletefiles
set RFlag=
if %Recursive%==TRUE set RFlag=/S

forfiles /P %Dir% /D -%Age% %RFlag% /M %Files% /C "cmd /c @echo Deleting @path as it is older than %Age% days >> %OutputLog%"
@echo forfiles /P %Dir% /D -%Age% %RFlag% /M %Files% /C "cmd /c del /F @path >> %OutputLog%" >> %OutputLog%
forfiles /P %Dir% /D -%Age% %RFlag% /M %Files% /C "cmd /c del /F @path >> %OutputLog%"
@echo.
@echo *** Sleep for %SleepTime% seconds ***
@echo. >> %OutputLog%
@echo *** Sleep for %SleepTime% seconds *** >> %OutputLog%
sleep %SleepTime%

goto :EOF

:FinishRun

for /F "tokens=1-4 delims=/ " %%i in ('date /t') do (
set DayOfWeek=%%i
set Day=%%j
set Month=%%k
set Year=%%l
set Date=%%l-%%k-%%j
)

for /F %%m in ('time /t') do (
set CurTime=%%m
)

@echo. >> %OutputLog%
@echo ------------------------------------------------------------------------------ >> %OutputLog%
@echo ------------------------------------------------------------------------------ >> %OutputLog%
@echo Finish of batch job %0 on %Date% at %CurTime% >> %OutputLog%
@echo ------------------------------------------------------------------------------ >> %OutputLog%
@echo ------------------------------------------------------------------------------ >> %OutputLog%
@echo. >> %OutputLog%


goto :EOF
Parameter File:
C:\batchjobs\logs ;31 ;*.log ;TRUE
C:\batchjobs\logs ;31 ;*.zip ;TRUE
C:\batchjobs\logs ;7 ;*.bkf ;TRUE
S:\BKUP-DAILY ;8 ;*.bkf ;TRUE
S:\BKUP-SYSSTATE ;2 ;*.bkf ;TRUE
S:\BKUP-WEEKLY ;21 ;*.bkf ;TRUE

Sample of the logging file.
The first run was via the Scheduled Task, the second run was when I logged in.

Code: [Select]
------------------------------------------------------------------------------
------------------------------------------------------------------------------
Start of batch job C:\Batchjobs\Delete_Old_Files.bat on 2008-09-24 at 00:30
------------------------------------------------------------------------------
------------------------------------------------------------------------------

Deleting "C:\batchjobs\logs\MYOB-Newsletter_COPY-08-24-Sun.log" as it is older than 31 days
Deleting "C:\batchjobs\logs\DALA_Backups-2008-08-24.log" as it is older than 31 days
Deleting "C:\batchjobs\logs\Delete_old_files-2008-08-24.log" as it is older than 31 days
forfiles /P C:\batchjobs\logs /D -31 /S /M *.log /C "cmd /c del /F @path >> C:\Batchjobs\Logs\Delete_Old_Files-2008-09-24.log"

*** Sleep for 10 seconds ***
Deleting "C:\batchjobs\logs\DALA_Backups-2008-08-24.zip" as it is older than 31 days
forfiles /P C:\batchjobs\logs /D -31 /S /M *.zip /C "cmd /c del /F @path >> C:\Batchjobs\Logs\Delete_Old_Files-2008-09-24.log"

*** Sleep for 10 seconds ***
forfiles /P C:\batchjobs\logs /D -7 /S /M *.bkf /C "cmd /c del /F @path >> C:\Batchjobs\Logs\Delete_Old_Files-2008-09-24.log"

*** Sleep for 10 seconds ***
forfiles /P S:\BKUP-DAILY /D -8 /S /M *.bkf /C "cmd /c del /F @path >> C:\Batchjobs\Logs\Delete_Old_Files-2008-09-24.log"

*** Sleep for 10 seconds ***
forfiles /P S:\BKUP-SYSSTATE /D -2 /S /M *.bkf /C "cmd /c del /F @path >> C:\Batchjobs\Logs\Delete_Old_Files-2008-09-24.log"

*** Sleep for 10 seconds ***
forfiles /P S:\BKUP-WEEKLY /D -21 /S /M *.bkf /C "cmd /c del /F @path >> C:\Batchjobs\Logs\Delete_Old_Files-2008-09-24.log"

*** Sleep for 10 seconds ***

------------------------------------------------------------------------------
------------------------------------------------------------------------------
Finish of batch job C:\Batchjobs\Delete_Old_Files.bat on 2008-09-24 at 00:31
------------------------------------------------------------------------------
------------------------------------------------------------------------------

[emailprotected]
[emailprotected] [emailprotected]
MailSubject=Delete_Old_Files on DALA-FS - 2008-09-24 (Wed) / 00:31
MailBody=Delete_Old_Files on DALA-FS for 2008-09-24 (Wed) has completed.
sendmail.vbs -f [emailprotected] -t [emailprotected] [emailprotected] -s "Delete_Old_Files on DALA-FS - 2008-09-24 (Wed) / 00:31" -b "Delete_Old_Files on DALA-FS for 2008-09-24 (Wed) has completed." -a C:\Batchjobs\Logs\Delete_Old_Files-2008-09-24.log

------------------------------------------------------------------------------
------------------------------------------------------------------------------
Start of batch job C:\Batchjobs\Delete_Old_Files.bat on 2008-09-24 at 00:38
------------------------------------------------------------------------------
------------------------------------------------------------------------------

forfiles /P C:\batchjobs\logs /D -31 /S /M *.log /C "cmd /c del /F @path >> C:\Batchjobs\Logs\Delete_Old_Files-2008-09-24.log"

*** Sleep for 10 seconds ***
forfiles /P C:\batchjobs\logs /D -31 /S /M *.zip /C "cmd /c del /F @path >> C:\Batchjobs\Logs\Delete_Old_Files-2008-09-24.log"

*** Sleep for 10 seconds ***
forfiles /P C:\batchjobs\logs /D -7 /S /M *.bkf /C "cmd /c del /F @path >> C:\Batchjobs\Logs\Delete_Old_Files-2008-09-24.log"

*** Sleep for 10 seconds ***
Deleting "S:\BKUP-DAILY\DALA_Backups-2008-09-16-C-Drive.bkf" as it is older than 8 days
Deleting "S:\BKUP-DAILY\DALA_Backups-2008-09-16-D-Drive.bkf" as it is older than 8 days
forfiles /P S:\BKUP-DAILY /D -8 /S /M *.bkf /C "cmd /c del /F @path >> C:\Batchjobs\Logs\Delete_Old_Files-2008-09-24.log"

*** Sleep for 10 seconds ***
Deleting "S:\BKUP-SYSSTATE\Systemstate-2008-09-20.bkf" as it is older than 2 days
Deleting "S:\BKUP-SYSSTATE\Systemstate-2008-09-21.bkf" as it is older than 2 days
forfiles /P S:\BKUP-SYSSTATE /D -2 /S /M *.bkf /C "cmd /c del /F @path >> C:\Batchjobs\Logs\Delete_Old_Files-2008-09-24.log"

*** Sleep for 10 seconds ***
Deleting "S:\BKUP-WEEKLY\DALA_Backups-2008-09-01-C-Drive.bkf" as it is older than 21 days
Deleting "S:\BKUP-WEEKLY\DALA_Backups-2008-09-01-D-Drive.bkf" as it is older than 21 days
forfiles /P S:\BKUP-WEEKLY /D -21 /S /M *.bkf /C "cmd /c del /F @path >> C:\Batchjobs\Logs\Delete_Old_Files-2008-09-24.log"

*** Sleep for 10 seconds ***

------------------------------------------------------------------------------
------------------------------------------------------------------------------
Finish of batch job C:\Batchjobs\Delete_Old_Files.bat on 2008-09-24 at 00:41
------------------------------------------------------------------------------
------------------------------------------------------------------------------

[emailprotected]
[emailprotected] [emailprotected]
MailSubject=Delete_Old_Files on DALA-FS - 2008-09-24 (Wed) / 00:41
MailBody=Delete_Old_Files on DALA-FS for 2008-09-24 (Wed) has completed.
sendmail.vbs -f [emailprotected] -t [emailprotected] [emailprotected] -s "Delete_Old_Files on DALA-FS - 2008-09-24 (Wed) / 00:41" -b "Delete_Old_Files on DALA-FS for 2008-09-24 (Wed) has completed." -a C:\Batchjobs\Logs\Delete_Old_Files-2008-09-24.log

Any ASSISTANCE would be really appreciated as this has got me absolutely stumped.

Cheers,
CameronQuote

On the flip side, if I logon and execute the same scheduled task or execute the script manually, all applicable files in those locations contained in the parameter file are deleted successfully.

It appears the the drive is only mapped when you are logged on and then only if the drive was made persistent. How and when is the drive mapped?

Can you MOVE mapping the drive to your batch file? If you need to make temporary mappings for your file and you don't want to screw up any system settings, it's possible to map multiple drive letters to the same network drive.

Good luck and glad to see you back Cameron. Many thanks for the reply & welcome back SIdewinder; has been a while.

Taking on board your suggestion is the likely way to go - had skipped over the idea a few days ago.

Will look into the use of pushd/popd and see how I go.
Problem solved, so for anyone interested read on .....

The use of pushd & popd as I'd thought was a go-er was not the way forward.
Had issues with how pushd truncated USB network storage device paths.

Instead used the 'subst' command.
Placement is majorly important due to the 'call' statement - the 'subst' statements must be executed within the 'call' statement (in the same shell).

This is how it was resolved ....
Code: [Select]set Directories=%~dpn0.txt
set Recursive=TRUE

for /F "tokens=1,2,3,4 delims=;" %%i in (%Directories%) do (
set Dir=%%i
set Age=%%j
set Files=%%k
set Recursive=%%l

call %0 deletefiles

)

goto FinishRun

:deletefiles

SUBST X: %Dir%

set RFlag=
if %Recursive%==TRUE set RFlag=/S

forfiles /P X: /D -%Age% %RFlag% /M %Files% /C "cmd /c @echo Deleting @path as it is older than %Age% days >> %OutputLog%"
@echo forfiles /P X:(%Dir%) /D -%Age% %RFlag% /M %Files% /C "cmd /c del /F @path >> %OutputLog%" >> %OutputLog%
forfiles /P X: /D -%Age% %RFlag% /M %Files% /C "cmd /c del /F @path >> %OutputLog%"

SUBST X: /D

@echo.
@echo *** Sleep for %SleepTime% seconds ***
@echo.
@echo. >> %OutputLog%
@echo *** Sleep for %SleepTime% seconds *** >> %OutputLog%
@echo. >> %OutputLog%
sleep %SleepTime%

goto :EOF
The Parameter file also changed to be explicit in its detail ...
C:\batchjobs\logs ;31 ;*.txt ;TRUE
C:\batchjobs\logs ;31 ;*.log ;TRUE
C:\batchjobs\logs ;31 ;*.zip ;TRUE
C:\batchjobs\logs ;7 ;*.bkf ;TRUE
\\192.168.0.115\Backup\BKUP\BKUP-DAILY ;8 ;*.bkf ;TRUE
\\192.168.0.115\Backup\BKUP\BKUP-SYSSTATE ;2 ;*.bkf ;TRUE
\\192.168.0.115\Backup\BKUP\BKUP-WEEKLY ;21 ;*.bkf ;TRUE

Hope this is all of some HELP for anyone else too.

Cheers,
Cameron



Discussion

No Comment Found