PoshCode Archive  Artifact Content

Artifact 45946eea0ab12e984f08ce25882b05c0a7967c1b16774e43cf190943ac6f8d57:

  • File Set-RandomFile.ps1 — part of check-in [163693626f] at 2018-06-10 13:43:44 on branch trunk — Creates random file with extension which has assosiation (just for fun). (user: greg zakharov size: 958)

# encoding: ascii
# api: powershell
# title: Set-RandomFile
# description: Creates random file with extension which has assosiation (just for fun).
# version: 0.1
# type: function
# author: greg zakharov
# license: CC0
# function: Set-RandomFile
# x-poshcode-id: 4611
# x-archived: 2013-11-22T23:55:50
# x-published: 2013-11-16T15:44:00
#
#
#requires -version 2.0
function Set-RandomFile {
  param(
    [Parameter(Mandatory=$true,
               Position=0)]
    [String]$Path,
    
    [Parameter(Position=1)]
    [ValidateRange(0, 31)]
    [Int32]$Length = 7
  )
  
  begin {
    $ext = cmd /c assoc | % {if ($_ -match '=\w+' -and $_ -notmatch '.\d+=') {$_.Split('=')[0]}}
    $itm = -join ([GUID]::NewGuid().Guid -replace '-', '')[0..$Length]
  }
  process {
    $rnd = Get-Random -max ($ext.Length - 1)
  }
  end{
    if (Test-Path $Path) {
      [void](ni -p $Path -n $($itm + $ext[$rnd]) -t File -fo)
    }
  }
}