Link to home
Start Free TrialLog in
Avatar of audExpert
audExpert

asked on

Why does a batch file execute by a scheduled task in Win2K3 not copy files?

Why is my batch file failing or how can I figure out why?

I have a batch file that copies files from a development machine to a production machine.  When I run the bat file manually while logged in as an administrator, the files are copied successfully.  However, when I execute the bat file through scheduled tasks, even though the job runs and completes, no files are copied.  The job is scheduled to run under an administator account, has administrator permissons for the job, and the mapping is using the same administrator permissions.

This is the bat file:

===================
cd c:\windows\system32
start /wait net use K: \\AU-WEBDEV-VM\pyrlman09 /persistent:yes /Y
cd e:\inetpub\wwwroot\pyrlman09
e:
echo on
mcopy K:\*.* e:\inetpub\wwwroot\pyrlman09 /S/e/Y/v/c/d/i
EXIT
===================

When I open K, there are files.

When I log the results of the scheduled task, I get the following in the log file:

===============
C:\WINDOWS\system32\ScheduledTasks>cd c:\windows\system32
C:\WINDOWS\system32>start /wait net use K: \\AU-WEBDEV-VM\pyrlman09 /persistent:yes /Y
C:\WINDOWS\system32>cd e:\inetpub\wwwroot\pyrlman09
C:\WINDOWS\system32>e:
E:\inetpub\wwwroot\pyrlman09>echo on
E:\inetpub\wwwroot\pyrlman09>xcopy K:\*.* e:\inetpub\wwwroot\pyrlman09 /S/e/Y/v/c/d/i
0 File(s) copied
E:\inetpub\wwwroot\pyrlman09>EXIT
========================

But when I run the bat file manually the desired file gets copied.  Please advise as this has been a bane for at least a couple of years.  Any other suggestions are appreciated.
Avatar of Glen Knight
Glen Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Are you specifying a user for te scheduled task?
If so does it have access to the folders/files at both locations?

If not you need to because it's unlikely te service account will have access
The log is not showing the error output. You have not provided a password to the net use. Is it the same as for the admin account?
ASKER CERTIFIED SOLUTION
Avatar of Bryon H
Bryon H
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of audExpert
audExpert

ASKER

There is no error in the log.  The error is that the number of files copied is 0.  When I run the bat file manually, the number of files copied is 1.  Of course, I have to remove the file from the target each time I do this test.  

True, I did not provide a password in the net use command but even when I do, the results are the same.  This is the optional line that I use:

start /wait net use K: \\AU-WEBDEV-VM\pyrlman09 thepassword /user:au-webdev-vm\JobUser /persistent:yes

This user name is in the administrator groups of both the target and source machine.  Please note that this job is pulling data from K, the source to E, the local machine.

Thank you .
Bryon,
If we optimize the cd and drive change, we do a
cd /d e:\inetpub\wwwroot\pyrlman09
That does both changing the drive and the folder.
The swap you suggest doesn't make any difference.

However, I agree to take out the unnecessary start command. It's output or error messages are not logged into the scheduler log, because net use runs in an own window if called with start. And there is no reason why the start should be done in conjunction with /wait (without that, it would make sense).
My mistake in posting the bat file code.  With respect to the xcopy comment, I have been using mcopy but tried xcopy to see if my effort to use mcopy was the problem.  I simply pasted the xcopy version but I have been using mcopy.
Sorry, audExpert, had overlooked that you did not provide ANY credentials, which is ok if the tasks runs with an appropriate account (which it does).
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I logged the results of the job by setting the properites of the job in task scheduler.  The run command value is:
C:\WINDOWS\system32\ScheduledTasks\PyrlMans.bat >> C:\windows\system32\scheduledtasks\pyrlmanlog2.txt

However, I am now trying Qlemo's and bryon's recommended changes to the bat file.
please check what credentials the task is configured to run as

also, you might add this to the end of each line that does something, so we can see all the output:
>> c:\batchlog.txt

so:
cd c:\windows\system32
start /wait net use K: \\AU-WEBDEV-VM\pyrlman09 /persistent:yes /Y >> c:\batchlog.txt
cd e:\inetpub\wwwroot\pyrlman09
e:
echo on
mcopy K:\*.* e:\inetpub\wwwroot\pyrlman09 /S/e/Y/v/c/d/i >> c:\batchlog.txt
EXIT

include the changes already mentioned, the above is just an example

Success.  It appears that the problem was the word "start."  I changed the bat file to read:

net use K: \\AU-WEBDEV-VM\pyrlman09 /persistent:yes /Y 2>&1 >c:\windows\system32\scheduledtasks\netuse.log.  Then I tested the bat manually and it copied the file from source to target.
I then deleted the test file on target and retried the job using the scheduled job.  This time the job completed and copied the test file to the target machine.  The netuse.log read as follows:
"The command completed successfully."

All of your help is very very much appreciated!!!!!!!!!!!  Thank you.  I would kindly like to split the points between sir Qlemo and Byron.
I read that I needed to have the wait command in the net use statement to insure that the batch file didn't proceed before continueing.  I hope that eliminating "start /wait" is ok.
it will wait for the net use to either work, or fail... without the start command

feel free to split points however you wish, or ask more questions :)
Absolutely. Start is used to "spawn" a program in-parallel, without  waiting. With the /wait switch, you can wait on programs which do  usually start without halting the calling program, like notepad. It's  perfectly fine to omit start /wait from your job.
 
 Example for proper use of start /wait:
 
REM -- This starts notepad, and continues immediately notepad.exe echo notepad started, no wait echo. REM -- This one waits. start /wait notepad echo notepad startet, wait
The last echo will be shown only after closing the second notepad.
Thank you for that clarification.  I've tested the job and the task will definitely not work with the start /wait or /wait entries.  The reason I added it was that the drive connections sometimes drop and the job seems to proceed before these connections are entirely restored.  To test the new edits, I dropped or disconnected the mapping for K and tried the job.  While the mappings are not showing in the explorer windows, the job still succeeded.  Thanks again for your help.
Help much appreciated.  Also appreciated learning how to capture output to a logfile.