Download FTP Files

This topic contains 4 replies, has 3 voices, and was last updated by  Jonathan Tyler 4 years, 3 months ago.

  • Author
    Posts
  • #4072

    Dust
    Participant

    I need help with a script that downloads files from an FTP site using credentials. Everything works until I get to the DownloadFile method for the .NET.webclient. It throws an Exception: Illegal characters in path. error and I can't figure out if it's the source or the target and I've renamed the folders and files to eliminate spaces and dashes but I still get the same error.

    Here's my script:

    Function Get-FTPFiles{
    
    [CmdLetBinding()]
    Param(
    [System.Uri]$uri='ftp://ftpsite/folder/subfolder/',
    [string]$userid='username',
    [string]$password='password'
    )
    
    $ftp=[system.net.ftpwebrequest]::Create($uri)
    if($userid){$ftp.Credentials=New-Object System.Net.NetworkCredential($userid,$password)}
    $ftp.Method=[system.net.webrequestmethods+ftp]::ListDirectoryDetails
    $response=$ftp.GetResponse()
    $strm=$response.GetResponseStream()
    $reader=New-Object System.IO.StreamReader($strm,'UTF-8')
    $list=$reader.ReadToEnd()
    $list
    $lines=$list.Split("`n")
    foreach($line in $lines){
    if($line){
    $f=$line.Split(' ',[System.StringSplitOptions]::RemoveEmptyEntries)
    $names = [string] "$($f[8]) $($f[9]) $($f[10]) $($f[11]) $($f[12]) $($f[13]) $($f[14])"
    $dt=[datetime]"$($f[5]) $($f[6]) $($f[7])"
    $length=$f[4]
    $hash=@{Name=$names;FileDate=$dt;Length=$length;}
    New-Object PSObject -Property $hash
    }
    
    foreach ($name in $names){
    $name
    $source = "$uri"+$name
    $source
    $target = '\\server\folder\subfolder\sub\sub2\'+$name
    $target
    $wc = new-object system.net.webclient
    
    $uri2 = new-object system.uri($source)
    
    #Download file from FTP server
    Try{
    #$wc.Credentials = new-object System.Net.NetworkCredential($userid, $password)
    $wc.downloadfile($uri2, $target)
    }
    Catch [Net.WebException]{
    $_|fl * -Force
    }
    #Delete files from FTP server after download
    $request = [system.Net.FtpWebRequest]::Create($uri2)
    $request.Method = [System.Net.WebRequestMethods+FTP]::DeleteFile
    $response = $request.GetResponse()
    "Deleted from FTP: $source" -f $response.StatusDescription
    $response.Close()
    }
    }
    }
    
    Get-FTPFiles

     

    Thanks for the help.

  • #4076

    Poshoholic
    Participant

    I think it's probably the source, based on the error text and what others have reported around the web.  You should add some commands before you call DownloadFile that output the exact string for the Uri as well as the path.  Write-Debug or Write-Verbose would be good commands to use, because then you could turn the verbose/debug output on or off with ease and leave them in should other issues arise later.

    With these outputs, make sure there are no double-quotes surrounding the strings (or for the Uri that there are no double-quotes or other characters that are not allowed without escaping in a Uri in the string).  I suspect something in that output will help highlight what the issue is here.

  • #4081

    Dust
    Participant

    VERBOSE: ftp://site.state.co.us/Pioneer/Submit_Form_3.1/1912719 – Turlington 22-28 NoVar April222013 1052.xlsx
    VERBOSE: ftp://site.state.co.us/Pioneer/Submit_Form_3.1/

    Here's the verbose, I'm not sure what "Illegal characters" it's finding...?

    The first verbose is the source and the next one is the Uri. I've tried removing all of the spaces, underscores, dashes and dots in the name but it still doesn't make a difference.

    Thanks for the reply.

  • #4087

    Dust
    Participant

    Ok, I got it. I had to do a $names.Trim(). I guess there were some trailing characters that it didn't like. Sorry about that.

    Thanks,

  • #4098

    Jonathan Tyler
    Participant

    It is good that you were able to find the issue and correct.  For your benefit and that of others, I would like to share what I have come up with in the past to help find these types of errors quickly.

    When you Write-Verbose/Write-Debug your strings (in this case a URI), I like to output those strings INSIDE quotes:

    Write-Verbose -Message "The URI is `"$uri`"'

    By doing this, if there are extra spaces that you don't intend, you will see them show up inside the escaped quotation marks.  Using this technique has saved me on countless occasions to find things that you might not normally see in the debug output.

    Hope this helps you in the future.

You must be logged in to reply to this topic.