44

The environment is Windows Web Server 2008, and I'm just trying to copy a folder full of files to a network location (a managed backup network folder). Here is my .bat file, running in the folder with my .bat files:

copy *.bak \\networklocation\*.bak
pause

however \\networklocation requires username x and password y, so running the script gives:

Logon failure: unknown user name or bad password.

I can't figure out a way to supply my credentials. I tried creating a scheduled task and modifying the security options, but this only seems to allow you to use credentials relevant to the machine (i.e. I try putting in username x and it can't find it).

How do I run this script with all the right permissions?

Solution:

net use \\networklocation\sharefolder password /USER:username
copy \*.bak \\networklocation\sharefolder\*.bak
Anthony Mastrean
  • 21,850
  • 21
  • 110
  • 188
Zac
  • 1,722
  • 1
  • 19
  • 22
  • If you don't want to store credentials in batch file, then you should login to the remote folder and check option 'store password'. So copy command will work until restart of one of the connected computers. – Dracontis Apr 11 '16 at 08:10
  • 1
    `net use` fails on the second and subsequent calls with: *The local device name is already in use*, because its effect persists between calls even with `/PERSISTENT:NO`. It is therefore wrong to include this command into the script, or one must somehow remove the network drive at the end. What to do? – Anton Shepelev Apr 12 '19 at 12:34

2 Answers2

51

Try using the net use command in your script to map the share first, because you can provide it credentials. Then, your copy command should use those credentials.

net use \\<network-location>\<some-share> password /USER:username

Don't leave a trailing \ at the end of the

starlocke
  • 3,407
  • 2
  • 25
  • 38
Max
  • 19,654
  • 13
  • 84
  • 122
  • 1
    So I tried adding this before my copy command, and I get "The network path was not found". If I type \\networklocation\ into explorer it works and brings up the password box, so I know the location is correct. I'm actually using the ip address too (\\x.x.x.x\\) so I'd really have thought it shouldn't have a problem with the location. – Zac Oct 04 '10 at 10:33
  • 1
    Try to use some folder in the network path. \\networklocation\folder – Max Oct 04 '10 at 10:43
  • Thanks! Needed a folder specifying. Makes plenty of sense actually, I guess you can't use the root because it's not really a root, is it? Just somewhere that has some shared folders within it. – Zac Oct 04 '10 at 11:17
  • 5
    Just as an info, on Windows XP I got an error with this script but it works great if you don't include the backslash after the directory name. Thank you! – mavrosxristoforos Sep 02 '13 at 05:34
6

You can also map the share to a local drive as follows:

net use X: "\\servername\share" /user:morgan password
Zac
  • 1,722
  • 1
  • 19
  • 22