Question

I have a scheduled task in Windows Server 2003 R2 that is supposed get some files from a network location (mapped drive) and copy them to a local folder in preparation for an FTP sync to a mobile device later that day.

The task runs no problem when I click it and run it. Task scheduler says it is running updates the time it has run and the files appear in the correct location. Yet...

When the task is supposed to run over night (and I am not already logged in) the task runs (task scheduler indicates that the task ran at the specified time) but the file copy does not occur. I suspect this has to do with the task logging the account in, then running the file copy before the Network Drive has been connected and is ready.

Here is my code from the batch file:

@echo off

FORFILES /p "K:\Oncology\BSWRICS-MDM" /s /m *.* /c "cmd /c Del @path" /d -30

set "cleanup=K:\Oncology\BSWRICS-MDM"

for /f "usebackq tokens=*" %%a in (`dir /b/s/ad "%cleanup%" ^| sort /r`) do (rmdir "%%~a" 2>nul && echo:Removed: "%%a")

xcopy  K:\Oncology\BSWRICS-MDM\*.* C:\wamp\www\Portal\files\BSWRICS-MDM /Y /S 

FORFILES /p "C:\wamp\www\Portal\files\BSWRICS-MDM" /s /m *.* /c "cmd /c Del @path" /d -30

The first few lines delete files older than 30 days from the local folder, then the xcopy occurs. As I said, this script works perfectly when I log in first.

Is there some code I can insert at the start to have the script check if the network drive is ready and only if TRUE, proceed to the next instruction?

Some answers to expected questions: - The network drive letter never changes. - The account that logs in with the scheduled task is the same one I can successfully run the task from. - The task actually runs (logged in task scheduler) but there is no evidence of the file copy having been performed.

Thanks in advance.

Was it helpful?

Solution

You could delete the mapped drive and try to map it again, and then wait until the errorlevel shows it is connected before proceeding.

@echo off
:LOOP
net use Y: /delete
net use Y: \\server\share
if errorlevel 1 (
goto :ERROR
) else (
goto :OK
)

:ERROR
echo ERROR!
rem Try again!
timeout /t 5
goto :LOOP

:OK
echo OK!
rem Carry on!
pause >nul
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top