Functions1.ps1
<#PSScriptInfo
.VERSION 1.0.4 .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" $script:_S = [Microsoft.VisualBasic.Strings] $script:_F = [System.IO.File] $script:_Interact = [Microsoft.VisualBasic.Interaction] $script:_Sty = [Microsoft.VisualBasic.AppWinStyle] $script:_FS = [Microsoft.VisualBasic.FileIO.FileSystem] $script:_pthSep = [System.IO.Path]::DirectorySeparatorChar.ToString() function Append-If ($s,$sfx,$if) { if($if){$s+$sfx}else{$s} } function Brk ($sep,$s) { $p = Instr $sep $s; if($p -eq -1){throw "no sep[$sep] in s[$s]"}; Brk-At $s $p (len $sep)} function Brk-1 ($s,$sep) { $s = Trim $s; $p = Instr $s $sep; if($p-eq0) {$s,""} else { Brk-At $s $p (len $sep) }} function Brk-2 ($s,$sep) { $s = Trim $s; $p = Instr $s $sep; if($p-eq0) {"",$s} else { Brk-At $s $p (len $sep) }} function Brk-At ($s,$pos,$sepLen) { Trim(Left $s ($pos-1)); Trim(Mid $s ($pos+$Seplen)) } function Brk-Spc ($s) { Brk $s " " } function Brk-Spc1 ($s) { Brk-1 $s " " } function Brk-Spc2 ($s) { Brk-2 $s " " } function Get-Hash-Root ($hashO) { ([hashtable]$hashO) } function Brw-Ft { param([parameter(ValueFromPipeLine=$true)]$_ft) process{Shell "notepad.exe ""$_ft""" -sty NormalFocus }} function Brw-Obj { param([parameter(ValueFromPipeLine=$true)]$_Obj)begin{$ft=Tmp-Ft;$f=Opn-Ft $ft -for Write}process{$_Obj|foreach{$f.WriteLine($_)}}end{$f.Close();Brw-Ft $ft}} function Brw-Pth ($Pth) { if(Test-Path $pth) {Shell "explorer ""$Pth""" -sty NormalFocus }} function Brw-TmpPth { Brw-Pth(Tmp-Pth) } function Ens-Pth ($Pth) { if(-not(Test-Path $pth)) { $a = md $pth } } function FstChr ($s) { Left $s 1 } function Push ($ay,$s) { if($ay -eq $null){$s}else{$ay;$s} } function Push-NonEmpty($ay,$s) { if(Is-Empty $s){$ay}else{Push $ay $s} } function Stop { $a = $a } function Get-Term ($s,$atMost=2) { $rest=$s $o=$null for($i=1;$i-lt $atMost;$i++) { $t,$rest=Shift-Term $rest $o=Push-NonEmpty $o $t if(Is-Empty $rest) {break} } $o if(Is-NonEmpty $rest) {$rest} } function Get-3Term ($s) { Get-Term $s 3 } function Ffn-Ext ($ffn) { $p = InstrRev $ffn "."; if($p -eq 0){""}else{mid $ffn $p} } function Ffn-Fn ($ffn) { $p = InstrRev $ffn (PthSep); if($p -eq 0){$ffn}else{mid $ffn ($p+1)} } function Ffn-Pth ($ffn) { $p = InstrRev $ffn (PthSep); if($p -eq 0){""} else{left $ffn $p} } function Ft-Ly ($ft) { $_F::ReadAllLines($ft) } function Ft-Str ($ft) { $_F::ReadAllText($ft) } function Get-Script ($fnNm) { (dir function:$funNm).ScriptBlock } function Get-Script ($fnNm) { (Get-Item function:$fnNm).ScriptBlock} function Has ($ay,$s) { $o=$false;foreach($i in $ay){if($i-eq$s){$o=$true;break}};$o} function Has-2Dash ($s) { Has $s "--" } function Has-3Dash ($s) { Has $s "---" } function Has-Spc ($s) { Has-Sub $s " " } function Has-Sub { param($sub,[Parameter(ValueFromPipeLine=$true)]$s) end{$s.Contains($sub)} } function Instr ($s,$sub) { $s.IndexOf($sub) + 1 } function InstrRev ($s,$sub) { $s.LastIndexOf($sub) + 1 } function Is-2DashRmk ($s) { Is-Pfx $s "--" } function Is-1Term ($s) { $any,$s = Is-AnyTerm $s; return $any} function Is-3Term ($s) { for($i=1;$i -le 3;$i++){$noTerm,$s=Is-NoTerm $s; if($noTerm){return $false}};return $true} function Is-AnyTerm ($s) { $t,$s = Shift-Term $s; (Is-NoEmpty $t),$s } function Is-Empty ($s) { if([System.String]::IsNullOrEmpty($s)){$true}else{-not((trim $s)-match"\S+")}} function Is-NonEmpty ($s) { -not(Is-Empty $s) } function Is-NoTerm ($s) { $t,$s = Shift-Term $s; (is-empty $t),$s } function Is-Pfx ($s,$pfx) { $s.StartsWith($pfx) } function Is-Sfx ($s,$sfx) { $s.EndsWith($sfx) } function Jn-CrLf ($ay) { $ay -join "`r`n" } function Is-Rmk ($s) { Is-Pfx $s "--" } function Rmv-Dup ($ay) { $o=$null; $ay|foreach{if(-not(Has $o $_)){$o=Push $o $_}}; $o} function Jn-Pth ($ay) { $ay = (Rmv-Empty $ay) | foreach { Rmv-PthSepPfxSfx $_ } $a = Jn-Sep $ay (PthSep) $a + (PthSep) } function Jn-Sep ($ay,$sep) { $ay -join $sep} function Left ($s,$len) { $_S::Left($s,$len) } function Len ($s) { $_S::Len($s) } function Mid ($s,$pos,$len=-1) {if($len -lt 0){$_S::Mid($s,$pos)}else{$_S::Mid($s,$pos,$len)}} function Minus-Ay ($ay1,$ay2){$ay1|foreach{if(-not(Has $ay2 $_)){$_}}} function Opn-Ft { param($ft,[Validateset("Read","Write","Append")]$for="Read") if($for -eq "Read"){$_FS::OpenTextFileReader($ft)}else{$_FS::OpenTextFileWriter($ft, ($for -eq "Append"))}} function Prepend-If ($s,$pfx,$if) { if($if){$pfx+$s}else{$s} } function PthSep { $_pthSep } function Right ($s,$len) { $_S::Right($s,$len) } function Rmv-3DashRmk($s) { Rmv-Rmk $s "---" } function Rmv-2DashRmk($s) { Rmv-Rmk $s "--" } function Rmv-Ele ($ay,$pfx) { $ay.Where{-not(Is-Pfx $_ $pfx)} } function Rmv-Empty ($ay) { $ay|foreach{if(-not(Is-Empty $_)){$_}}} function Rmv-Rmk ($s,$rmkPfx) { Tak-FstOrAll $s $rmkPfx } function Rmv-PthSepPfxSfx ($s) { Rmv-PfxSfx $s (PthSep) (PthSep)} function Rmv-PfxSfx ($s,$pfx,$sfx) { Rmv-Sfx (Rmv-Pfx $s $pfx) $sfx } function Rmv-Pfx ($s,$pfx) { If(Is-Pfx $s $pfx){Mid $s ((Len $pfx)+1)}else{$s}} function Rmv-Sfx ($s,$sfx) { if(Is-Sfx $s $sfx){$a=Len $s; $b=Len $sfx; $n=$a-$b; Left $s $n}else{$s}} function Shell ($cmd,[Microsoft.VisualBasic.AppWinStyle]$sty=$_Sty::Hide){$a=$_Interact::Shell($cmd,$sty,$false,-1) } function Shift-Term ($s) { Brk-Spc1 $s} function Spc ($nSpc) { $_S::Space($nSpc) } function Split-CrLf ($lines) { $lines -split "`r`n" } function Srt-Ft ($ft) { Ft-Ly $ft | sort } # }| Wrt-Obj $ft } function Switch-Item { param($idx,[parameter(ValueFromRemainingArguments=$true)]$ap) $ap[$idx]} function Sz ($ay) { [System.Array]$ay.Count } function Tak-Fst ($s,$sep) { (Brk-2 $s $sep)[0] } function Tak-FstOrAll($s,$sep) { (Brk-1 $s $sep)[0] } function Tak-Snd ($s,$sep) { (Brk-2 $s $sep)[1] } function Tak-SndOrAll($s,$sep) { (Brk-1 $s $sep)[1] } function Tmp-Fdr ($fdr) { $p = Jn-Pth ((Tmp-Pth),$fdr,(Tmp-Nm)); Ens-Pth $p; $p } function Tmp-Fn ($ext) { (Tmp-Nm) + $ext } function Tmp-Ft ($fdr) { (Tmp-Pth) + (Tmp-Fn ".txt" $fdr) } function Tmp-Fx ($fdr) { (Tmp-Pth) + (Tmp-Fn ".xlsx" $fdr) } function Tmp-Nm { "T" + ([System.DateTime]::Now.ToString("yyyy_MM_dd_HHmmss")) } function Tmp-Pth ($fdr) { Jn-Pth ($_tmpPth,$fdr) } function Trim ($s) { ([string]$s).Trim() } function Ty { param([Parameter(ValueFromPipeLine=$true)]$Obj) END{if($Obj-eq $null){}else{$obj.GetType()}} } function Ty1 { if($Obj-eq $null){}else{$obj.GetType()} } function TyNm ($Obj) { if($Obj-eq $null){'$null'}else{([System.Type]$obj.GetType()).FullName} } function Ub ($ay) { (Sz $ay) - 1 } function Wrt-Obj { param($ft,[Parameter(ValueFromPipeLine=$true)]$Obj)begin{$f=Opn-Ft $ft -for Write}process{$Obj|foreach{$f.WriteLine($_)}}end{$f.Close()}} $script:_tmpPth = .{$o = Jn-Pth ($env:Tmp,"mypowershell"); Ens-Pth $o; $o} # srt-Ft $MyInvocation.InvocationName |