Last active
February 23, 2018 16:46
-
-
Save olegrumiancev/239ba09f7b0834bd164bb06821a4f295 to your computer and use it in GitHub Desktop.
PowerShell public directory listing download
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Import-Module BitsTransfer | |
function Download-File($src, $dest) { | |
$job = Start-BitsTransfer -Source $src -Destination $dest -Priority Normal -Asynchronous | |
while (($job.JobState.ToString() -eq "Transferring") -or ($job.JobState.ToString() -eq "Connecting")) { | |
$percent = [Math]::Round(($job.BytesTransferred/$job.BytesTotal),2)*100 | |
Write-Progress -Activity "$($job.JobState)" -Status "$percent% Complete:" -PercentComplete $percent | |
Sleep 1 | |
} | |
Complete-BitsTransfer -BitsJob $job | |
} | |
Function Copy-Folder([string]$source, [string]$destination, [bool]$recursive) { | |
if (!$(Test-Path($destination))) { | |
New-Item $destination -type directory -Force | |
} | |
$r=iwr $source | |
$r.ParsedHtml.body.getElementsByTagName('A')|%{ | |
# Parse each line, looking for files and folders | |
$line = $_.outerHTML | |
if ($line.ToUpper().Contains("HREF")) { | |
# File or Folder | |
if (!$line.ToUpper().Contains("PARENT DIRECTORY") -and !$line.ToUpper().Contains("?")) { | |
$item = $_.pathname | |
Write-Host $item | |
if ($item.EndsWith("/")) { | |
# Folder | |
if ($recursive) { | |
# Subfolder copy required | |
Copy-Folder "$source$item/" "$destination$item/" $recursive | |
} | |
} else { | |
# File | |
Download-File "$source$item" "$destination$item" | |
} | |
} | |
} | |
} | |
} | |
# For both src and dest manke sure your path has trailing forward/backward slash (https://listing/uploads/ and c:\downloads\) | |
$src = "" | |
$dest = "" | |
$isRecursive = $true | |
Copy-Folder $src $dest $isRecursive |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment