2016-07-15 82 views
0

我有一個包含多個網址的文件這樣的文件下載的文件的文件名:閱讀使用PowerShell

http://ligman.me/1HCDxl9 
http://ligman.me/1HCCCRP 
http://ligman.me/1HCCCRP 
http://ligman.me/1H4Q0e5 
http://ligman.me/1H4Q0e5 
http://ligman.me/1JI6V77 
http://ligman.me/1JI6V77 
http://ligman.me/1CSMobd 
http://ligman.me/1CSMobd 

我想寫一個PowerShell腳本將逐行讀取該文件中的行,然後下載每行後面的文件(URL)。到目前爲止,我已經設法使用以下腳本下載文件:

$reader = [System.IO.File]::ReadLines("C:\Temp\Ebooks\ebooks.txt") | Where-Object { $_ -ne '' } 
$targetDir = "C:\Temp\Ebooks\" 
$wc = New-Object System.Net.WebClient 

foreach($file in $reader) {  
    $sourceFileName = $file.SubString($file.LastIndexOf('/')+1) + ".pdf" 
    $targetFileName = $targetDir + $sourceFileName 
    $wc.DownloadFile($file, $targetFileName) 
    Write-Host "Downloaded $file successfully to directory $targetDir" 
} 

我的問題是文件名。現在,我只能將它們保存爲PDF格式,但有時文件不是PDF文件,而是DOCX或XLSX。另外,如果它們不被命名爲1225DID或13DChwr,那將會很好。基本上,我仍然需要讀取實際的文件名,然後用該名稱保存下載的文件。

我該怎麼做?

編輯:這是工作得到實際的文件名,但當我嘗試打開文件時,我得到一個錯誤,他們不是PDF或已損壞(只要我嘗試用福昕閱讀器打開PDF文件)

$reader = [System.IO.File]::ReadLines("C:\Temp\Ebooks\ebooks.txt") | Where-Object { $_ -ne '' } 
$targetDir = "C:\Temp\Ebooks\" 
$wc = New-Object System.Net.WebClient 

$reader | %{  
    $uri = $_ 
    $request = Invoke-WebRequest -Uri $uri -MaximumRedirection 0 -ErrorAction Ignore 

    $sourceFileName = $request.Headers.Location.SubString($request.Headers.Location.LastIndexOf('/') + 1) 
    $targetFileName = $targetDir + $sourceFileName 
    $wc.DownloadFile($file, $targetFileName) 
    Write-Host "Downloaded $file successfully to directory $targetDir" 
} 
+0

您能分享您試圖目標實際的URL之一,之類的東西的例子具有相同的格式? – Bassie

+0

確定我編輯爲網址,它們應該現在有效 – LeonidasFett

+0

我已經嘗試閱讀這些文件的內容處置,但它似乎只有「附件」作爲值,沒有別的。 – LeonidasFett

回答

2

使用小提琴手,似乎在場景後面有一個重定向。

如果您執行以下腳本,則會在您提供的URL後面顯示「真實」URL。

$links = @(
    "http://ligman.me/1HCDxl9", 
    "http://ligman.me/1HCCCRP", 
    "http://ligman.me/1H4Q0e5", 
    "http://ligman.me/1JI6V77", 
    "http://ligman.me/1CSMobd" 
) 

$links | %{ 
    $uri = $_ 

    $request = Invoke-WebRequest -Uri $uri -MaximumRedirection 0 -ErrorAction Ignore 
    Write-Host $request.Headers.Location 
} 

此腳本生成以下列表,其中包含文檔名稱及其擴展名。

http://download.microsoft.com/download/4/2/f/42f9b256-977e-4792-a9eb-d490516d4468/AF103733558_en-us_access2013quickstartguide.pdf 
http://download.microsoft.com/download/6/7/5/675609de-a32b-44d4-ace6-86305afb808f/AF103733448_en-us_word2013quickstartguide.pdf 
http://download.microsoft.com/download/2/8/7/28747b20-70b0-4003-b82a-5ab0d222bbd6/AF103733495_en-us_publisher2013quickstartguide.pdf 
http://download.microsoft.com/download/e/6/f/e6fc74dc-9f0d-4e6c-bbcc-6855e4d7a78c/AF103733479_en-us_project2013quickstartguide.pdf 
http://download.microsoft.com/download/c/e/b/ceb742d6-bc1f-4447-ad06-b0842338dd8c/AF103733547_en-us_onenote2013quickstartguide.pdf 

這裏是一個版本,下載爲我的作品文件:

$links | %{ 
    $uri = $_ 

    $request = Invoke-WebRequest -Uri $uri -MaximumRedirection 0 -ErrorAction Ignore 
    $location = $request.Headers.Location 
    $output = "D:\temp\" + $location.SubString($location.LastIndexOf('/') + 1) 

    Invoke-WebRequest -Uri $location -OutFile $output 
} 
+0

這是完美的,我現在有實際的文件名。但是,打開文件時出現錯誤,例如PDF文件損壞。我試圖打開哪一個並不重要,它總是一樣的。請參閱我的問題以獲取更新的代碼。 – LeonidasFett

+0

編輯答案給出一個下載示例。 –