Last active
October 8, 2019 21:21
-
-
Save dfinke/3d0017e0fe4195a5242c796cdfde5960 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Import-Module PSStringScanner | |
function selectf { | |
$ss = New-PSStringScanner ($args -join ' ') | |
$select = $ss.scanUntil("(?=from )") | |
$null=$ss.scan("from ") | |
if($ss.check(" where ")) { | |
$targetPath = $ss.scanuntil("(?=where )") | |
$null=$ss.scan("where ") | |
$whereVar = $ss.scanuntil("(?==~ )") | |
$null=$ss.scan("=~ ") | |
$whereValue = $ss.scan(".*") | |
} else { | |
$targetPath = $ss.scan(".*") | |
} | |
$cmd = "dir {0} | select {1}" -f $targetPath, $select | |
if($whereVar) { | |
$cmd += ' | where {{ $_.{0} -match {1} }}' -f $whereVar, $whereValue | |
} | |
$cmd | iex | |
} | |
#selectf "name,ext*,length from 'C:\temp'" | |
selectf "name,ext*,length from 'C:\temp' where extension =~ 'png$'" # use a regex |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Prints