4

We have a bunch of zip files hosted on an FTP server, which are also accessible via HTTP. I would love to do something like (gci http://test.com/test/ *.zip ) and give me all the zip files which exist on the webserver.

Does anyone know of a way to do such a thing in a clean way?

TIA

4
  • You could hack it together if the http://test.com/test had directory indexes enabled but there is no "clean" way of doing it.
    – arco444
    Jan 14, 2015 at 14:15
  • if it´s a ftp server why not use the ftp protocol? you should be able to get a directory listing via ftp from powershell
    – Paul
    Jan 14, 2015 at 14:20
  • I don't want to have the credentials as part of my script if i can help it.
    – mumbles
    Jan 14, 2015 at 14:30
  • Seems FTP will indeed be the only way i can do it. Thx.
    – mumbles
    Jan 14, 2015 at 14:47

1 Answer 1

8

this is quite easy with invoke-webrequest (PS V3)

$r=iwr http://asite.com/test2/ -UseBasicParsing  
$r.Links |?{$_.href -match ".zip"} 

of course as +arco444 states, the directory index must be enable directory listing


Edit To get the last modified file, you will have to parse the HTML, here is an example (the regex will have to be addapted to your config) :

$col=@()
$link,$date,$size=""
$r=iwr http://asite.com/test2/ 
$r.ParsedHtml.body.getElementsByTagName('TR')|%{ 
    $_.getElementsByTagName('TD') |select -expand innerHTML |%{     
        switch -regex ($_){
            "(.)*zip"{ $link = $_;break}
            "\d{2}-...-\d{4}(.)*"{$date=$_;break}
              "^\d*[KM]"    {$size=$_;break }       
            default{}
        }

    }
        if( $link -and $date -and $size){
        $o=new-object -typename psobject |select  -property "link","date","size"
        $o.link=$link
        $o.date=$date
        $o.size=$size

        $col+=$o
        }
    } 
    $col |select -unique "link","date","size" |sort -desc date |select -last 1
7
  • But what if there isn't a page with links? Just the files?
    – mumbles
    Jan 14, 2015 at 14:29
  • Thx, turns out it's not accessible via http. Damn.
    – mumbles
    Jan 14, 2015 at 14:48
  • Kayasazx - got it exposed to HTTP. How do you just list the files without the url in your example? I'll give you the answer. thx
    – mumbles
    Jan 14, 2015 at 15:10
  • nevermind, found it: $r.Links.innerHTML |?{$_ -match ".zip" } | Select-Object -Last 1
    – mumbles
    Jan 14, 2015 at 15:51
  • You should use the sort-object before the select if you want the newest file Jan 14, 2015 at 16:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.