PowerShell -WebClient DownloadFile Wildcards?

后端 未结 3 815
说谎
说谎 2021-01-22 12:11

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 We

3条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-22 12:47

    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:\temp\"
    $dl_file = $path + "allitems.html"
    
    $WebClient = New-Object System.Net.WebClient
    $WebClient.UseDefaultCredentials = $true
    $WebClient.DownloadFile($url, $dl_file)
    

    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*a\s*[^>]*?href\s*=\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
    }
    

提交回复
热议问题