خرید بک لینک

Vote count: 0

I like to write a powershell script to download all files and subfolders from my ftp server. I found a script to download all files from one specific folder, but I also like to download the subfolders and their files.

    #FTP Server Information - SET VARIABLES
    $ftp = "ftp://ftp.abc.ch/" 
    $user = 'abc' 
    $pass = 'abc'
    $folder = '/'
    $target = "C:LocalDataPowershell"

    #SET CREDENTIALS
    $credentials = new-object System.Net.NetworkCredential($user, $pass)

    function Get-FtpDir ($url,$credentials) {
        $request = [Net.WebRequest]::Create($url)
        $request.Method = [System.Net.WebRequestMethods+FTP]::ListDirectory
        if ($credentials) { $request.Credentials = $credentials }
        $response = $request.GetResponse()
        $reader = New-Object IO.StreamReader $response.GetResponseStream() 
        $reader.ReadToEnd()
        $reader.Close()
        $response.Close()
    }

    #SET FOLDER PATH
    $folderPath= $ftp + "/" + $folder + "/"

    $Allfiles=Get-FTPDir -url $folderPath -credentials $credentials
    $files = ($Allfiles -split "`r`n")

    $files 

    $webclient = New-Object System.Net.WebClient 
    $webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass) 
    $counter = 0
    foreach ($file in ($files | where {$_ -like "*.*"})){
        $source=$folderPath + $file  
        $destination = $target + $file 
        $webclient.DownloadFile($source, $target+$file)

        #PRINT FILE NAME AND COUNTER
        $counter++
        $counter
        $source
    }

Thanks for your help (:

asked 1 min ago

برچسب: نویسنده: استخدام کار تاريخ: شنبه 18 ارديبهشت 1395 ساعت: 3:16

صفحه بندی