# Dragoniod wanted a vbscript that would run a random file in a directory.
# I thought this would be a good exercise in Powershell
# Below are my workings in making this the shortest one-liner as posible.
# I first wrote this script to return a random file from a folder
function Get-RndFile
{
param([string]$dir)
[array]$a=Get-ChildItem $dir | where{$_.PSIsContainer -eq $false}
[int]$b=(($a|Measure).count)-1
[int]$c=(new-object random).next(0,$b)
Write-Output ($a[$c]).fullName
}
# I then set about reducing it to see if I could make a one-liner
function Get-RndFile1
{
param($dir)
$a=ls $dir|?{$_.PSIsContainer -eq $false}
($a[(new-object random).next(0,(($a|Measure).count)-1)]).fullName
}
# The function finally turned into a one-liner
function Get-RndFile2{((ls $args[0]|?{$_.PSIsContainer -eq $false})[(new-object random).next(0,(((ls $args[0]|?{$_.PSIsContainer -eq $false})|Measure).count)-1)]).fullName}
# The completed script as a one-liner. Cool but completely obsure.
icm{ii((ls $args[0]|?{$_.PSIsContainer -eq $false})[(new-object random).next(0,(((ls $args[0]|?{$_.PSIsContainer-eq$false})|Measure).count)-1)]).fullName}-arg c:\scripts