none
Upload a file through FTP using PowerShell RRS feed

  • Question

  • Hello,

    I'm using this code to upload a file through ftp:

    #Directory where files are located  
    $FromDir="C:\me"   
    
    #ftp server info 
    $ftp = "ftp://xxx.com/public_html" 
    $user = "xxx" 
    $pass = "xxx"  
     
    $webclient = New-Object System.Net.WebClient 
    $webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass)  
    $YestDate = (get-date).AddDays(-1)
    $FileNameDate = date $YestDate -f yyyyMMdd
    
    #Pick up files
    foreach($item in (dir $FromDir "FTP_*$FileNameDate.txt")){
        "Uploading $item..." 
        $uri = New-Object System.Uri($ftp+$item.Name) 
        $webclient.UploadFile($uri, $item.FullName) 
     }
    

    I don't get any error but the code is not working.

    • Moved by Bill_Stewart Friday, January 26, 2018 2:40 PM This is not "research solutions for me" forum
    Thursday, November 23, 2017 2:27 PM

All replies

  • You cannot use the WebClient to upload files to most ftp servers.  The other possible issue is that the server is not saving the file where you think it is saving it.  Also your ftp site is not a legitimate ftp site.  It is an HTML page.  ftp locations are folders and not web pages.


    \_(ツ)_/

    Thursday, November 23, 2017 2:49 PM
  • Can you help me to correct the code?
    Thursday, November 23, 2017 2:55 PM
  • It cannot be corrected.  The code cannot do ftp uploads.


    \_(ツ)_/

    Thursday, November 23, 2017 3:12 PM
  • Other solution?

    I want to upload a backup on a server.

    Thursday, November 23, 2017 3:15 PM
  • I  recommend starting by asking the server admins how to upload a file.


    \_(ツ)_/

    Thursday, November 23, 2017 3:26 PM
  • I use WinSCP to script pushing and pulling files to/from a remote SFTP site and it seems to work well. I fire the script on a schedule from a scheduled task on a Windows 2008 R2 server and it emails me the results.

    I'm not the greatest coder but if you like I can post the actual script but there are plenty of examples on the web.

    Hope this helps,

    Linn

    Friday, November 24, 2017 3:28 PM