Feelings of White   brought to you by gange green… it’s not just for wars!
Me, looking like Hot Sex inc. with my cool shades, a beer and my bountiful chest hair displayed for all to see
  • all
  • curator's pick
  • funny
  • narcissism
  • technical
  • the arts
  • the void
  • writing

Look what you can do in Powershell: FTP!

Imagine my surprise when my friendly neighbourhood I.T. fellow told me a ftp command line client built into windows. Man, I haven't done command line FTP since high school. But after finding a good ftp command reference, well, of course, I couldn't resist trying to concoct an unholy marriage between ftp and powershell.


$listOfFiles = "open secret.ftplocation.com
user username password
binary
cd LocationICareAbout
mdir * -" | ftp -i -n | where {$_.Trim() -ne "" } | where {$_.SubString(24,5) -ne "<DIR>"} | %{ $_.SubString(39) }

To explain: FTP can accepted piped-input, but I realized instead of having to put my answerfile into an actual file, I could just use powershell's ability to have multi-line strings and variables pumped right in.

The above text is logging into the ftp location client of yer choice and getting a file listing (the msdir bit). As I only cared about files, I ignore the "<DIR>"s, and with a bit of basic string manipulation, viola! I've got a list of all files in a given directory


$cmd = "open secret.ftplocation.com
user username password
binary
cd LocationICareAbout
" +
($listOfFiles | %{ "get ""$_""`n" })

$cmd | ftp -i -n

To explain: This final bit turns the list of files into another literal string, which says
   get FileX
   get FileY
etc, which gets piped to ftp. the end result is a download of all the files in the directory to my hard drive. Of course, you could get creative and do all sorts of manipulations to $listOfFiles, if that's what turns you on.

I've got another nearly identical script where instead of "get" I say "delete", same concepts. And got another one that builds up $listOfFiles from a local directory (via ls) and uses "put" to upload all files from a local dir to the remote dir. You get the idea.

ftp supposedly supports a -s:filename but I couldn't get it to work. And really, who would want to? Inlining the ftp commands is just so damn sexy!

After Thoughts:
(wherein I blather on and on and get less technical)

Man I had a blast doing this. Within about two hours I'd gone from ftp n00b to having working scripts that actually helped me do my job much faster. I'd been using my favorite abadonware ftp gui to do uploads/downloads and now I had scripts that did it all faster and consistently.

A nice bite-sized problem involving unknown, but good, tools combined with the joy of the admin development model meant figuring it all out was fun for me. The scripts are pretty basic, really, but figuring junk out makes me go a big rubbery one :P

Really, it's that lovely admin development model more than anything else I think I love. (I've done lots of it over my life, but only ever heard the phrase thanks to powershell blogs.) I have built up so many extrodinarily useful scripts over the last few months, by typing commands into a stupid blue window until they worked, then pasting them into notepad and saving it.

Within another month or two I'll have fully automated processes at my job that used to take up days and days and days of peoples times. Of course, it would only take 1 or 2 weeks of dedicated time to do it all, but we all know how that goes.. no one's going to give you official time to fix anything, not that you've got time when everything needs to be done yesterday, so you gotta slip it under the wire. But then! To the rescue! Comes that lovely admin development model (some people might call it agile development too), where I don't have to do it all at once. I just have to add a few more lines to my powershell scripts here and there, and one day *poof* the entire process is a repeatable automated one-liner instead of a clusterfuck of a timewasting error prone inconsistent undocumented manual process.

All part of my grand plan for world domination. One dirty hack at a time.

