none
How do I disconnect FTP connection? RRS feed

  • Question

  • I'm feeling too connected. Maybe we need a break.

    I've written a script that does several boring things with files before dropping them into two "Send" folders. 

    Based on file names, one set goes to person A, and the other set goes to person B. I'm using:

    $webclient = New-Object System.Net.WebClient $webclient.Credentials = New-Object System.Net.NetworkCredential($ft00,$ft01) foreach($item in (dir $SendDir\PersonA\"*.*")) { ... $uri = New-Object System.Uri($ftp+$RemainderOfPath+$item.Name) $webclient.UploadFile($uri, $item.FullName) #more stuff

    }

    It works fast and very well. The next section is a near duplicate except it looks in folder 'Person B' to send. 

    I've recreated the entire section including the first two lines (and setting new credentials for $ft00 and $ft01): 

    $webclient = New-Object System.Net.WebClient 
    $webclient.Credentials = New-Object System.Net.NetworkCredential($ft00,$ft01)  

    When I debug the script, I find that I'm still connected to the first FTP site. It attempts to send the files to non-existent folder locations (and doesn't catch on that it didn't properly upload - but that's a separate issue I can work on), and fails without notification.

    I *think* I need to abort the current ftp connection and start a new one. I have scoured the interwebs, and come up empty handed. It seems everyone is more interested in STAYING connected. I've tried

    $webclient.Dispose();

    and

     $webclient.Finalize() 

    (exactly as I've written them there - so if my format is wrong that could be the issue too?)

    Also, I'm very, very new - this is my first Powershell script - so feel free to use small words.

    Thank you!





    • Edited by 7b Thursday, December 3, 2015 9:12 PM Clarification
    Thursday, December 3, 2015 9:03 PM

Answers

  • No need.  Just place in a function and call the function for each target. There is no open Connection.  WebClient opens and closes the connection on each call.

    function SendFiles{
    	param(
    $uri. $folder ) foreach ($item in (dir $folder)) { $uri = New-Object System.Uri($ftp + $RemainderOfPath + $item.Name) $webclient.UploadFile($uri, $item.FullName) } } $webclient = New-Object System.Net.WebClient $webclient.Credentials = New-Object System.Net.NetworkCredential($ft00, $ft01) SendFiles $uri1 "$SendDir\PersonA\*" SendFiles $uri2 "$SendDir\PersonB\*"


    \_(ツ)_/



    • Edited by jrv Thursday, December 3, 2015 9:14 PM
    • Marked as answer by 7b Saturday, December 5, 2015 9:55 AM
    Thursday, December 3, 2015 9:12 PM

All replies

  • No need.  Just place in a function and call the function for each target. There is no open Connection.  WebClient opens and closes the connection on each call.

    function SendFiles{
    	param(
    $uri. $folder ) foreach ($item in (dir $folder)) { $uri = New-Object System.Uri($ftp + $RemainderOfPath + $item.Name) $webclient.UploadFile($uri, $item.FullName) } } $webclient = New-Object System.Net.WebClient $webclient.Credentials = New-Object System.Net.NetworkCredential($ft00, $ft01) SendFiles $uri1 "$SendDir\PersonA\*" SendFiles $uri2 "$SendDir\PersonB\*"


    \_(ツ)_/



    • Edited by jrv Thursday, December 3, 2015 9:14 PM
    • Marked as answer by 7b Saturday, December 5, 2015 9:55 AM
    Thursday, December 3, 2015 9:12 PM
  • This is very curious  - why?

      $uri = New-Object System.Uri($ftp + $RemainderOfPath + $item.Name)

    Just use the URL  - 'http://www.someplace.com/folder/' + $item.Name

    I think you need to rethink what you need to do anc clarify it for yourself and for any further questions.


    \_(ツ)_/


    • Edited by jrv Thursday, December 3, 2015 9:17 PM
    Thursday, December 3, 2015 9:17 PM
  • Note that this also works:

    [system.uri] 'http://www.someplace.com/folder/' + $item.Name

    This is why it is unnecessary as the method will auto-cast this.

    This:

    $webclient.UploadFile('http://www.someplace.com/folder/' + $item.Name $item.FullName)

    Does exactly the same thing.


    \_(ツ)_/


    • Edited by jrv Thursday, December 3, 2015 9:23 PM
    Thursday, December 3, 2015 9:21 PM
  • Because each file, is going first to 1 of 2 ftp "recipients" but then based on its filename into 1 of 3 sub-folders as well. Also, because this is my very first script, and I'm still learning.

    Thanks for the help!

    Saturday, December 5, 2015 9:53 AM
  • Thank you! This led me down a complete re-working and education of "scope" as all my variables failed. I learned a lot as I tweaked and rewrote.  

    However, it worked! This was exactly what I needed.

    Thank you. 

    Saturday, December 5, 2015 9:55 AM