We help IT Professionals succeed at work.

How to copy and over-write read only files

HSA10036
HSA10036 used Ask the Experts™
on
I want to copy read only files located on my computer to the server. Files from my computer will replace files with the same name located on the server called Fin-ibmserver. The files are read only on the server as well as on my computer.

I researched available solutions and came up with this:

Set objFSO = CreateObject ("Scripting.FileSystemObject")
strFromFolder = "C:\Documents and Settings\hsa\Desktop\Audit\QB-RemovedPW"
strToFolder = "\\Fin-ibmserver\QBField"

'Change attributes on ToFolder
For Each strFile In objFSO.GetFolder("\\Fin-ibmserver\QBField").Files
  Set objFile = objFSO.GetFile(strFile)
  If objFile.Attributes = objFile.Attributes AND 1 Then
      objFile.Attributes = objFile.Attributes XOR 1
  End if
Next

'Change attributes on FromFolder and then Copy
For Each strFile In objFSO.GetFolder("C:\Documents and Settings\hsa\Desktop\Audit\QB-RemovedPW").Files
  Set objFile = objFSO.GetFile(strFile)
  If objFile.Attributes = objFile.Attributes AND 1 Then
      objFile.Attributes = objFile.Attributes XOR 1
  End if
objFile.Copy strToFolder, True      
Next

The error message I get is that permission is denied when executing objFile.Copy
I can drag and drop the file manually without an error message.
Your help is greatly appreciated. Please keep it simple since I am not a programmer.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Expert of the Quarter 2010
Expert of the Year 2010
Commented:
'Change attributes on FromFolder and then Copy
For Each strFile In objFSO.GetFolder("C:\Documents and Settings\hsa\Desktop\Audit\QB-RemovedPW").Files
  Set objFile = objFSO.GetFile(strFile)
  If objFile.Attributes = objFile.Attributes AND 1 Then
      objFile.Attributes = objFile.Attributes XOR 1
  End if
If objFSO.FileExists(strToFolder & "\" & objFile.Name) Then
  objFSO.DeleteFile(strToFolder & "\" & objFile.Name, True)
End If
objFile.Copy strToFolder, True      
Next

Author

Commented:
Thank you, I will try your solution when I get back to work tomorrow.  

With the code I had, I noticed that the attributes on the files in the To folder never changed and remained Read Only. Even when I changed the attribute manually on the file, it went right back to 'Read Only'. The files are in a shared folder on the server, does that have anything to do with it? I made sure that the security and permission levels were set to allow full control.
But even the attributes on the files in the From folder, which resides on the local computer, also they did not change attributes, except for the first file, and the rest remained Read Only.

Author

Commented:
I tested your code and a couple of things;
objFSO.DeleteFile(strToFolder & "\" & objFile.Name, True)
should be objFSO.DeleteFile(strToFolder & "\" & objFile.Name), True

The change of attributes from Read Only never took place. Your lines of code seem to do the trick, despite the files being Read Only.

I got Access denied using the objFile.Copy strToFolder, True
Instead I used objFSO.CopyFile

I reduced the code to the following:

Set objFSO = CreateObject ("Scripting.FileSystemObject")
strFromFolder = "C:\Documents and Settings\hsa\Desktop\Audit\QB-RemovedPW"
strToFolder = "\\Fin-ibmserver\QBField"

'Delet files in ToFolder that exist in FromFolder. FSO cannot write over read only files, but it can delete them.
For Each strFile In objFSO.GetFolder("C:\Documents and Settings\hsa\Desktop\Audit\QB-RemovedPW").Files
  Set objFile = objFSO.GetFile(strFile)
 
If objFSO.FileExists(strToFolder & "\" & objFile.Name) Then
  objFSO.DeleteFile(strToFolder & "\" & objFile.Name), True
End If
Next

'Copy now the read only files. On error resume next because there is an error notification to be bypassed
On error resume next      
'For Each strFile In objFSO.GetFolder("C:\Documents and Settings\hsa\Desktop\Audit\QB-RemovedPW").Files
 For Each strFile In objFSO.GetFolder(strFromFolder).Files
Set objFile = objFSO.GetFile(strFile)
  objFSO.CopyFile "C:\Documents and Settings\hsa\Desktop\Audit\QB-RemovedPW\*.qbw" , "\\Fin-ibmserver\QBField\"      
Next

It seems to do the trick for now. Your input pointed me in the right direction.

Author

Commented:
The solution did not copy the files, only delete them. Furthermore part of my initial proposed code proved to be redundant