Functions2.psm1
ADD-Type -Path "C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.VisualBasic.Compatibility\v4.0_10.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualBasic.Compatibility.dll" $local:_S = [Microsoft.VisualBasic.Strings] $local:_F = [System.IO.File] $local:_Interact = [Microsoft.VisualBasic.Interaction] $local:_Sty = [Microsoft.VisualBasic.AppWinStyle] $local:_FS = [Microsoft.VisualBasic.FileIO.FileSystem] $local:_pthSep = [System.IO.Path]::DirectorySeparatorChar.ToString() function Is-Some ($script) { $some=$false;foreach($_ in $input){if( &$script) {$some=$true ;break}};$some} function Is-All ($script) { $all =$true ;foreach($_ in $input){if(!(&$script)){$all =$false;break}};$all } function Is-None ($script) { $no =$true ;foreach($_ in $input){if( &$script) {$no =$false;break}};$no } function Get-FixWdtFmtStr($wdt) { $i=0; (Get-Term $wdt 100| foreach {"{$i,-$_}"; $i+=1} ) -join "" } function Fmt-Col ($wdt) { (Get-FixWdtFmtStr $wdt) -f $input } function Add-FnSfx ($sfx) { $a,$b=Brk-2 $fn "." -rev; $a+$sfx+"."+$b} function Rmv-Ext { $input | Tak-FstOrAll "." -rev } function Repl-Ext ($newExt) { process{ ($input | Rmv-Ext) + $newExt } } function About { cls; help about_$input } function Append-If ($sfx,$if) { if($if){$input+$sfx}else{$input} } function Brk ($sep) { $p = $input | Instr $sep; if($p -eq -1){throw "no sep[$sep] in s[$input]"}; $input | Brk-At $p (len $sep) } function Brk-1 ($sep,[switch]$rev) { process{$input|Brk $s=Trim $s; $p=if($rev){$input | InstrRev $sep}else{$input | Instr $sep} if($p-eq0) {$s,""} else {$input | Brk-At $p ($sep|Len)} } } function Brk-2 ($sep,[switch]$rev) { $s = $input|Trim $p = $s|Instr $sep $rev; if($p-eq 0) {"",$s} else { $s | Brk-At $p ($sep|Len) }} function Brk-At ($pos,$sepLen) { $input | Left ($pos-1) | Trim; $input | Mid ($pos+$Seplen) | Trim } function Brk-Spc { $input | Brk " " } function Brk-Spc1 { $input | Brk-1 " " } function Brk-Spc2 { $input | Brk-2 " " } function Get-HashRoot ($hashO) { ([hashtable]$hashO) } function Brw-Ft { process{Shell "notepad.exe ""$($input|Get-FullFn)""" -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) { $pth=Get-FullPth $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 ($s) { if($ay -eq $null){$s}else{$ay;$s} } function Push-NonEmpty($s) { if(Is-Empty $s){$ay}else{Push $ay $s} } function Get-Idx ($ele) { ([System.Array]$ay).IndexOf($ele) } function Rmv-2DotInPth { process { $pth = Rmv-PthSepPfxSfx $innput while(Has-Sub $pth '..') { $a = $pth -split '\' + $_pthSep $i2 = Get-Idx $a '..' $i1 = $i2 - 1 $u = ub $a $o = @() (0..$u) | foreach { if(($_ -ne $i2) -and ($_ -ne $i1)) { $o = push $o ($a[$_]) } } $pth = (Jn-Sep $o $_pthSep) + $_pthSep }; $pth }} function Rmv-LasChr { process{if(($input|Len)-eq 0){""}else{$input|Left(($input|Len)-1)}}} function Repl-Sub ($sub,$by,$beg=1,$cnt=-1) { process{$_S::Replace($input,$sub,$by,$beg,$cnt)}} function Ens-PthSfx { if($input|Is-Sfx $_pthSep){$input}else{$input+$_pthSep}} function Get-FullPth { $o=if($input|Has-Sub ':'){$input}else{$pwd.Path+$_pthSep+$input};$o|Get-NormPth } function Get-NormPth { $o=$input|Repl-Sub '\\' '\'; $o = Repl-Sub $o '\.\' '\';$o = Rmv-2DotInPth $o; Ens-PthSfx $o } function Get-FullFn { if($input|Is-Sfx $_pthSep){throw "`$Ffn[$input] cannot ends with \"}Rmv-LasChr($input|Get-FullPth)} function Get-PthAy ($fspec='*.*') { [System.IO.Directory]::GetDirectories(($input|Get-FullPth),$fspec) } function Get-FfnAy ($fspec='*.*') { [System.IO.Directory]::GetFiles(($input|Get-FullPth),$fspec) } function Get-Term ($atMost=2) { process { $o=$null foreach($i in 2..$atMost) { $t,$s=$input | Shift-Term $o = $o | Push-NonEmpty $t if(Is-Empty $s) {break} } $o if(Is-NonEmpty $s) {$s} }} function Get-3Term { process { $input|Get-Term 3 } } function Get-Dft ($dft) { if($input|Is-Empty){$dft}else{$v} } function Get-FfnExt { $p = $input|InstrRev "."; if($p -eq 0){""}else{$input|Mid $p} } function Get-FfnFn { $p = $input|InstrRev $_pthSep; if($p -eq 0){$ffn}else{$input|Mid ($p+1)} } function Get-FfnPth { $p=$input|InstrRev $_pthSep; if($p-eq 0){""}else{$input|Left $p} } function Get-FtLy { $_F::ReadAllLines($input) } function Get-Str { $_F::ReadAllText($input) } function Get-Script { (Get-Item function:$input).ScriptBlock } function Has-Ele ($ele) { $o=$false;foreach($i in $ay){if($i-eq$s){$o=$true;break}};$o} function Has-2Dash { $input|Has-Sub "--" } function Has-3Dash { $input|Has-Sub "---" } function Has-Spc { $input|Has-Sub " " } function Has-Sub ($sub) { $s.Contains($sub) } function Instr ($sub,[switch]$rev) { if($rev){InstrRev $s $sub}else{$s.IndexOf($sub)+1} } function InstrRev ($sub) { $s.LastIndexOf($sub) + 1 } function Is-2DashRmk { $input|Is-Pfx "--" } function Is-1Term { !($input|Is-Empty) } function Is-3Term { $o=$true;foreach($i in 1..3){$noTerm,$s=Is-NoTerm $s;if($noTerm){$o=$false;break}};$o } function Is-Empty { if([System.String]::IsNullOrEmpty($s)){$true}else{-not((trim $s)-match"\S+")}} function Is-NonEmpty { process{-not($input|Is-Empty)} } function Is-NoTerm { process{$input|Is-Empty} } function Is-Pfx ($pfx) { process{([string]$input).StartsWith($pfx) }} function Is-Sfx ($sfx) { process{([string]$input).EndsWith($sfx) }} function Jn-CrLf { $input -join "`r`n" } function Is-Rmk { $input|Is-Pfx "--" } function Rmv-Dup { $o=$null; $input|foreach{if(-not(Has-Ele $o $_)){$o=Push $o $_}}; $o} function Jn-Pth { $ay=(Rmv-Empty $ay)|foreach{Rmv-PthSepPfxSfx $_}; (Jn-Sep $ay (PthSep)) + $_pthSep} function Jn-Sep ($sep) { $ay -join $sep} function Left ($len) { $_S::Left($s,$len) } function Len { $_S::Len($s) } function Mid ($pos,$len=-1) { if($len -lt 0){$_S::Mid($s,$pos)}else{$_S::Mid($s,$pos,$len)}} function Minus-Ay ($ay) { $input|foreach{if(-not($ay|Has-Ele $_)){$_}}} function Opn-Ft { param([Validateset("Read","Write","Append")]$for="Read") if($for -eq "Read"){$_FS::OpenTextFileReader($input)}else{$_FS::OpenTextFileWriter($input, ($for -eq "Append"))} } function Prepend-If ($pfx,$if) { process{ if($if){$pfx+$input}else{$input} }} function PthSep { $_pthSep } function Right ($len) { process{ $_S::Right($input,$len) }} function Rmv-3DashRmk { Rmv-Rmk $s "---" } function Rmv-2DashRmk { Rmv-Rmk $s "--" } function Rmv-Ele ($pfx) { $input.Where{-not($_|Is-Pfx $pfx)} } function Rmv-Empty { $input|foreach{if($_|Is-NonEmpty){$_}}} function Rmv-Rmk ($rmkPfx) { $input|Tak-FstOrAll $rmkPfx } function Rmv-PthSepPfxSfx { $input|Rmv-PfxSfx $_pthSep $_pthSep} function Rmv-PfxSfx ($pfx,$sfx) { $input|Rmv-Sfx $sfx|Rmv-Pfx $pfx } function Rmv-Pfx ($pfx) { If(Is-Pfx $s $pfx){Mid $s ((Len $pfx)+1)}else{$s}} function Rmv-Sfx ($sfx) { if(Is-Sfx $s $sfx){$a=Len $s; $b=Len $sfx; $n=$a-$b; Left $s $n}else{$s}} function Shell ([Microsoft.VisualBasic.AppWinStyle]$sty=$_Sty::Hide){$a=$_Interact::Shell($cmd,$sty,$false,-1) } function Shift-Term { process{$input|Brk-Spc1}} function Spc { $input|ty; $_S::Space($input)} function Split-CrLf { $input -split "`r`n" } function Srt-Ft { process{$input|Get-FtLy | sort }} # }| Wrt-Obj $ft } function Switch-Item { param($idx,[parameter(ValueFromRemainingArguments=$true)]$ap) $ap[$idx]} function Sz { ([System.Array]$input).Count} function Tak-Fst ($sep,[switch]$rev) { process{if($rev){($input|Brk-1 $sep -rev)[0]}else{($input|Brk-1 $sep)[0]} }} function Tak-FstOrAll ($sep,[switch]$rev) { process{if($rev){($input|Brk-1 $sep -rev)[0]}else{($input|Brk-1 $sep)[0]} }} function Tak-Snd ($sep,[switch]$rev) { process{if($rev){($input|Brk-2 $sep -rev)[1]}else{($input|Brk-2 $sep)[1]} }} function Tak-SndOrAll ($sep,[switch]$rev) { process{if($rev){($input|Brk-2 $sep -rev)[1]}else{($input|Brk-2 $sep)[1]} }} function Tmp-Fdr { $p = ((Tmp-Pth),$fdr,(Tmp-Nm))|Jn-Pth ; $p|Ens-Pth; $p } function Tmp-Fn { (Tmp-Nm) + $ext } function Tmp-Ft { (Tmp-Pth) + (".txt" |Tmp-Fn $fdr) } function Tmp-Fx { (Tmp-Pth) + (".xlsx"|Tmp-Fn $fdr) } function Tmp-Nm { "T" + ([System.DateTime]::Now.ToString("yyyy_MM_dd_HHmmss")) } function Tmp-Pth { process{($_tmpPth,$input)|Jn-Pth }} function Trim { process{([string]$input).Trim()} } function Ty { process{if($input-eq $null){}else{$input.GetType()}} } function TyNm { process{if($input-eq $null){'$null'}else{([System.Type]$input.GetType()).FullName}} } function Ub { ($input|Sz) - 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()}} $local:_tmpPth = .{$o = Jn-Pth ($env:Tmp,"mypowershell"); Ens-Pth $o; $o} # srt-Ft $MyInvocation.InvocationName |