Difference between revisions of "Using Powershell for ftp 2"
Line 24: | Line 24: | ||
response.Close(); | response.Close(); | ||
+ | |||
+ | </source> | ||
+ | |||
+ | <source lang="powershell> | ||
+ | |||
+ | $Server = "ftp://ftp.example.com/" | ||
+ | $User = "anonymous@example.com" | ||
+ | $Pass = "anonymous@anonymous.com" | ||
+ | |||
+ | Function Get-FtpDirectory($Directory) { | ||
+ | |||
+ | # Credentials | ||
+ | $FTPRequest = [System.Net.FtpWebRequest]::Create("$($Server)$($Directory)") | ||
+ | $FTPRequest.Credentials = New-Object System.Net.NetworkCredential($User,$Pass) | ||
+ | $FTPRequest.Method = [System.Net.WebRequestMethods+FTP]::ListDirectoryDetails | ||
+ | |||
+ | # Don't want Binary, Keep Alive unecessary. | ||
+ | $FTPRequest.UseBinary = $False | ||
+ | $FTPRequest.KeepAlive = $False | ||
+ | |||
+ | $FTPResponse = $FTPRequest.GetResponse() | ||
+ | $ResponseStream = $FTPResponse.GetResponseStream() | ||
+ | |||
+ | # Create a nice Array of the detailed directory listing | ||
+ | $StreamReader = New-Object System.IO.Streamreader $ResponseStream | ||
+ | $DirListing = (($StreamReader.ReadToEnd()) -split [Environment]::NewLine) | ||
+ | $StreamReader.Close() | ||
+ | |||
+ | # Remove first two elements ( . and .. ) and last element (\n) | ||
+ | $DirListing = $DirListing[2..($DirListing.Length-2)] | ||
+ | |||
+ | # Close the FTP connection so only one is open at a time | ||
+ | $FTPResponse.Close() | ||
+ | |||
+ | # This array will hold the final result | ||
+ | $FileTree = @() | ||
+ | |||
+ | # Loop through the listings | ||
+ | foreach ($CurLine in $DirListing) { | ||
+ | |||
+ | # Split line into space separated array | ||
+ | $LineTok = ($CurLine -split '\ +') | ||
+ | |||
+ | # Get the filename (can even contain spaces) | ||
+ | $CurFile = $LineTok[8..($LineTok.Length-1)] | ||
+ | |||
+ | # Figure out if it's a directory. Super hax. | ||
+ | $DirBool = $LineTok[0].StartsWith("d") | ||
+ | |||
+ | # Determine what to do next (file or dir?) | ||
+ | If ($DirBool) { | ||
+ | # Recursively traverse sub-directories | ||
+ | $FileTree += ,(Get-FtpDirectory "$($Directory)$($CurFile)/") | ||
+ | } Else { | ||
+ | # Add the output to the file tree | ||
+ | $FileTree += ,"$($Directory)$($CurFile)" | ||
+ | } | ||
+ | } | ||
+ | |||
+ | Return $FileTree | ||
+ | |||
+ | } | ||
</source> | </source> |
Revision as of 18:37, 5 August 2015
http://stackoverflow.com/questions/5394090/is-it-possible-to-do-active-mode-ftp-using-ftpwebrequest
// Get the object used to communicate with the server. FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/test.htm"); request.Method = WebRequestMethods.Ftp.UploadFile; // This example assumes the FTP site uses anonymous logon. request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com"); request.UsePassive = false; //////// ADDed this but not tested // Copy the contents of the file to the request stream. StreamReader sourceStream = new StreamReader("testfile.txt"); byte [] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd()); sourceStream.Close(); request.ContentLength = fileContents.Length; Stream requestStream = request.GetRequestStream(); requestStream.Write(fileContents, 0, fileContents.Length); requestStream.Close(); FtpWebResponse response = (FtpWebResponse)request.GetResponse(); response.Close();
Invalid language.
You need to specify a language like this: <source lang="html4strict">...</source>
Supported languages for syntax highlighting:
4cs, 6502acme, 6502kickass, 6502tasm, 68000devpac, abap, actionscript, actionscript3, ada, algol68, apache, applescript, apt_sources, arm, asm, asp, asymptote, autoconf, autohotkey, autoit, avisynth, awk, bascomavr, bash, basic4gl, bf, bibtex, blitzbasic, bnf, boo, c, c_loadrunner, c_mac, caddcl, cadlisp, cfdg, cfm, chaiscript, cil, clojure, cmake, cobol, coffeescript, cpp, cpp-qt, csharp, css, cuesheet, d, dcl, dcpu16, dcs, delphi, diff, div, dos, dot, e, ecmascript, eiffel, email, epc, erlang, euphoria, f1, falcon, fo, fortran, freebasic, freeswitch, fsharp, gambas, gdb, genero, genie, gettext, glsl, gml, gnuplot, go, groovy, gwbasic, haskell, haxe, hicest, hq9plus, html4strict, html5, icon, idl, ini, inno, intercal, io, j, java, java5, javascript, jquery, kixtart, klonec, klonecpp, latex, lb, ldif, lisp, llvm, locobasic, logtalk, lolcode, lotusformulas, lotusscript, lscript, lsl2, lua, m68k, magiksf, make, mapbasic, matlab, mirc, mmix, modula2, modula3, mpasm, mxml, mysql, nagios, netrexx, newlisp, nsis, oberon2, objc, objeck, ocaml, ocaml-brief, octave, oobas, oorexx, oracle11, oracle8, oxygene, oz, parasail, parigp, pascal, pcre, per, perl, perl6, pf, php, php-brief, pic16, pike, pixelbender, pli, plsql, postgresql, povray, powerbuilder, powershell, proftpd, progress, prolog, properties, providex, purebasic, pycon, pys60, python, q, qbasic, rails, rebol, reg, rexx, robots, rpmspec, rsplus, ruby, sas, scala, scheme, scilab, sdlbasic, smalltalk, smarty, spark, sparql, sql, stonescript, systemverilog, tcl, teraterm, text, thinbasic, tsql, typoscript, unicon, upc, urbi, uscript, vala, vb, vbnet, vedit, verilog, vhdl, vim, visualfoxpro, visualprolog, whitespace, whois, winbatch, xbasic, xml, xorg_conf, xpp, yaml, z80, zxbasic
$Server = "ftp://ftp.example.com/" $User = "anonymous@example.com" $Pass = "anonymous@anonymous.com" Function Get-FtpDirectory($Directory) { # Credentials $FTPRequest = [System.Net.FtpWebRequest]::Create("$($Server)$($Directory)") $FTPRequest.Credentials = New-Object System.Net.NetworkCredential($User,$Pass) $FTPRequest.Method = [System.Net.WebRequestMethods+FTP]::ListDirectoryDetails # Don't want Binary, Keep Alive unecessary. $FTPRequest.UseBinary = $False $FTPRequest.KeepAlive = $False $FTPResponse = $FTPRequest.GetResponse() $ResponseStream = $FTPResponse.GetResponseStream() # Create a nice Array of the detailed directory listing $StreamReader = New-Object System.IO.Streamreader $ResponseStream $DirListing = (($StreamReader.ReadToEnd()) -split [Environment]::NewLine) $StreamReader.Close() # Remove first two elements ( . and .. ) and last element (\n) $DirListing = $DirListing[2..($DirListing.Length-2)] # Close the FTP connection so only one is open at a time $FTPResponse.Close() # This array will hold the final result $FileTree = @() # Loop through the listings foreach ($CurLine in $DirListing) { # Split line into space separated array $LineTok = ($CurLine -split '\ +') # Get the filename (can even contain spaces) $CurFile = $LineTok[8..($LineTok.Length-1)] # Figure out if it's a directory. Super hax. $DirBool = $LineTok[0].StartsWith("d") # Determine what to do next (file or dir?) If ($DirBool) { # Recursively traverse sub-directories $FileTree += ,(Get-FtpDirectory "$($Directory)$($CurFile)/") } Else { # Add the output to the file tree $FileTree += ,"$($Directory)$($CurFile)" } } Return $FileTree }