Script Tips

Copy Files Remotely

From the reader mailbag: How to copy files to a remote computer using the CopyFile method.

For the next several columns, I’ll literally be delivering your answers as I catch up on my e-mail and forum posts from ScriptingAnswers.com:

First up is Paul, who wants to know if there’s a way to copy a file from his local computer to a remote computer. The remote computer has an admin share (C$), which Paul says he has access to. He mentions that he’s aware of the FileSystemObject’s CopyFile method, but wants to copy the file remotely.

Ironically, Paul, you’re sitting right on the answer. Although the docs don’t really go into it, you can use a UNC nearly anywhere in the FileSystemObject. The trick with CopyFile (and its cousin, MoveFile) is to provide the complete path, including the filename, for the destination:

Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile "C:\local.txt", _
    "\\RemotePC\C$\Folder\file.txt"

You can actually use UNCs nearly anywhere in scripting that a regular file path is accepted. That’s because the path -- whether local or a UNC -- is just passed to Windows, which knows perfectly well what to do with a UNC when it sees one.

About the Author

Don Jones is a multiple-year recipient of Microsoft’s MVP Award, and is Curriculum Director for IT Pro Content for video training company Pluralsight. Don is also a co-founder and President of PowerShell.org, a community dedicated to Microsoft’s Windows PowerShell technology. Don has more than two decades of experience in the IT industry, and specializes in the Microsoft business technology platform. He’s the author of more than 50 technology books, an accomplished IT journalist, and a sought-after speaker and instructor at conferences worldwide. Reach Don on Twitter at @concentratedDon, or on Facebook at Facebook.com/ConcentratedDon.

comments powered by Disqus
Most   Popular

Upcoming Training Events