helpers/File.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
function Get-FileLength { param ( [Parameter(Mandatory = $true)] $file ) $length = $null; if ($file -isnot [System.IO.DirectoryInfo]) { $length = $file.length; } if ($null -eq $length) { return ""; } elseif ($length -ge 1PB) { return ($length / 1PB).ToString("F") + 'PB'; } elseif ($length -ge 1TB) { return ($length / 1TB).ToString("F") + 'TB'; } elseif ($length -ge 1GB) { return ($length / 1GB).ToString("F") + 'GB'; } elseif ($length -ge 1MB) { return ($length / 1MB).ToString("F") + 'MB'; } elseif ($length -ge 1KB) { return ($length / 1KB).ToString("F") + 'KB'; } return $length.ToString() + 'B '; } function Get-FileName { param ( [Parameter(Mandatory = $true)] $file ) $name = $file.Name; if ( ($Global:ColorSettings.File.Types.SymbolicLink.ShowTarget -eq $true) ` -and ($file.Attributes -band [IO.FileAttributes]::ReparsePoint) ) { $target = $null; try { $target = Get-ShortenedPath (Get-Item -Path $file | Select-Object -ExpandProperty Target); } catch {} if ($null -ne $target) { $name = $name + ' -> ' + $target; } } return $name; } function Write-ColorizedFileLine { param ( [String] $color, [Parameter(Mandatory = $true)] $file ) Write-HostColor -ForegroundColor $color -Value ( "{0,-7} {1,21} {2,10} {3}" -f $file.Mode, ([String]::Format("{0,10} {1,5}", $file.LastWriteTime.ToString("d"), $file.LastWriteTime.ToString("t"))), (Get-FileLength $file), (Get-FileName $file) ); } function Write-FileHeader { param ( [Parameter(Mandatory = $true, Position = 1)] [String] $directory ) if ($Script:showHeader -eq $false) { return; } Write-Host; if ($Global:ColorSettings.File.Path.Visible -eq $true) { $currentDirectory = Get-ShortenedPath $directory; $pathTitleColor = $Global:ColorSettings.File.Path.TitleColor; $pathTextColor = $Global:ColorSettings.File.Path.TextColor; Write-HostColor -Value " Directory: " -ForegroundColor $pathTitleColor -NoNewline; Write-HostColor -Value "$currentDirectory" -ForegroundColor $pathTextColor; Write-Host; } if ($Global:ColorSettings.File.Header.Visible -eq $true) { $headerTextColor = $Global:ColorSettings.File.Header.TextColor; $headerSeparatorsColor = $Global:ColorSettings.File.Header.SeparatorsColor; Write-HostColor -Value "Mode LastWriteTime Length Name" -ForegroundColor $headerTextColor; Write-HostColor -Value "---- ------------- ------ ----" -ForegroundColor $headerSeparatorsColor; } $Script:showHeader = $false; } function Write-File { param ( [Parameter(Mandatory = $true, Position = 1)] [System.IO.FileSystemInfo] $file ) $regexOptions = ([System.Text.RegularExpressions.RegexOptions]::IgnoreCase); $binary = New-Object System.Text.RegularExpressions.Regex($Global:ColorSettings.File.Types.Binary.RegEx, $regexOptions); $code = New-Object System.Text.RegularExpressions.Regex($Global:ColorSettings.File.Types.Code.RegEx, $regexOptions); $compressed = New-Object System.Text.RegularExpressions.Regex($Global:ColorSettings.File.Types.Compressed.RegEx, $regexOptions); $text = New-Object System.Text.RegularExpressions.Regex($Global:ColorSettings.File.Types.Text.RegEx, $regexOptions); $hidden = New-Object System.Text.RegularExpressions.Regex($Global:ColorSettings.File.Types.Hidden.RegEx, $regexOptions); if ($file -is [System.IO.DirectoryInfo]) { $currentDirectory = $file.Parent.FullName; } else { $currentDirectory = $file.DirectoryName; } Write-FileHeader $currentDirectory; if ($file.Attributes -band [IO.FileAttributes]::ReparsePoint) { Write-ColorizedFileLine $Global:ColorSettings.File.Types.SymbolicLink.Color $file; } elseif ($hidden.IsMatch($file.Name)) { Write-ColorizedFileLine $Global:ColorSettings.File.Types.Hidden.Color $file; } elseif ($file -is [System.IO.DirectoryInfo]) { Write-ColorizedFileLine $Global:ColorSettings.File.Types.Directory.Color $file; } elseif ($binary.IsMatch($file.Name)) { Write-ColorizedFileLine $Global:ColorSettings.File.Types.Binary.Color $file; } elseif ($code.IsMatch($file.Name)) { Write-ColorizedFileLine $Global:ColorSettings.File.Types.Code.Color $file; } elseif ($compressed.IsMatch($file.Name)) { Write-ColorizedFileLine $Global:ColorSettings.File.Types.Compressed.Color $file; } elseif ($text.IsMatch($file.Name)) { Write-ColorizedFileLine $Global:ColorSettings.File.Types.Text.Color $file; } else { Write-ColorizedFileLine $Global:ColorSettings.File.DefaultColor $file; } } |