Quantcast
Channel: Windows PowerShell Forum
Viewing all articles
Browse latest Browse all 2562

Upload Files to an FTP site

$
0
0

Hi,

I need to upload some files to an FTP site.  Done some looking and have only found this the code below.  It looks like it might work but I need to create a folder as well and there doesn't seem to be straight forward way to do this.  IS there a way to do this using ftp.exe, the windows ftp client as I know that mkdir will create a folder


Function Upload_ftp
#Load the System.Net namespace.
[System.Reflection.Assembly]::Load
("System.Net, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")

#The full path to the source file.
$Source =  "C:\MyFiles\SourceFile.txt"

#The full destination path (will be created).
$Destination = "ftp://www.myserver.com/myfolder/destination.txt"

#Use the static method Create to create a web request.
#Pass the destination as an argument, and cast it to a FtpWebRequest.
$R=[System.Net.FtpWebRequest][System.Net.WebRequest]::Create($Destination)

#Tell the request how it will login (using a NetworkCredential object)
$R.Credentials = New-Object System.Net.NetworkCredential($username, "$password)

#...and what kind of method it will represent. A file upload.
$R.Method = "STOR"

#Here I use the simplest method I can imagine to get the
#bytes from the file to an byte array.
$FileContens = [System.IO.File]::ReadAllBytes($Source)

#Finaly, I put the bytes on the request stream.
$S = $R.GetRequestStream()
$S.Write($FileContens, 0, $FileContens.Length)
$S.Close()
$S.Dispose()


Alter De Ruine


Viewing all articles
Browse latest Browse all 2562

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>