title: Scheduled Task .bat - check if network drive ready
body: <p>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.</p>
  
  <p>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...</p>
  
  <p>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.</p>
  
  <p>Here is my code from the batch file:</p>
  
  <pre><code>@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&gt;nul &amp;&amp; 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
  </code></pre>
  
  <p>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.</p>
  
  <p>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?</p>
  
  <p>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.</p>
  
  <p>Thanks in advance.</p>