powershell - 如何在 PowerShell 中从 Web 下载整个文件夹/子文件夹

标签 powershell download webclient

我可以使用以下方法从网络下载单个文件:

$wc = New-Object System.Net.WebClient
$wc.DownloadFile("http://blah/root/somefile.ext", "C:\Downloads\www\blah\root\somefile.ext")

但是如何下载所有文件,包括子文件夹?像下面这样的东西会很好......
$wc.DownloadFile("http://blah/root/", "C:\Downloads\www\blah\root\")

根文件夹本身在 IE 中显示为目录列表,您知道,例如:
[To Parent Directory]
                01 July 2012    09:00       1234 somefile.ext
                01 July 2012    09:01       1234 someotherfile.ext

作为奖励,我如何只下载根文件夹中的文件,而忽略子文件夹?

最佳答案

以下是我根据 Andy 的建议提出的建议(当然,在 Google 的大量帮助下):

####################################################################################################
# This function copies a folder (and optionally, its subfolders)
#
# When copying subfolders it calls itself recursively
#
# Requires WebClient object $webClient defined, e.g. $webClient = New-Object System.Net.WebClient
#
# Parameters:
#   $source      - The url of folder to copy, with trailing /, e.g. http://website/folder/structure/
#   $destination - The folder to copy $source to, with trailing \ e.g. D:\CopyOfStructure\
#   $recursive   - True if subfolders of $source are also to be copied or False to ignore subfolders
#   Return       - None
####################################################################################################
Function Copy-Folder([string]$source, [string]$destination, [bool]$recursive) {
    if (!$(Test-Path($destination))) {
        New-Item $destination -type directory -Force
    }

    # Get the file list from the web page
    $webString = $webClient.DownloadString($source)
    $lines = [Regex]::Split($webString, "<br>")
    # Parse each line, looking for files and folders
    foreach ($line in $lines) {
        if ($line.ToUpper().Contains("HREF")) {
            # File or Folder
            if (!$line.ToUpper().Contains("[TO PARENT DIRECTORY]")) {
                # Not Parent Folder entry
                $items =[Regex]::Split($line, """")
                $items = [Regex]::Split($items[2], "(>|<)")
                $item = $items[2]
                if ($line.ToLower().Contains("&lt;dir&gt")) {
                    # Folder
                    if ($recursive) {
                        # Subfolder copy required
                        Copy-Folder "$source$item/" "$destination$item/" $recursive
                    } else {
                        # Subfolder copy not required
                    }
                } else {
                    # File
                    $webClient.DownloadFile("$source$item", "$destination$item")
                }
            }
        }
    }
}

当然不能保证,但它适用于我感兴趣的网站

关于powershell - 如何在 PowerShell 中从 Web 下载整个文件夹/子文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11436694/

相关文章:

powershell - 如何在 ScriptBlock 中设置 $_?

networking - Windows 7 : Change the priority of traffic to wired instead of the wireless connection?

PHP:如何缩短网址+从数据库中获取数据

iOS - AFNetworking 在失败后恢复下载

javascript - Google 云端硬盘(网络)中的下载如何进行?

asp.net - 下载带有 "save as"的文件

c# - 从 Webclient 调用标签 javascript 点击函数

powershell - 为什么Powershell被执行2或4次?

c# - 从 https URL 下载文件时出现 WebClient 错误

powershell - 如何在PowerShell中使用Copy-Item覆盖文件