0

I am trying to copy a file to a network location with a batch file:

net use F: \\my\destination\folder /user:myuser password
copy C:\Users\myUser\Desktop\toCopy.txt F:

Works like a charm. However, when I leave out the drive letter, I get an "Access Denied" error.

net use \\my\destination\folder /user:myuser password
copy C:\Users\myUser\Desktop\toCopy.txt \\my\destination\folder

I don't want to use a hardcoded drive letter - what can I do ?

Tim
  • 3,910
  • 8
  • 45
  • 80

1 Answers1

1

copy command does not support UNC paths (by default but it can be changed). You have two options: use robocopy or change current directory to remote path using pushd (which creates a temporary drive letter for you, removed by popd), see also ...UNC path...without mapping it to a network drive?. Assuming you want to avoid virtual drives (otherwise your own solution works pretty well too):

robocopy "C:\Users\myUser\Desktop" "\\my\destination\folder" "tocopy.txt" 

Note that robocopy has this syntax:

robocopy source destination [file [file]...] [options]

Then you first need to specify source and destination directories and then files to copy (wildcards allowed).

Community
  • 1
  • 1
Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208
  • Thanks for the quick reply. Two things: Robocopy also tells me Access Denied, and I just spoke with the client who uses it and she told me that it used to work without drive letter, with the normal "copy" command. I am puzzled. – Tim Oct 06 '15 at 15:32
  • @Tim it _may_ work also with copy (see link at very beginning of my answer). Be sure to specify a valid network path and...credentials. If it works with drive letter then it'll also work with UNC and robocopy (or _modified_ copy). – Adriano Repetti Oct 06 '15 at 15:42
  • Pushd also gives me an access denied. I guess I'll have to stick to drive letters – Tim Oct 07 '15 at 06:06
  • I still don't understand why it works when a drive letter is attributed. The credentials are the same – Tim Oct 07 '15 at 08:04