Batch to copy and replace a txt file from one server to another

Sunny picture Sunny · Oct 21, 2013 · Viewed 16.9k times · Source

I have two servers, server1 and server2 on same network but require username and password to be mapped. server1 has a text file as C:\Users\output.txt. I want to create and schedule a batch script on server1, which should copy and replace output.txt file from server1 to server2 at path E:\data\output.txt on daily basis. I don't want to map server2 manually every time I start my computer nor do I want to enter my username and password each time.

I am using following commands in a batch, but not working;

net use C: \\server2\E:\data server2password /user:server2domain\server2username /savecred /p:yes
xcopy C:\Users\output.txt E:\data\

Answer

foxidrive picture foxidrive · Oct 21, 2013

If the data folder is shared then you specify it like this:

\\server2\data

In your net use command you will usually have to pick a drive letter that is not in use, as C: is commonly already the system drive.

So something like this may work - if the other switches in the net use command are correct.

net use z: \\server2\data server2password /user:server2domain\server2username /savecred /p:yes
xcopy "C:\Users\output.txt" z:\