批处理文件拷贝到SharePoint网站(Batch copy files to SharePoin

2019-07-31 02:37发布

我搜索SO,SU和SP.SE一个解决方案,但无法找到我需要的东西。 我在寻找这可能是一个脚本其他一些非编码方法/工具的解决方案。

我试图写一个脚本(被他人使用)或一些其他形式的自动化自动上传各种报表到SharePoint站点。 我设法得到以下(VBScript)代码的工作,但仅适用于基于文本的文件 - .CSV在这种情况下,虽然这也适用于.TXT等。

Option Explicit

Dim sCurPath
sCurPath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")
UploadAllToSP sCurPath

Sub UploadAllToSP(sFolder)
    Dim fso, folder, fil
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set folder = fso.GetFolder(sFolder)
    For Each fil In folder.Files
        If fso.GetExtensionName(fil) = "csv" Then
            UploadFileToSP fil
        End If
    Next
End Sub

Sub UploadFileToSP(ofile)
    Dim xmlhttp
    Dim sharepointUrl
    Dim sharepointFileName
    Dim tsIn
    Dim sBody

    Set tsIn = ofile.openAsTextstream
    sBody = tsIn.readAll
    tsIn.close
    sharepointUrl = "http://SHAREPOINT URL HERE"

    sharepointFileName = sharepointUrl & ofile.name
    set xmlHttp = createobject("MSXML2.XMLHTTP.4.0")
    xmlhttp.open "PUT", sharepointFileName, false
    xmlhttp.send sBody
    If xmlhttp.status < 200 Or xmlhttp.status > 201 Then
        wscript.echo "There was a problem uploading " & ofile.name & "!"
    End If
End Sub

这仅适用于文本文件,因为它管的文本数据转换成的SP网站的文件。 但是,如果我要转移任何类型的二进制文件(.XLS,.PDF),这导致垃圾正在上传。

我想看看一个Shell.Application ==> .Namespace()但是这似乎并不具有URL,但只有一个物理驱动器的工作。 下面是一些的还有什么我试过(修剪,以显示相关件):

Set oApp = CreateObject("Shell.Application")

If oApp.NameSpace(sharepointUrl) <> Null then ' Always Null!
    ' Copy here
    ' Some lines omitted
    oApp.NameSpace(sharepointUrl).CopyHere ofile.Name ' This also fails when not surrounded by the Null check
Else
    MsgBox "SharePoint directory not found!"
End If

我也尝试使用XCOPY一个批处理文件,但不能连接到http://两种。 我看着这个方法 ,它可以为我工作,但我不希望处理映射/ NET USE ,因为我们的公司有多个网络共享,取决于谁是登录该映射。

由于没有这些工作很需要我的方式: 有自动这种功能的方法?

我有VBA / VBscript的经验,所以无论是像上面的脚本,或者说建立在一个MS Office应用程序(如Outlook是最好的,但我可能可以适应任何我给出)将是可取的。 话虽这么说,我打开,让我做到这一点,在Windows或Office本地运行任何方法。 不过,我没有访问到Visual Studio,所以我不能使用任何.NET功能。

Answer 1:

感谢肖恩柴郡在明显的答案,我并没有看到我指点。 发布相关的代码,因为我不相信这又是那么存在于。

Sub UploadFilesToSP(sFolder)

Dim sharepointUrl
Dim sharepointFileName
Dim LlFileLength
Dim Lvarbin()
Dim LobjXML
Dim LvarBinData
Dim PstrFullfileName
Dim PstrTargetURL
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
Dim fldr
Dim f

    'This has not been successfully tested using an "https" connection.
    sharepointUrl = "http://SHAREPOINT URL HERE"
    Set LobjXML = CreateObject("Microsoft.XMLHTTP")
    Set fldr = fso.GetFolder(sFolder)

    For Each f In fldr.Files
        sharepointFileName = sharepointUrl & f.Name

        PstrFullfileName = sFolder & f.Name
        LlFileLength = FileLen(PstrFullfileName) - 1

        ' Read the file into a byte array.
        ReDim Lvarbin(LlFileLength)
        Open PstrFullfileName For Binary As #1
        Get #1, , Lvarbin
        Close #1

        ' Convert to variant to PUT.
        LvarBinData = Lvarbin
        PstrTargetURL = sharepointFileName 

        ' Put the data to the server, false means synchronous.
        LobjXML.Open "PUT", PstrTargetURL, False

        ' Send the file in.
        LobjXML.Send LvarBinData
    Next f

Set LobjXML = Nothing
Set fso = Nothing

End Sub

这是VBA代码,格式化大多使用VBScript工作,虽然我不能得到这个块正确地传输。 如VBA,这可以通过指定数据类型等来改善一些

' Read the file into a byte array.
ReDim Lvarbin(LlFileLength)
Open PstrFullfileName For Binary As #1
Get #1, , Lvarbin
Close #1


Answer 2:

这是一个很老的文章,但一个非常有用的一个,这样感谢大家的贡献。 这是我的版本与早期绑定。 我发现,以前的发布并没有工作,由于VBA假设的无声明的变量类型。

 Private Sub cmdUploadToApplicationsAndApprovals_Click() Dim strSharePointUrl As String Dim strSharePointFileName As String Dim lngFileLength As Long Dim bytBinary() As Byte Dim objXML As XMLHTTP Dim varBinData As Variant Dim strFullfileName As String Dim strTargetURL As String Dim fso As FileSystemObject Set fso = New FileSystemObject Dim folder As folder Dim file As file Dim strFolder As String strFolder = CurrentProject.Path & "\Upload\" 'This has not been successfully tested using an "https" connection. strSharePointUrl = "http://sps.mysite.ca/subsite/DocLib/" Set objXML = New XMLHTTP 'CreateObject("Microsoft.XMLHTTP") Set folder = fso.GetFolder(strFolder) For Each file In folder.Files strSharePointFileName = strSharePointUrl & file.Name strFullfileName = strFolder & file.Name lngFileLength = FileLen(strFullfileName) - 1 'Read the file into a byte array. ReDim bytBinary(lngFileLength) Open strFullfileName For Binary As #1 Get #1, , bytBinary Close #1 'Convert to variant to PUT. varBinData = bytBinary strTargetURL = strSharePointFileName 'Put the data to the server, false means synchronous. objXML.Open "PUT", strTargetURL, False 'Send the file in. objXML.Send varBinData 'Now Update the metadata Next file 'Clean up Set objXML = Nothing Set fso = Nothing MsgBox "Done" End Sub 

FYI上面的代码所需的2点的引用。 1.微软XML,v6.0的2. Microsoft脚本运行时

希望这有助于提高对已经辉煌的答案!



文章来源: Batch copy files to SharePoint site