PowerShell -WebClient 下载文件通配符? [英] PowerShell -WebClient DownloadFile Wildcards?
问题描述
我想将多个文件从 SharePoint 库复制到本地目录.可以使用通配符吗?以下代码不起作用.但是有没有办法使用 WebClient 和通配符?(我必须使用 WebClient.不可能使用 SharePoint WebServices :-( )
I want to copy multiple files from a SharePoint libary to a local directory. It is possible to use Wildcards? The following code is not working. But is there a way to use the WebClient and Wildcards? (I must use the WebClient. It is not possible to use the SharePoint WebServices :-( )
$url = "http://mySharePoint/websites/Site/TestDocBib/*.jpg"
$path = "D: emp"
$WebClient = New-Object System.Net.WebClient
$WebClient.UseDefaultCredentials = $true
$WebClient.DownloadFile($url, $path)
推荐答案
您可以解析列表的 html.
you can parse though the html of the list.
# dummy url - i've added allitems.aspx
$url = "http://mySharePoint/websites/Site/TestDocBib/allitems.aspx"
$path = "D: emp"
$dl_file = $path + "allitems.html"
$WebClient = New-Object System.Net.WebClient
$WebClient.UseDefaultCredentials = $true
$WebClient.DownloadFile($url, $dl_file)
下载文件后,您可以解析该文件 - 一个快速的谷歌出现,Lee Holmes 已经完成了大部分工作:
once you've downloaded the file you can parse though the file - a quick google turned up that Lee Holmes had done most of it already:
http://www.leeholmes.com/blog/2005/09/05/unit-testing-in-powershell-%E2%80%93-a-link-parser/
你想要的主要部分是正则表达式:
the main bit you want is the regex:
$regex = "<s*as*[^>]*?hrefs*=s*[`"']*([^`"'>]+)[^>]*?>"
我非常快速地破解 - 这可能(或可能不)工作......但要点在那里:)
I very quick hack - that may (or may not) work... but the gist is there :)
$test = gc $dl_file
$t = [Regex]::Matches($test, $regex, "IgnoreCase")
$i = 0
foreach ($tt in $t) {
# this assumes absolute paths - you may need to add the hostname if the paths are relative
$url = $tt.Groups[1].Value.Trim()
$WebClient = New-Object System.Net.WebClient
$WebClient.UseDefaultCredentials = $true
$WebClient.DownloadFile($url, $($path + $i + ".jpg"))
$i = $i + 1
}
这篇关于PowerShell -WebClient 下载文件通配符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!