functions1.ps1
<#PSScriptInfo .VERSION 1.0.2 .GUID a85856c3-6ea4-424e-afd5-56fb8b50b5c9 .AUTHOR user .COMPANYNAME .COPYRIGHT .TAGS .LICENSEURI .PROJECTURI .ICONURI .EXTERNALMODULEDEPENDENCIES .REQUIREDSCRIPTS .EXTERNALSCRIPTDEPENDENCIES .RELEASENOTES .DESCRIPTION Some functions #> Add-Type -Path "C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.VisualBasic.Compatibility\v4.0_10.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualBasic.Compatibility.dll" function ft-ly ($ft) { [System.IO.File]::ReadAllLines($ft) } function ft-str ($ft) { [System.IO.File]::ReadAllText($ft) } function rmv-eleByPfx($ay,$pfx) { $ay.Where{-not (is-pfx $_ $pfx)} } function is-pfx ($s,$pfx) { $s.StartsWith($pfx) } function sz ($ay) { [System.Array]$ay.Count } function ub ($ay) { (sz $ay) - 1 } function map { param($f,[parameter(ValueFromPipeLine=$true)]$ay)process{$ay | foreach {$f.Invoke($_)}} } function split-crlf ($lines) { $lines.Split(@("`r`n"),[System.StringSplitOptions]::None)} function left ($s,$len) { $s.Substring(0,$len) } function mid ($s,$pos,$len=-1) { if($len -lt 0) { $s.Substring($pos-1) } else { $s.Substring($pos-1,$len)}} function brk-at ($s,$pos,$sepLen) { trim(left $s ($pos-1)); trim(mid $s ($pos+$Seplen)) } function instr ($s,$sub) { $s.IndexOf($sub) + 1 } function instrRev ($s,$sub) { $s.LastIndexOf($sub) + 1 } function len ($s) { $s.Length } function brk ($s,$sep) { $p = instr $s $sep; if($p -eq -1) {throw "no sep[$sep] in s[$s]"}; brk-at $s $p (len $sep)} function get-script ($funNm) { $f = (get-item function:$funNm); $f.ScriptBlock } function brk-spc ($s) { brk $s " "} function spc ($nSpc) { [Microsoft.VisualBasic.Strings]::Space($nSpc) } function trim ($s) { ([string]$s).Trim() } function brk-2 ($s,$sep) { $s = trim $s; $p = instr $s $sep; if($p -eq 0) {"",$s} else { brk-at $s $p (len $sep) }} function brk-1 ($s,$sep) { $s = trim $s; $p = instr $s $sep; if($p -eq 0) {$s,""} else { brk-at $s $p (len $sep) }} function tak-s1 ($s,$sep) { (brk-2 $s $sep)[0] } function tak-s1OrAll ($s,$sep) { (brk-1 $s $sep)[0] } function tak-s2 ($s,$sep) { (brk-2 $s $sep)[1] } function tak-s2OrAll ($s,$sep) { (brk-1 $s $sep)[1] } function fstChr ($s) { left $s 1 } function pthSep { [System.IO.Path]::DirectorySeparatorChar.ToString() } function append-if ($s,$pfx,$if) { if($if) {$s+$pfx} else {$s} } function ffn-Pth ($ffn) { $p = instrRev $ffn (pthSep); if($p -eq 0){""}else{left $ffn $p} } function is-1term ($s) { $any,$s = any-term $s; return $any} function any-term ($s) { $t,$s = shift-term $s; (-not(is-empty $t)),$s } function no-term ($s) { $t,$s = shift-term $s; (is-empty $t),$s } function is-3term ($s) { for($i=1;$i -le 3;$i++){$noTerm,$s=no-term $s; if($noTerm){return $false}};return $true} function is-empty ($s) { if([System.String]::IsNullOrEmpty($s)){$true}else{-not((trim $s) -match "\S+")}} function has-spc ($s) { has-sub $s " " } function brk-spc1 ($s) { brk-1 $s " " } function brk-spc2 ($s) { brk-2 $s " " } function brk-spc ($s) { brk $s " " } function has-sub ($s,$sub) { $s.Contains($sub) } function has-dd ($s) { has $s "--" } function has-ddd ($s) { has $s "---" } function shift-term ($s) { brk-spc1 $s} function srt-ft ($ft) { ft-ly $ft | sort | wrt-ay $ft } function wrt-obj { param($ft,[Parameter(ValueFromPipeLine=$true)]$obj)begin{$f=opn-ft $ft -for Write}process{$obj|foreach{$f.WriteLine($_)}}end{$f.Close()}} function jn-crlf ($ay) { $ay -join "`r`n" } function opn-ft { param($ft,[Validateset("Read","Write","Append")]$for="Read") if($for -eq "Read") {[Microsoft.VisualBasic.FileIO.FileSystem]::OpenTextFileReader($ft)} else {[Microsoft.VisualBasic.FileIO.FileSystem]::OpenTextFileWriter($ft, ($for -eq "Append"))}} function jn-pth ($ay) { $s = pthSep; ($ay -join $s) + $s } function tmp-fdr ($fdr) { $p = jn-pth ((tmp-pth)+$fdr),(tmp-nm); ens-pth $p; $p } function tmp-pth { $o = jn-pth $env:tmp,"mypowershell"; ens-pth $o; $o} function ens-pth ($pth) { if(-not(test-path $pth)) { $a = md $pth } } function tmp-nm { "T" + ([System.DateTime]::Now.ToString("yyyy_MM_dd_HHmmss")) } function tmp-fn ($ext) { (tmp-nm) + $ext } function tmp-ftFn { tmp-fn ".txt" } function tmp-ft { (tmp-pth) + (tmp-ftFn)} function rmv-rmk ($s,$rmkPfx) { tak-s1OrAll $s $rmkPfx } function rmv-ddRmk ($s) { rmv-rmk $s "--" } function rmv-dddRmk ($s) { rmv-rmk $s "---" } function rmv-empty { param([parameter(ValueFromPipeLine=$true)]$obj)process{$obj|foreach{if(-not(is-empty $_)){$_}}}} function brw-obj { param([parameter(ValueFromPipeLine=$true)]$obj)begin{$t=tmp-ft}process{wrt-obj $t $obj}end{brw-ft $t}} function get-3terms ($s) { $s = rmv-rmk $s; $1,$s = shift-term $s; $2,$3 = shift-term $s; rmv-empty $1,$2,$3} function brw-pth ($pth) { if(test-path $pth) {shell "explorer ""$pth""" -sty NormalFocus }} function brw-tmpPth { brw-pth(tmp-pth) } function brw-ft { param([parameter(ValueFromPipeLine=$true)]$ft) process {shell "notepad.exe ""$ft""" -sty NormalFocus }} function shell ($cmd,[Microsoft.VisualBasic.AppWinStyle]$sty=[Microsoft.VisualBasic.AppWinStyle]::Hide){$a=[Microsoft.VisualBasic.Interaction]::shell($cmd,$sty,$false,-1) } # srt-ft $MyInvocation.InvocationName |