
Permission Denied Error On objFile.Copy
Const strDriveToBackup = "N:"
' TO BACKUP ONLY A SPECIFIC FOLDER IN THE ABOVE DRIVE, PUT
THE FOLDER NAME ONLY HERE - OTHERWISE DO NOT CHANGE. FOR
INSTANCE,
' IF YOU WANT TO BACK UP A FOLDER NAMED "Files", THE
LINE BELOW SHOULD READ: Const strFolderToBackup = "Files"
Const strFolderToBackup = ""
' CHANGE THE DRIVE AND FOLDER BELOW TO WHICHEVER LOCATION
YOU WANT THE BACKUP TO SAVE TO
Const strBackupLocation = "C:\HOME-DRIVE-BACKUP"
' ****************************** DO NOT CHANGE ANYTHING
BELOW THIS LINE ******************************
Dim objFSO
Dim objDriveRoot
Dim objFile
Dim objFolder
Call doBackup
Public Sub doBackup
' Create a refernece to the file system
Set objFSO = CreateObject
("Scripting.FileSystemObject")
' Find out whether the user wants to backup a
specific folder, or the entire thing
If strFolderToBackup <> "" Then ' The user is
backing up a folder within the drive
' Make sure that the system can detect the
drive/folder to back up
If Not objFSO.FolderExists
(strDriveToBackup & "\" & strFolderToBackup) Then
MsgBox "The system cannot detect "
& strDriveToBackup & "\" & strFolderToBackup & ". The
backup cannot continue. Please check your network
settings.",vbExclamation,"Cannot Detect Drive"
Exit Sub
Else
Set objDriveRoot = objFSO.GetFolder
(strDriveToBackup & "\" & strFolderToBackup)
End If
Else ' The user is just backing up a drive
' Make sure that the system can detect the
drive to back up
If Not objFSO.DriveExists
(strDriveToBackup) Then
MsgBox "The system cannot detect
drive " & strDriveToBackup & ". The backup cannot
continue. Please check your network
settings.",vbExclamation,"Cannot Detect Drive"
Exit Sub
Else
Set objNetwork = objFSO.GetDrive
(strDriveToBackup)
Set objDriveRoot = objFSO.GetFolder
(objNetwork.Path)
End If
End If
' Now we'll verify that the destination folder
actually exists - if it doesn't, we'll create it.
On Error Resume Next
If Not objFSO.FolderExists(strBackupLocation) Then
objFSO.CreateFolder strBackupLocation
If Err.Number <> 0 Then ' An error occured
trying to create the destination folder
MsgBox "An error occurred trying
to create " & strBackupLocation & ". Reason: " &
Err.Description & ".",vbExclamation,"Could Not Create
Destination"
Exit Sub
End If
End If
On Error GoTo 0
On Error Resume Next
' Backup folders
For Each objFolder In objDriveRoot.SubFolders
If Not objFSO.FolderExists
(strBackupLocation & "/" & objFolder.Name) Then
objFSO.CreateFolder
strBackupLocation & "/" & objFolder.Name
End If
objFolder.Copy strBackupLocation & "/" &
objFolder.Name
If Err.Number <> 0 Then
MsgBox "Error copying " &
objFolder.Name & " to " & strBackupLocation & " for the
following reason: " & Err.Description & ". Backup will
continue.",,"Folder Could Not Copy"
Err.Clear
End If
Next
'
***********************************************************
*****************
' BELOW IS WHERE I GET THE ERROR - FOR EACH AND
EVERY FILE IN THE SOURCE DRIVE
'
***********************************************************
*****************
For Each objFile In objDriveRoot.Files
objFile.Copy strBackupLocation
If Err.Number <> 0 Then
MsgBox "Error copying " &
objFile.Name & " to " & strBackupLocation & " for the
following reason: " & Err.Description & ". Backup will
continue.",,"File Could Not Copy"
Err.Clear
End If
Next
Quote:
>-----Original Message-----
>Can you send the part of the code that implements the
logic?
>Thanks
>Sandeepan
>> I'm writing a script to copy multiple files and folders
>> from a mapped network drive to a local folder. Copying
the
>> folders works great, no problem, and all their sub
folders
>> and files carry over nicely.
>> However, for each and every file sitting at the top
level
>> of the source folder, I get a "Permission Denied" error
>> and the file does not copy. I checked security settings
>> and I do have full control over the file and the source
>> drive.
>> Any thoughts?
>.