Error message when the filesystemobject.copyfolder method encounters a read-only destination file: "Permission Denied"


You try to use the following code to copy the contents of a specific folder to a destination folder that contains a read-only file:
Dim fso, result, source, destination 
source= "C:\source_folder"
destination = "D:\destination_folder"
Set fso = CreateObject("Scripting.FileSystemObject")
result = fso.CopyFolder(source, destination, true)
Note In this code, source_folder represents the actual source folder name, and destination_folder represents the actual destination folder name.
When you run this code, you receive the following error message in the Windows Script Host dialog box:
Script: C:\folderName\test.vbs

Line: 3

Char: 1

Error: Permission Denied

Code: 800A0046

Source: Microsoft VBScript runtime error
This behavior is expected. Typically, you receive a "Permission Denied" error message when the filesystemobject.copyfolder method encounters a read-only destination file. However, you can drag a file to a folder that contains a read-only file of the same name. In this case, you receive the following message in Windows Explorer:
This folder already contains a read-only file named 'test.txt'

Would you like to replace the existing file:

with this one?



This issue occurs because of a limitation in the CopyFolder method. The CopyFolder method cannot overwrite a file in the destination folder if the file has the read-only attribute set. The CopyFile method has this same limitation.


To resolve this issue, use the XCOPY utility together with the /R switch to overwrite any read-only files. The following code example shows one way to use the XCOPY utility to do this.

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure. However, they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
Dim oShell, source, destination 
source = "C:\myFolder\*"
destination = "D:\myOtherFolder"
Set oShell = WScript.CreateObject("WScript.Shell")
oShell.Run "xcopy.exe " & source & " " & destination & " /R /Y"
Set oShell = Nothing
In this code example, the /R switch enables the overwriting of read-only files. The /Y switch suppresses the prompt to confirm that you want to overwrite the existing destination file.

  • The /Y switch may be preset in the COPYCMD environment variable. If you want the prompt to appear, you can use the /-Y version of the /Y switch. The /-Y version of the switch turns off the /Y switch that is preset in the COPYCMD environment variable.
  • The XCOPY utility is not available in Windows Vista. To resolve this issue in Windows Vista, use the Robocopy.exe file instead of the Xcopy.exe file in the code example.