How to reduce a script into obscurity"

Author Message
TomRiddle

  • Total Posts : 620
  • Scores: 12
  • Reward points : 0
  • Joined: 2/7/2008
  • Location: Australia
  • Status: offline
How to reduce a script into obscurity" Saturday, May 21, 2011 7:32 PM (permalink)
0
# 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
 
-join([int[]][char[]]'Ut|jwXmjqq%Wzqjx'|%{[char]($_-5)})
 
#1
    TNO

    • Total Posts : 2094
    • Scores: 36
    • Reward points : 0
    • Joined: 12/18/2004
    • Location: Earth
    • Status: offline
    Re:How to reduce a script into obscurity" Thursday, June 02, 2011 3:42 PM (permalink)
    0
    How about:
     
    dir c:\test\*.* | Get-Random | Invoke-Item
    To iterate is human, to recurse divine. -- L. Peter Deutsch
     
    #2
      TomRiddle

      • Total Posts : 620
      • Scores: 12
      • Reward points : 0
      • Joined: 2/7/2008
      • Location: Australia
      • Status: offline
      Re:How to reduce a script into obscurity" Thursday, June 02, 2011 4:36 PM (permalink)
      0
      Hey.. It's no longer obscure.
       
      Powershell never stops amazing me on how simple some scripts can be when you use the power of the pipeline. 
       
      I tried dir .* and dir * but did not think of *.* doh.
      So as a result I had to resort to that ugly method of getting the files without folders.  
       
      Well done.
      -join([int[]][char[]]'Ut|jwXmjqq%Wzqjx'|%{[char]($_-5)})
       
      #3

        Online Bookmarks Sharing: Share/Bookmark

        Jump to:

        Current active users

        There are 0 members and 1 guests.

        Icon Legend and Permission

        • New Messages
        • No New Messages
        • Hot Topic w/ New Messages
        • Hot Topic w/o New Messages
        • Locked w/ New Messages
        • Locked w/o New Messages
        • Read Message
        • Post New Thread
        • Reply to message
        • Post New Poll
        • Submit Vote
        • Post reward post
        • Delete my own posts
        • Delete my own threads
        • Rate post

        2000-2012 ASPPlayground.NET Forum Version 3.9