views:

141

answers:

1

I'm running a VBScript in SQL Agent but I get a 'Permission Denied' on line 34 (the first copy attempt). I've run this script outside SQL Agent with no problems

FYI: The 'X:\' drive is mapped to a SharePoint folder. This may be the culprit.

Function Main()
Const SourceDrive As String = "X:\"
Dim fso
Dim Today
Dim FileName
Dim FromFile
Dim FromDrive
Dim ArchivePath

Set fso = CreateObject("Scripting.FileSystemObject")

Today = Format(Now, "yyyyMMdd")

'To add more sources just add them to the array list
Dim Sources() As Variant
Sources() = Array("Item1", _
                  "Item2")

'To add more targets just add them to the array list
Dim Targets() As Variant
Targets() = Array("C:\Users\myalias\Desktop\MyToFolder", _
                  "C:\Users\myalias\Desktop\MyToFolder2")


For i = 0 To UBound(Sources)
    FileName = "WebSurveyAlertCallbacks_" & Sources(i) & "_" & Today & ".xls"
    FromDrive = fso.BuildPath(SourceDrive, Sources(i))
    FromFile = fso.BuildPath(FromDrive, FileName)
    ArchivePath = fso.BuildPath(FromDrive, "Archive")
    If fso.FileExists(FromFile) Then
        For t = 0 To UBound(Targets)
            fso.CopyFile FromFile, fso.BuildPath(Targets(t), FileName), True
        Next
        fso.CopyFile FromFile, fso.BuildPath(ArchivePath, FileName), True
        fso.DeleteFile FromFile
    End If
Next

Set fso = Nothing

Main = DTSTaskExecResult_Success
End Function
+1  A: 

The agent probably runs under a different user-account (i.e. not you) and then doesn't have permissions to the files/folders you're using.

When you run it outside, it uses your logged on user's permissions and executes fine.

Aren
I would tend to agree with this answer but the agent is logged in with the same user as the one I'm using.
Chris