Microsoft KB Archive/941190

= Permission Denied error returned when FileSystemObject.CopyFolder encounters a Read-Only Destination File =

Article ID: 941190

Article Last Modified on 8/6/2007

-

APPLIES TO


 * Microsoft Visual Basic, Scripting Edition 5.5

-



Source: Microsoft Support



RAPID PUBLISHING
RAPID PUBLISHING ARTICLES PROVIDE INFORMATION DIRECTLY FROM WITHIN THE MICROSOFT SUPPORT ORGANIZATION. THE INFORMATION CONTAINED HEREIN IS CREATED IN RESPONSE TO EMERGING OR UNIQUE TOPICS, OR IS INTENDED SUPPLEMENT OTHER KNOWLEDGE BASE INFORMATION.



Action
Attempt to use the following code to copy the contents of specific folders when the destination folder contains a Read Only file with the same name as one in the source folder:

Dim fso, result, source, destination

source= &quot;C:\myFolder&quot;

destination = &quot;D:\myOtherFolder&quot;

Set fso = CreateObject(&quot;Scripting.FileSystemObject&quot;)

result = fso.CopyFolder(source, destination, true)



Result
A Windows Script Host dialog box displays the following error message:

Script: C:\folderName\test.vbs

Line: 3

Char: 1

Error: Permission Denied

Code: 800A0046

Source: Microsoft VBScript runtime error

However, the user is able to drag a file to a folder containing a ReadOnly file of the same name. In this case, Windows Explorer displays a dialog box with the following message:

This folder already contains a read-only file named 'test.txt'

Would you like to replace the existing file:

with this one?

If the user clicks &quot;Yes&quot; than Explorer switches the Read-Only attribute to Normal and completes the copy.



Cause
This is due to a limitation in the CopyFolder method. The documentation for the associated CopyFile method states the following for the overwrite parameter:

Note that CopyFile will fail if destination has the read-only attribute set, regardless of the value of overwrite.



Resolution
use the XCOPY utility with the /R switch to overwrite any ReadOnly files:

Dim oShell, source, destination

source = &quot;C:\myFolder\*&quot;

destination = &quot;D:\myOtherFolder&quot;

Set oShell = WScript.CreateObject(&quot;WScript.Shell&quot;)

oShell.Run &quot;XCOPY.EXE source destination /R /Y&quot;

Set oShell = Nothing

In the above snippet the /R switch allows the overwrite of ReadOnly files and the /Y switch suppresses the prompt to confirm that you want to overwrite the existing destination file. Note that the /Y switch may be preset in the COPYCMD environment variable in which case you can force the prompt to display with the /-Y switch.



MoreInformation
Please note that XCOPY is to be deprecated with the release of Microsoft Vista; please use Robocopy.exe in place of XCOPY.EXE.



DISCLAIMER
MICROSOFT CORPORATION AND/OR ITS RESPECTIVE SUPPLIERS MAKE NO REPRESENTATIONS ABOUT THE SUITABILITY, RELIABILITY, OR ACCURACY OF THE INFORMATION AND RELATED GRAPHICS CONTAINED HEREIN. ALL SUCH INFORMATION AND RELATED GRAPHICS ARE PROVIDED &quot;AS IS&quot; WITHOUT WARRANTY OF ANY KIND. MICROSOFT AND/OR ITS RESPECTIVE SUPPLIERS HEREBY DISCLAIM ALL WARRANTIES AND CONDITIONS WITH REGARD TO THIS INFORMATION AND RELATED GRAPHICS, INCLUDING ALL IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, WORKMANLIKE EFFORT, TITLE AND NON-INFRINGEMENT. YOU SPECIFICALLY AGREE THAT IN NO EVENT SHALL MICROSOFT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, PUNITIVE, INCIDENTAL, SPECIAL, CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF USE, DATA OR PROFITS, ARISING OUT OF OR IN ANY WAY CONNECTED WITH THE USE OF OR INABILITY TO USE THE INFORMATION AND RELATED GRAPHICS CONTAINED HEREIN, WHETHER BASED ON CONTRACT, TORT, NEGLIGENCE, STRICT LIABILITY OR OTHERWISE, EVEN IF MICROSOFT OR ANY OF ITS SUPPLIERS HAS BEEN ADVISED OF THE POSSIBILITY OF DAMAGES.

Keywords: kbnomt kbrapidpub KB941190

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.