2007 Mar 14 10:07 pm; Filed under powershell, technical and tagged ftp.
« Good Goddamn, My Job Sux « before «
» after » Good Goddamn, Blogger sucks balls »
  1. Liam J. on 2007 Apr 02

    I don't know if this will be read by you, James, but I have to say this about Powershell: It's still not as good as BASH. On tab, it will autocomplete for things in the directory you're in, but BASH will search the path for all the files that match what you've typed in, so far. If there's one, it will autocomplete and if there's more than one, it will list them.

    I'll still take BASH/Cygwin.

  2. legion on 2007 Apr 02

    Oh man.. I hear what you're saying, and yes the tab-completion has got some heat about being weak in comparison. Even Microsoft was pretty forward about that, and there's lots of profile-plugins and snippets for the tab lovers.

    So, while I conceed to you about the tab (not that I personally care), the wickedness of powershell is that it uses full & live objects. not just that you can say "new-object System.FooBar.MyClass" and stuff, but the pipe-line is *not* text-based, but object-based.

    When I do an "ls" and pipe it into something, I don't have to worry that it's characters 23-47 that make up the filename, and characters 10-22 that make up the filesize, or whatever. I just type $_.FullName or $_.Length, and it even knows the type! so I can say $_.FullName.SubString(3,21) or whatever.

    Genious!! I have done more than enough string-parsing in my life, I'll skip it whenever possible. It's error prone and typically a pain in the ass.

    Nevermind that I can do junk like
    ([xml](svn info --xml)) and turn svn's text-based xml output into a full xml document object that I can instantly navigate with xpath, or do any other xml stuff with it (modify it, save it back to disk, whatever)

    HOT DAMN!!!!

  3. Liam J. on 2007 Apr 03

    No argument here. The thing is powerful. It's just frustrating when you don't know the exact name of a command.

  4. /\/\o\/\/ on 2008 Feb 09

    Agreed basic Tabcompletion is a bit limited in PowerShell, but it is extendable.

    For an Idea of what tabcompletion can ( or be made to ) do in PowerShell take a look a my PowerTab :

    http://thepowershellguy.com/blogs/posh/pages/powertab.aspx

    for a short flash demo, if your not using PowerShell Yet ;-) :

    http://thepowershellguy.com/blogs/posh/archive/2007/06/02/powertab-flash-exampes.aspx

    Enjoy,

    Greetings /\/\o\/\/

  5. Anonymous on 2008 Mar 20

    I you guys are so on about the autocomplete thing... may i suggest powershellplus. Its an ide for powershell and has all the niceties (event its own script editor with a debugger!!! Veeeeeeery cool)

    Just google powershellplus.

    Its currently beta and free for non-commercial use

Posting your comment.
Follow responses with this post's comment feed.

Leave a reply

Subscribe

Recent Posts

  • Bootstrapping Baby
  • Basic Math
  • Jimbo vs. The Tooltip
  • Why I’m Throwing Away My Vote
  • Comics You Must Read! #1
  • The Shit Dancer [Tamdhu Stories 1.3]
  • Money & Other Friends [Tamdhu Stories 1.2]
  • Betty the Gas-Whore [Tamdhu Stories 1.1]
  • Command Line Parsing using Attribute Decoration in .NET
  • We Are Metallica
  • The Best of Projects: Fiction [3 of 3]
  • Good goddamn, Galactica Roxxors With Coxx Out
  • BSG 4×07 - Guess What’s Coming to Dinner?
  • WordPress plugin: Apply a CSS class defined in a post’s custom field
  • The Best of Projects: Autoserious [2 of 3]

Recent Comments

  • Uncle Henri on Bootstrapping Baby
  • Uncle Henri on Bootstrapping Baby
  • legion on Bootstrapping Baby
  • Michelle on Bootstrapping Baby
  • Erron on Bootstrapping Baby
  • Steven on Bootstrapping Baby
  • Cliff on Bootstrapping Baby
  • Liam on Bootstrapping Baby
  • Shaun on Bootstrapping Baby
  • Kyle on Bootstrapping Baby

Tags

4400 bsg cliff comics curation depression ds9 erron family fiction game janine job kelly kyle liam lost manifesto meta mlp music passionate diatribes plug plugin poem powershell rds relationships review revisionism sam sermon software spirituality star trek suicide tamdhu testpoint the process travels video vlad wacky walkabout wtf

Archives

  • December 2008 (3)
  • October 2008 (1)
  • August 2008 (2)
  • July 2008 (3)
  • June 2008 (1)
  • May 2008 (11)
  • April 2008 (7)
  • March 2008 (3)
  • February 2008 (1)
  • January 2008 (2)
  • December 2007 (1)
  • October 2007 (1)
  • September 2007 (3)
  • August 2007 (1)
  • June 2007 (3)
  • May 2007 (2)
  • March 2007 (5)
  • February 2007 (5)
  • January 2007 (13)
  • September 2006 (1)
  • June 2001 (3)
  • May 2001 (2)
  • April 2001 (2)
  • March 2001 (2)
  • February 2001 (1)
  • January 2001 (1)
  • November 2000 (5)
  • May 2000 (3)
  • April 2000 (5)
  • March 2000 (3)
  • February 2000 (3)
  • January 2000 (6)
  • December 1999 (17)

Copyright © 2008 Feelings of White | Powered by WordPress | Designed by Stephen Reinhardt; tweaked by me