Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save olegrumiancev/239ba09f7b0834bd164bb06821a4f295 to your computer and use it in GitHub Desktop.
Save olegrumiancev/239ba09f7b0834bd164bb06821a4f295 to your computer and use it in GitHub Desktop.
PowerShell public directory listing download
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