批处理文件拷贝到SharePoint网站 [英] Batch copy files to SharePoint site
问题描述
我搜索SO,SU和 SP.SE 一个解决方案,但无法找到我需要的东西。我在寻找这可能是一个脚本或其他一些非编码方法/工具的解决方案。
I searched SO, SU, and SP.SE for a solution, but could not find what I needed. I'm looking for a solution which may be a script or some other non-coding method/tool.
我试图写一个脚本(由他人使用)或一些其他形式的自动化自动上传各种报表到SharePoint网站。我设法得到以下(VBScript)的code的工作,但仅适用于基于文本的文件 - .CSV在这种情况下,虽然这也适用于.TXT等。
I am trying to write a script (to be used by others) or some other form of automation to upload various reports automatically to a SharePoint site. I have managed to get the following (VBScript) code to work, but only for text-based files -- .CSV in this case, though this also works for .TXT, etc.
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),这导致垃圾正在上传
This only works for text files because it pipes the text data into a file on the SP site. However, if I want to transfer any kind of binary file (.XLS, .PDF), this results in garbage being uploaded.
我想看看在 Shell.Application
==> .Namespace()
,但这似乎没有一个网址,但只有一个物理驱动器的工作。下面是一些还有什么我试过(修剪,以显示相关部分):
I tried to take a look at a Shell.Application
==> .Namespace()
, but this doesn't seem to work with a URL, but only a physical drive. Here's some of what else I tried (trimmed to show relevant pieces):
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://
无论是。我看了看<一个href=\"http://my.re-invent.com/helpdesk/KB/a222/how-do-i-copy-many-files-at-once-to-my-sharepoint-2003.aspx\"相对=nofollow>这个方法,这可能为我工作,但我preFER不处理映射/ NET USE
,因为公司拥有多个网络共享,映射为这取决于谁的登录而变化。
I also tried a batch file using xcopy, but that can't connect to the http://
either. I looked at this method, which may work for me, but I'd prefer not to deal with mapping/NET USE
, since our company has multiple network shares, the mapping for which varies depending on who's logged in.
由于没有这些工作很需要我的方式:有自动这种功能的方法
Since none of these work quite the way I need: Is there a method to automate this kind of functionality?
我有VBA / VBScript中的经验,所以无论是像上面的脚本,或一些内置的MS Office应用程序(如Outlook是最好的,但我可能可以适应任何我给出)是preferable。话虽这么说,我打开,让我做到这一点,在Windows或Office本地运行的任何方法。不过,我没有访问到Visual Studio,所以我不能使用任何.NET功能。
I have experience with VBA/VBscript, so either a script like the above, or something built in to an MS Office application (Outlook is best, but I can probably adapt whatever I am given) would be preferable. That being said, I am open to any method that would allow me to do this, running natively in Windows or Office. However, I do not have access to Visual Studio, so I can't use any .NET functionality.
推荐答案
感谢<一个href=\"http://stackoverflow.com/questions/12039730/batch-copy-files-to-sharepoint-site#comment16073151_12039730\">Sean 的柴郡在明显的答案,我没有看到指着我。发布相关code,因为我不相信这又存在于左右。
Thanks to Sean Cheshire for pointing me at the obvious answer that I did not see. Posting the relevant code, since I don't believe this yet exists on SO.
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 code,格式化大多使用VBScript工作,虽然我不能得到这个块正确地传输。作为VBA中,这可以通过分配的数据类型等来改善一些
This is VBA code, formatted to mostly work with VBScript, though I could not get this block to transfer properly. As VBA, this can be improved some by assigning data types, etc.
' Read the file into a byte array.
ReDim Lvarbin(LlFileLength)
Open PstrFullfileName For Binary As #1
Get #1, , Lvarbin
Close #1
这篇关于批处理文件拷贝到SharePoint网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!