lib/Backup/Functions1.ps1
#requires -version 5.0 using namespace Microsoft.VisualBasic using namespace System.IO using namespace Microsoft.VisualBasic.FileIO 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:_pthSep = [System.IO.Path]::DirectorySeparatorChar.ToString() function Get-Switch($switch){if($switch){$true}else{$false}} function Get-MaxEle($ay) {$o=$null;$ay|foreach{if($o -lt $_){$o=$_}};$o} function Get-Max($a,$b) {if($a -gt $b){$a}else{$b} } function ReSz-Ay($ay,$newUb) {$ub = ub $ay; if($newUb-gt $ub){return $ay+(New-Object object[] ($newUb-$ub))};if($newUb-lt $ub){return $ay[0..$newUb]};$ay} function Get-Wdt($drAy) { $o=@() $ub = -1 $drAy | foreach { $dr = $_ $ub1 = ub $dr if($ub1 -gt $ub){$ub = $ub1; $o=ReSz-Ay $o $ub} (0..$ub) | foreach { $o[$_] = max ($o[$_]) (len ($dr[$_])) } } $o } function Is-FilNewer ($f1,$f2) { (!(Test-Path $f2)) -or ((dir $f1).LastWriteTime -gt (dir $f2).LastWriteTime) } function Split-Spc ($s) { (Trim $s) -split '\s+' } function Is-SomTrue ($ay) { $somT =$false;foreach($_ in $ay){if( $_){$somT=$true ;break}};$somT} function Is-AllTrue ($ay) { $allT =$true ;foreach($_ in $ay){if(!$_){$allT=$false;break}};$allT} function Is-SomFalse ($ay) { $somF =$false;foreach($_ in $ay){if(!$_){$somF=$true ;break}};$somF} function Is-AllFalse ($ay) { $allF =$true ;foreach($_ in $ay){if( $_){$allF=$false;break}};$allF} function Get-FixWdtFmtStr([int[]]$wdt,$qqStr=$null){ if(Is-Empty $qqStr) { $i=0 $ay = $wdt|foreach { "{$i,-$_}" $i+=1 } return $ay -join '' } $o=$qqStr $i=0 $wdt|foreach { $o = Repl-Sub $o '?' "{$i,-$_}" -cnt 1 $i+=1 } $o -join '' } function Add-FnSfx ($fn,$sfx) { $a,$b=Brk-2 $fn "." -rev; $a+$sfx+"."+$b} function Rmv-Ext ($fn) { Get-FstOrAll $fn "." -rev } function Repl-Ext ($fn,$newExt) { (Rmv-Ext $fn) + $newExt } function About ($hlp) { cls; help about_$hlp } 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,[switch]$rev) { $p= instr($s=trim $s) $sep -rev:$(sw $rev);if($p-eq0){$s,""}else{Brk-At $s $p (len $sep)}} function Brk-2 ($s,$sep,[switch]$rev) { $p=instr ($s=trim $s) $sep -rev:$(sw $rev);if($p-eq0){"",$s}else{Brk-At $s $p (len $sep)}} function Get-Ay { $args } 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,[switch]$rev=$false) { Brk-1 $s " " $rev} function Brk-Spc2 ($s,[switch]$rev=$false) { Brk-2 $s " " $rev} function Brw-Ft ($ft) { Shell "notepad.exe ""$(Get-FullFn $ft)""" -sty NormalFocus } function Brw-Obj ($obj) { $ft=Get-TmpFt;$f=Opn-Ft $ft -for Write; $obj|foreach{$f.WriteLine($_)}; $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(Get-TmpPth) } function Ens-Pth ($pth) { if(-not(Test-Path $pth)) { $a = md $pth } } function Get-FstChr ($s) { Left $s 1 } function Get-LasChr ($s) { Right $s 1 } function Push-NonEmpty([object[]]$ay,$s) { if(Is-Empty $s){$ay}else{$ay+$s} } function Get-Idx ($ay,$ele) { ([System.Array]$ay).IndexOf($ele) } function Cnt-Sub ($s,$sub) { $fm=1 $o=0; $l = Len $sub while(($p = Instr $s $sub -fmPos $fm) -gt 0) { $o += 1 $fm = $p + $l } $o } function Fmt-FixWdt { param( [int[]] [Parameter(Mandatory=$true)]$wdt, [object[]] $dta, [string] $qqStr=$null, [switch] $sqQuote) if($sqQuote) { $dta = Quote-Ay $dta '[]' } $wdt1 = $wdt | foreach { Fmt-QQ "{0,-?}" $_ } $ub = ub $wdt1 $dta1 = (0..$ub) | foreach { $wdt1[$_] -f $dta[$_] } if($qqStr-eq $null) {$qqStr = Get-StrRepeat '?' ($ub+1) } Fmt-QQ $qqStr $dta1 } function Brk-Quote ($quote) { switch(Len $quote) { 1 {return $quote,$quote} 2 {return (Left $quote 1), (Right $quote 1)} 0 {return '',''} } $o = Brk-1 $quote "*" if($o[1]-eq ''){throw "`$quote[$quote] should have len = 0 1 2 or has *"} $o } function Get-Ay($obj,[string[]]$propertyName) { process { $propertyName | foreach {$obj.$_}} } function Get-PSCusObj([object[]]$propertyValues,[string[]]$propertyName) { process { $o=@{}; $ub=ub $propertyName; (0..$ub) | foreach {$o.Add($propertyName.$_,$propertyValues.$_)}; [PSCustomObject]$o }} function Fmt-QQ($qqStr,$ay) { $ay | foreach {$qqStr = Repl-Sub $qqStr '?' $_ -cnt 1 }; $qqStr } function Fmt-Macro($macroStr,$hash) { return; $hash.Keys|foreach{Fmt-QQ $qqStr $_ $hash[$_] } } function Quote-Str($s,$bkt) { $1,$2 = Brk-Quote $bkt; $1+$s+$2 } function Quote-Ay ($ay,$bkt) { $1,$2 = Brk-Quote $bkt; $ay|foreach{$1+$_+$2} } function Rmv-2DotInPth { param([string][Parameter(Mandatory=$true)] $pth) $pth = Rmv-PthSepPfxSfx $pth 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 += ($a[$_]) } } $pth = ($o -join $_pthSep) + $_pthSep }; $pth } function Stringify-Ay($ay) {$ay|foreach{"$_"}} function Get-MacroAy($macroStr, [switch]$inclBkt) { $ay = ($macroStr -split '\{') $o = $ay | foreach{ if(Has-Sub $_ '}') { $o = Get-Fst $_ '}' if($inclBkt) { $o = Quote-Str $o '[]' } $o } } Rmv-Dup $o } function Push-NoDup ($ay,$s) { if((Is-Empty $s)-or($ay-contains $s)){return $ay}; $ay+(,$s) } function Rmv-LasChr ($s) { if((Len $s)-eq 0){""}else{Left $s ((Len $s)-1)} } function Repl-Sub ($s,$sub,$by,$beg=1,$cnt=-1) { [Strings]::Replace($s,$sub,$by,$beg,$cnt) } function Repl-QQ ($qqStr,$ay) { $o=$qqStr;$ay|foreach {$o = Repl-Sub $o '?' $_ -cnt 1}; $o} function Ens-PthSfx ($pth) { if(Is-Sfx $pth $_pthSep){$pth}else{$pth+$_pthSep}} function Get-FullPth ($pth) { $o=if(Has-Sub $pth ':'){$pth}else{$pwd.Path+$_pthSep+$pth};Get-NormPth $o } function Get-NormPth ($pth) { $o=Repl-Sub $pth '\\' '\'; $o = Repl-Sub $o '\.\' '\';$o = Rmv-2DotInPth $o; Ens-PthSfx $o } function Get-FullFn ($fn) { if(Is-Sfx $fn $_pthSep){throw "`$Ffn[$ffn] cannot ends with \"}Rmv-LasChr(Get-FullPth $fn)} function Get-PthAy ($pth,$fspec='*.*') { [System.IO.Directory]::GetDirectories((Get-FullPth $pth),$fspec) } function Get-FfnAy ($pth,$fspec='*.*') { [System.IO.Directory]::GetFiles((Get-FullPth $pth),$fspec) } function Get-Term ($s,$atMost=2) { (Trim $s) -split '\s+',$atMost } function Get-3Term ($s) { Get-Term $s 3 } function Get-Dft ($v,$dft) { if(Is-Empty $v){$dft}else{$v} } function Get-FfnExt ($ffn) { $p = InstrRev $ffn "."; if($p -eq 0){""}else{mid $ffn $p} } function Get-FfnFn ($ffn) { $p = InstrRev $ffn (PthSep); if($p -eq 0){$ffn}else{mid $ffn ($p+1)} } function Get-FfnPth ($ffn) { $p = InstrRev $ffn (PthSep); if($p -eq 0){""} else{left $ffn $p} } function Get-FtLy ($ft) { [File]::ReadAllLines($ft) } function Get-Str ($ft) { [File]::ReadAllText($ft) } function Get-Script ($fnNm) { (Get-Item function:$fnNm).ScriptBlock } function Has-2Dash ($s) { Has-Sub $s "--" } function Has-3Dash ($s) { Has-Sub $s "---" } function Has-Spc ($s) { Has-Sub $s " " } function Has-Sub ($s,$sub) { $s.Contains($sub) } function Get-StrInstr ($s,$sub,[switch]$rev,[int32]$fmPos=1) { if($rev) {InstrRev $s $sub} else {[Strings]::Instr($fmPos,$s,$sub,[Microsoft.VisualBasic.CompareMethod]::Text)} } function Get-StrInstrRev ($s,$sub,$fmPos=-1) { [Strings]::InstrRev($s,$sub,$fmPos,[Microsoft.VisualBasic.CompareMethod]::Text) } function Is-2DashRmk ($s) { Is-Pfx $s "--" } function Is-1Term ($s) { !(Is-Empty $s) } function Is-3Term ($s) { $o=$true;foreach($i in 1..3){$noTerm,$s=Is-NoTerm $s;if($noTerm){$o=$false;break}};$o } 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) { Is-Empty $s } function Is-Pfx ($s,$pfx) { ([string]$s).StartsWith($pfx) } function Is-Sfx ($s,$sfx) { ([string]$s).EndsWith($sfx) } function Jn-CrLf ($ay) { $ay -join "`r`n" } function Is-Rmk ($s) { Is-Pfx $s "--" } function Rmv-Dup ($ay) { $o=@(); $ay|foreach{if(-not($o -ccontains $_)){$o=$o+$_}}; $o} function Jn-Pth ($ay) { $ay=(Rmv-Empty $ay)|foreach{Rmv-PthSepPfxSfx $_}; ($ay -join $_pthSep) + $_pthSep} function Get-StrLeft ($s,$len) { [Strings]::Left($s,$len) } function Get-StrLen ($s) { [Strings]::Len($s) } function Get-StrMid ($s,$pos,$len=-1) { if($len -lt 0){[Strings]::Mid($s,$pos)}else{[Strings]::Mid($s,$pos,$len)}} function Minus-Ay ($ay1,$ay2) { $ay1|foreach{if(-not(Has-Ele $ay2 $_)){$_}}} function Opn-Ft { param($ft,[Validateset("Read","Write","Append")]$for="Read") if($for -eq "Read"){[FileSystem]::OpenTextFileReader($ft)}else{[FileSystem]::OpenTextFileWriter($ft, ($for -eq "Append"))} } function Prepend-If ($s,$pfx,$if) { if($if){$pfx+$s}else{$s} } function Get-PthSep { $_pthSep } function Get-StrRight1 ($s,$len) { [Strings]::Right($s,$len) <# .Description abc #> } help get-StrRight1 return 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) { Get-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,[AppWinStyle]$sty=[AppWinStyle]::Hide){$a=[Interaction]::Shell($cmd,$sty,$false,-1) } function Get-StrSpc ($nSpc) { [Strings]::Space($nSpc) } function Split-CrLf ($lines) { $lines -split "`r`n" } function Srt-Ft ($ft) { Get-FtLy $ft | sort } # }| Wrt-Obj $ft } function Switch-Itm { param($idx,[parameter(ValueFromRemainingArguments=$true)]$ap) $ap[$idx]} function Get-AySz ($ay) { [Array]$ay.Count } function Get-Fst ($s,$sep,[switch]$rev) { if($rev){(Brk-1 $s $sep -rev)[0]}else{(Brk-1 $s $sep)[0]} } function Get-FstOrAll ($s,$sep,[switch]$rev) { if($rev){(Brk-1 $s $sep -rev)[0]}else{(Brk-1 $s $sep)[0]} } function Get-Snd ($s,$sep,[switch]$rev) { if($rev){(Brk-2 $s $sep -rev)[1]}else{(Brk-2 $s $sep)[1]} } function Get-SndOrAll ($s,$sep,[switch]$rev) { if($rev){(Brk-2 $s $sep -rev)[1]}else{(Brk-2 $s $sep)[1]} } function Get-TmpFdr ($fdr) { $p = Jn-Pth ((Get-TmpPth),$fdr,(Get-TmpNm)); Ens-Pth $p; $p } function Get-TmpFn ($ext) { (Get-TmpNm) + $ext } function Get-TmpFt ($fdr) { (Get-TmpPth) + (Get-TmpFn ".txt" $fdr) } function Get-TmpFx ($fdr) { (Get-TmpPth) + (Get-TmpFn ".xlsx" $fdr) } function Get-TmpNm { "T" + ([System.DateTime]::Now.ToString("yyyy_MM_dd_HHmmss")) } function Get-TmpPth ($fdr) { Jn-Pth ($_tmpPth,$fdr) } function Get-StrTrim ($s) { ([string]$s).Trim() } function Get-Type { param([Parameter(ValueFromPipeLine=$true)]$obj)process{if($obj-eq $null){}else{$obj.GetType()}} } function Get-TyNm ($obj) { if($obj-eq $null){'$null'}else{([System.Type]$obj.GetType()).FullName} } function Get-AyUb ($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()}} function Local:a($alias,$for) { Set-Alias $alias $for -Scope Global } a tmpPth Get-TmpPth a tmpFx Get-TmpFx a tmpFdr Get-tmpFdr a tmpNm Get-tmpNm a tmpFn Get-Fn a sz Get-AySz a ub Get-AyUb a wo Wrt-Obj a brwFt Brw-Ft a left Get-StrLeft a right Get-StrRight a mid Get-StrMid a len Get-StrLen a trim Get-StrTrim a ty Get-Type a tyNm Get-TyNm a ayMaxEle Get-MaxEle a pthSep Get-PthSep a instrRev Get-StrInstrRev a instr Get-StrInStr a ffnPth Get-FfnPth a ffnFn Get-FfnFn a ffnExt Get-FfnExt a ffnFnn Get-FfnFnn a ay Get-Ay a max Get-Max a sw Get-Switch $local:_tmpPth = $env:Tmp + '\' + 'mypowershell' if(!(Test-Path $_tmpPth)){md $_tmpPth} # srt-Ft $MyInvocation.InvocationName #> |