# creating a DOS script to copy files



## itsonlyme4 (Jan 2, 2009)

Let me start by saying that today, I am using Windows Scheduler to copy (and then delete) a file over our network (to a UNC path) from one server to another using this .bat file:


```
xcopy F:\Backups\PRODDM* [URL="file://kocxraid01/DB2backup"]\\Kocxraid01\DB2backup[/URL] >>F:\Backups\LOGS\ProdDMBackupAfterLoad.txt
del F:\Backups\PRODDM* >>F:\Backups\LOGS\ProdDMBackupAfterLoad.txt
```
It has been working fine for quite some time - the credentials come from the windows scheduler.

Now I have the requirement to stop using Windows Scheduler and have a third party scheduling software call the .bat file to perform the file copy and delete.

The trouble I am having is that when the third party scheduler calls the job, the job fails because of an 'access denied' error. Obviously the credentials that start the third party scheduler service does not have access to the share I am copying to.

How do I pass the proper network credentials in my script? I don't think I can do it with xcopy!!?? I tried NET USE to try and map a none persisent drive but I can't seem to get the syntax correct.

Does anyone have any suggestions for how I can get this accomplished?


----------



## Impera (Jul 17, 2005)

Hi,

Try invoking your script with the "runas" command. This will let you enter the credentials (user id and password) required to run the script.

Hope it helps


----------



## itsonlyme4 (Jan 2, 2009)

Please forgive my ignornance.. but how would I INVOKE the script using RUNAS. I did RUNAS /? from a command prompt but I still don't understand how to invoke the script using RUNAS. Can you provide the syntax?


----------



## Impera (Jul 17, 2005)

Hi
Open cmd and type:

runas /u:_youruser_ _yourscript.bat_

This will ask you for the youruser password. Once you type it in, the script will be run with the credentials supplied.


----------



## TheOutcaste (Aug 8, 2007)

Problem with RunAs is you have to enter the password each time, which kind of defeats the purpose of running this as a scheduled task.
You can use *savecred* to save the password, but then anyone who knows the username can use RunAs to run _anything_ without needing to know the password, a rather big security hole.

Easiest solution would probably be to change the credentials the third party scheduler service is running under.
Open *Services* (*Start* | *Run*, type *services.msc*, press *Enter*)
Right click the third party scheduler service, click *Properties*
Click the *Log On* tab
Change the selection under *Log on as:* from *Local System Account* to *This account:* and enter the credentials for the account you want to use..

Net Use should also work, but there are a few issues:


If you don't want to be prompted for the password, it has to be included in the script. Anyone who can read the file can read the password.
You _must_ make sure the user is not using the drive letter you want to use for a different share.
The share cannot already be mapped to a different drive letter, or you won't be able to map it.
If the above won't be a problem, it should be very simple:
First check the drive letter and delete if it is in use, just in case there was a power failure the last the the script ran and the drive is still mapped.
*If EXIST **V:** Net Use **V:** /Delete*
To use a local account on the PC:
* Net Use V: \\Kocxraid01\DB2backup /USER:username password*
If you need to use a Domain account:
* Net Use V: \\Kocxraid01\DB2backup /USER:domainname\username **password* 
Now delete the map:
*Net Use **V:** /Delete
*
I've written a batch file subroutine that will check for #2 and #3. It will check if the share is already mapped, and if not, map it to the next availible letter. It will then return the drive letter in a variable that you will then use to access the drive, as well as a flag you can use to see if the share was already mapped or not, so you can decide if you should delete the mapping or not.

Let me know if you need something like that and I'll dig it up.

HTH

Jerry


----------

