04 Mar 2009

Lately I have been playing around with PowerShell. I decided that I will write scripts in order to perform some simple actions, actions that could be scripted generally are not due to the fact that writing the script takes longer than manually doing it.

So here is the first task I would like to automate: sequentially download each file in a list, the list being provided as a text file.

Download a file using PowerShell

For this part, I simply "stole" the code from somewhere else: http://community.bartdesmet.net/blogs/bart/archive/2006/11/25/PowerShell-_2D00_-How-to-download-a-file_3F00_.aspx, plain simple, I don't think I'll be able to do better.

Read a text file in an array

I was expecting this to be a bit more complicated, but it turns out to be piece of cake:

$array = Get-Content TextFile.txt

Not too much trouble.

Putting it all together

So now we need to do make these things work together. First, read the content of the file (given as parameter) in an array, then for each item in the array get the client to download it.

Now, there is one little trick here. The WebClient.DownloadFile method's second argument is the local file. In this case, we want it to have the same file name as the source file name. Leveraging the .NET framework, System.IO.Path is the way to go.

$list = Get-Content $args

$clnt = New-Object System.Net.WebClient

foreach($url in $list) 

	#Get the filename 
	$filename = [System.IO.Path]::GetFileName($url) 

	#Create the output path 
	$file = [System.IO.Path]::Combine($pwd.Path, $filename) 

	Write-Host -NoNewline "Getting ""$url""... "

	#Download the file using the WebClient 
	$clnt.DownloadFile($url, $file) 

	Write-Host "done." 

Two remarks on of the Path class:

  • To call static members, we have to use :: instead of ., unlike in C#.
  • Surprisingly (and what a nice surprise), Path.GetFileName works on URLs too. So Path.GetFileName(http://www.google.com/hello.txt) returns "hello.txt".

The script is not error proof, but it good enough for me now.

Edit: I added some Host-Write lines in order to display something in the shell. When downloading a long list of files, it is nice to see the progress.

blog comments powered by Disqus