0
votes

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 ?

1

1 Answers

1
votes

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).