Functions2.ps1

<#PSScriptInfo
 
.VERSION 1.0.5
 
.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"
$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 $_){if(  &$script) {$some=$true ;break}};$some}
function Is-All   ($script)                        { $all =$true ;foreach($_ in $_){if(!(&$script)){$all =$false;break}};$all }
function Is-None  ($script)                        { $no  =$true ;foreach($_ in $_){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 $_ }
function Add-FnSfx    ($sfx)                   { $a,$b=Brk-2 $fn "." -rev; $a+$sfx+"."+$b}
function Rmv-Ext                              { process{$_ | Tak-FstOrAll "." -rev }}
function Repl-Ext     ($newExt)              { process{ ($_ | Rmv-Ext) + $newExt } }
function About                               { cls; help about_$_ }
function Append-If    ($sfx,$if)                { if($if){$_+$sfx}else{$_} }
function Brk          ($sep)                    { process{$p = $_|Instr $sep; if($p -eq -1){throw "no sep[$sep] in s[$_]"}; $_|Brk-At $p ($sep|Len) }}
function Brk-1        ($sep,[switch]$rev)       { process{
    $s=$_|Trim 
    $p=if($rev){$s| InstrRev $sep}else{$s| Instr $sep} 
    if($p-eq0)
        {$s,""}
    else
        {$s| Brk-At $p ($sep|Len)} }
}
function Brk-2        ($sep,[switch]$rev)       { process{
    $s = $_|Trim 
    $p = $s|Instr $sep $rev;
    if($p-eq 0) {"",$s} else { $s | Brk-At $p ($sep|Len) }}}
function Brk-At       ($pos,$sepLen)           { process{
     $a = ($_ | Left ($pos-1) | Trim)
     $b = ($_ | Mid ($pos+$Seplen) | Trim)
     return @($a,$b) }} 
function Brk-Spc                               { process{$_| Brk   " "} }
function Brk-Spc1                              { process{$_| Brk-1 " "} }
function Brk-Spc2                              { process{$_| Brk-2 " "} }
function Get-HashRoot ($hashO)                     { ([hashtable]$hashO) }
function Brw-Ft                                    { process{Shell "notepad.exe ""$($_|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(($_|Len)-eq 0){""}else{$_|Left(($_|Len)-1)}}}
function Repl-Sub     ($sub,$by,$beg=1,$cnt=-1)    { process{$_S::Replace($_,$sub,$by,$beg,$cnt)}}
function Ens-PthSfx                                { if($_|Is-Sfx $_pthSep){$_}else{$_+$_pthSep}}
function Get-FullPth                               { $o=if($_|Has-Sub ':'){$_}else{$pwd.Path+$_pthSep+$_};$o|Get-NormPth }
function Get-NormPth                               { $o=$_|Repl-Sub '\\' '\'; $o = Repl-Sub $o '\.\' '\';$o = Rmv-2DotInPth $o; Ens-PthSfx $o }
function Get-FullFn                              { if($_|Is-Sfx $_pthSep){throw "`$Ffn[$_] cannot ends with \"}Rmv-LasChr($_|Get-FullPth)}
function Get-PthAy    ($fspec='*.*')          { [System.IO.Directory]::GetDirectories(($_|Get-FullPth),$fspec) }
function Get-FfnAy    ($fspec='*.*')          { [System.IO.Directory]::GetFiles(($_|Get-FullPth),$fspec) }
function Get-Term     ($atMost=2)               { process {
    $o=$null
    foreach($i in 2..$atMost) 
    {
        $t,$s=$_ | Shift-Term
        $o = $o | Push-NonEmpty $t
        if(Is-Empty $s)
            {break}
    }
    $o
    if(Is-NonEmpty $s)
        {$s}
}}
function Get-3Term                           { process { $_|Get-Term 3 } }
function Get-Dft      ($dft)                 { if($_|Is-Empty){$dft}else{$v} }
function Get-FfnExt                          { $p = $_|InstrRev "."; if($p -eq 0){""}else{$_|Mid $p} }
function Get-FfnFn                           { $p = $_|InstrRev $_pthSep; if($p -eq 0){$ffn}else{$_|Mid ($p+1)} }
function Get-FfnPth                          { $p=$_|InstrRev $_pthSep; if($p-eq 0){""}else{$_|Left $p} }
function Get-FtLy                            { $_F::ReadAllLines($_) }
function Get-Str                             { $_F::ReadAllText($_) }
function Get-Script                         { process{(Get-Item function:$_).ScriptBlock} }
function Has-Ele      ($ele)                 { $o=$false;foreach($i in $ay){if($i-eq$s){$o=$true;break}};$o}
function Has-2Dash                             { process{$_|Has-Sub "--"} }
function Has-3Dash                             { process{$_|Has-Sub "---"} }
function Has-Spc                               { process{$_|Has-Sub " "} }
function Has-Sub      ($sub)                   { process{$_.Contains($sub)} }
function Instr        ($sub,[switch]$rev)      { process{if($rev){$_|InstrRev $sub}else{$_.IndexOf($sub)+1} }}
function InstrRev     ($sub)                   { process{$_.LastIndexOf($sub) + 1 }}
function Is-2DashRmk                           { process{$_|Is-Pfx "--" }}
function Is-1Term                              { process{!($_|Is-Empty) }}
function Is-3Term                              { process{$o=$true;foreach($i in 1..3){$noTerm,$s=$_|Is-NoTerm;if($noTerm){$o=$false;break}};$o }}
function Is-Empty                              { process{if([System.String]::IsNullOrEmpty($_)){$true}else{-not(($_|Trim)-match"\S+")}}}
function Is-NonEmpty                           { process{-not($_|Is-Empty)} }
function Is-NoTerm                             { process{$_|Is-Empty} }
function Is-Pfx       ($pfx)                   { process{([string]$_).StartsWith($pfx) }}
function Is-Sfx       ($sfx)                   { process{([string]$_).EndsWith($sfx) }}
function Jn-CrLf                               { $_ -join "`r`n" }
function Is-Rmk                                { process{$_|Is-Pfx "--"} }
function Rmv-Dup                               { $o=$null; $_|foreach{if(-not(Has-Ele $o $_)){$o=$o+$_}}; $o}
function Jn-Pth                               { $ay=(Rmv-Empty $ay)|foreach{Rmv-PthSepPfxSfx $_}; (Jn-Sep $ay (PthSep)) + $_pthSep}
function Jn-Sep       ($sep)                   { $_ -join $sep}
function Left         ($len)                    { process{$_S::Left($_,$len)} }
function Len                                   { process{$_S::Len($_)} }
function Mid          ($pos,$len=-1)            { process{if($len -lt 0){$_S::Mid($_,$pos)}else{$_S::Mid($_,$pos,$len)}}}
function Minus-Ay     ($ay)                  { $_|foreach{if(-not($ay|Has-Ele $_)){$_}}}
function Opn-Ft                                    { param([Validateset("Read","Write","Append")]$for="Read") if($for -eq "Read"){$_FS::OpenTextFileReader($_)}else{$_FS::OpenTextFileWriter($_, ($for -eq "Append"))} }
function Prepend-If   ($pfx,$if)                { process{ if($if){$pfx+$_}else{$_} }}
function PthSep                                    { $_pthSep }
function Right        ($len)                   { process{ $_S::Right($_,$len) }}
function Rmv-3DashRmk                          { process{ $_|Rmv-Rmk "---" }}
function Rmv-2DashRmk                          { process{ $_|Rmv-Rmk "--" }}
function Rmv-Ele      ($pfx)                   { $_.Where{-not($_|Is-Pfx $pfx)} }
function Rmv-Empty                             { process{$_|foreach{if($_|Is-NonEmpty){$_}}}}
function Rmv-Rmk      ($rmkPfx)                { process{$_|Tak-FstOrAll $rmkPfx }}
function Rmv-PthSepPfxSfx                      { process{$_|Rmv-PfxSfx $_pthSep $_pthSep}}
function Rmv-PfxSfx   ($pfx,$sfx)              { process{$_|Rmv-Sfx $sfx|Rmv-Pfx $pfx }}
function Rmv-Pfx      ($pfx)                   { process{If($_|Is-Pfx $pfx){$_|Mid (($pfx|Len)+1)}else{$_}}}
function Rmv-Sfx      ($sfx)                   { process{if($_|Is-Sfx $sfx){$a=$_|Len; $b=$sfx|Len; $n=$a-$b; $_|Left $n}else{$_}}}
function Shell        ([Microsoft.VisualBasic.AppWinStyle]$sty=$_Sty::Hide){$a=$_Interact::Shell($cmd,$sty,$false,-1) }
function Shift-Term                         { process{$_|Brk-Spc1}}
function Spc          ($n)                  { $_S::Space($n)} 
function Split-CrLf                        { $_ -split "`r`n" }
function Srt-Ft                            { process{$_|Get-FtLy | sort }} # }| Wrt-Obj $ft }}
function Switch-Item                       { param($idx,[parameter(ValueFromRemainingArguments=$true)]$ap) $ap[$idx]}

function Sz    { begin{$o=0}process{$o+=1}end{$o}}
12,3,4,4,4,4,5|Sz
return
function Tak-Fst      ($sep,[switch]$rev)  { process{if($rev){($_|Brk-1 $sep -rev)[0]}else{($_|Brk-1 $sep)[0]} }}
function Tak-FstOrAll ($sep,[switch]$rev)  { process{if($rev){($_|Brk-1 $sep -rev)[0]}else{($_|Brk-1 $sep)[0]} }}
function Tak-Snd      ($sep,[switch]$rev)  { process{if($rev){($_|Brk-2 $sep -rev)[1]}else{($_|Brk-2 $sep)[1]} }}
function Tak-SndOrAll ($sep,[switch]$rev)  { process{if($rev){($_|Brk-2 $sep -rev)[1]}else{($_|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,$_)|Jn-Pth }}
function Trim                                { process{([string]$_).Trim()} }
function Ty         ($obj)                   { if($obj-eq $null){}else{$obj.GetType()}}
function TyNm                                { process{if($_-eq $null){'$null'}else{([System.Type]$_.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
#>