ctl.ps1


<#PSScriptInfo
 
.VERSION 1.0
 
.GUID ea4b6fd4-cd3f-4b35-b632-15dd9bcb3bf4
 
.AUTHOR Cristian Sánchez
 
.COMPANYNAME Cristian Sánchez
 
.COPYRIGHT
 
.TAGS
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 
.PRIVATEDATA
 
#>


<#
 
.DESCRIPTION
 Script to display directories in tree form.
 
#>
 
Param()

Param($directory,  
    [parameter(Mandatory = $false)]
    [switch]$undefinedLine)


<#
 
$console = $host.ui.rawui
$console.backgroundcolor = "black"
$console.foregroundcolor = "white"
$colors = $host.privatedata
$colors.verbosebackgroundcolor = "Black"
$colors.verboseforegroundcolor = "Green"
$colors.warningbackgroundcolor = "Red"
$colors.warningforegroundcolor = "white"
$colors.ErrorBackgroundColor = "Yellow"
$colors.ErrorForegroundColor = "Red"
set-location C:\
clear-host
#>




$multi = ".mp4", ".mp3", ".wav", ".mov", ".avi", ".mkv", ".ae", ".flac", ".ogg", ".mpg", "mpeg", ".png", ".jpg", ".jpeg", ".gif", ".psd", ".svg", ".ai", ".bmp", ".kml", ".css", ".php"
$text = ".pdf", ".docx", ".xlxs", ".ptt", ".json", ".txt", ".md", ".gitignore", ".xml", ".class", ".log", ".odt"
$executable = ".exe", ".msi", ".ps1", ".js", ".dmg", ".sh", ".c", ".js", ".jar", ".bat", ".csh", ".ex", ".cmd", ".html", ".java", ".py", ".swift"
$data = ".rar", ".zip", ".7z", ".iso", ".csv", ".key", ".obj", ".3ds", ".db", ".sql", ".apk", ".rom", ".otf", ".fon", "ttf", ".dll", ".ico", ".conf", ".deb", ".bin", ".dat", ".lnk", ".vsch"

if(!$directory){
    #IF IS A DISC PATH LIKE C:\ OR D:\
    $directory = $pwd

    if($directory[$directory.length-1] -ne "[\]"){
        #IF IS A NORMAL PATH LIKE C:\EXAMPLE\
        $directory = "$pwd\"
    }
}


#CHECK THE FORMAT OF INPUT PATH
if(!$directory.endswith("\")){
    Write-Host " "
    Write-Host "Error: The path format is not valid." -ForegroundColor red -BackgroundColor Black
    Write-Host "Expected: " -ForegroundColor red -BackgroundColor Black
    Write-Host " + slash -> c:\path\ " -ForegroundColor red -BackgroundColor Black
    Write-Host " ~~ " -ForegroundColor red -BackgroundColor Black
    Write-Host " + quotes -> 'c:\path\' " -ForegroundColor red -BackgroundColor black
    Write-Host " ~ ~ " -ForegroundColor red -BackgroundColor black
    Write-Host "Example: " -ForegroundColor red -BackgroundColor Black
    Write-Host " + Absolute -> D:\path\ " -ForegroundColor red -BackgroundColor Black
    Write-Host " + Relative -> .\path\ " -ForegroundColor red -BackgroundColor Black
    Write-Host " + Spaces -> 'D:\path example\' " -ForegroundColor red -BackgroundColor Black

    break
}


#GET THE PATH INHERITANCE
function isInherited{

    Param([String]$route, [int]$count)

    $array = ($route -split("\\")).Length
    $array = $array - 1

    $new = $count - $array

    if($new -eq 0){
        return "-"
    }else {      
        if($undefinedLine){
            #TO GET UNDEFINED LINES
            $inherit = "-"    
            for($i = 0; $i -lt $new; $i++){
                $inherit = " |" + "$inherit"
            }
        }else {
            #TO GET DELIMITED BY FOLDERS LINES
            $inherit = "|-"
            for($i = 0; $i -lt $new; $i++){    
                $inherit = " " + "$inherit"
            }

        }
    }
        return $inherit
}

#GET THE PATH LENGTH
function getLength{
    Param($path)
    $ma = $path -split("\\")
    [int]$total = $ma.Length - 1
    return  $total
}

#GET THE FILE FORMAT
function getFormat(){
    Param($extension)
    $color = "white", "? "
    foreach ($element in $multi){
        if( $element -eq $extension){
            return "green", ""
        }
    }

    foreach ($element in $text){
        if( $element -eq $extension){
            return "cyan", ""
        }
    }

    foreach ($element in $executable){
        if( $element -eq $extension){
            return "magenta", ""
        }
    }

    foreach ($element in $data){
        if( $element -eq $extension){
            return "darkgreen", ""
        }
    }

    return  $color
}

#GET THE PATH TYPE AND WRITE-HOST
function getType{

    Param($path, $length)
    foreach($element in Get-ChildItem $path -force -ErrorAction SilentlyContinue | Sort-Object -Property Attributes -Descending){
            #Write-host $element.Attributes
        Try{
            if ($element.mode -match 'd'){
                #FOR DIRECTORIES

                $prefix = isInherited -route "$directory" -count $length
                Write-Host "|$prefix" -NoNewline
                Write-Host "$element" -ForegroundColor Yellow -BackgroundColor DarkGray

                #IF ERROR GETTING MORE SPACES THAN THE EXPECTED USE
                #$newPath = "$element\"
                $newPath = "$path$element\"
                $newLength = getLength -path $newPath
                getType -path $newPath -length $newLength
   
            }elseif($element.mode -notmatch 'h'){
                #FOR FILES
                $prefix = isInherited -route "$directory" -count $length
                $color = getFormat -extension $element.extension
                Write-Host "|$prefix" -NoNewline
                Write-Host $color[1] -NoNewline
                Write-Host "$element" -ForegroundColor $color[0]
                       
            }else{
                #FOR HIDDEN FILES
                $prefix = isInherited -route "$directory" -count $length
                Write-Host "|$prefix" -NoNewline
                Write-Host "" -NoNewline -ForegroundColor white
                Write-Host "$element" -ForegroundColor DarkGray
            }

        } Catch {
            Write-Host "Error: The File has unrecognized attribute" -ForegroundColor red -BackgroundColor Black
        }
    }
}

Write-Host "."
$temporal = getLength -path $DIRECTORY
$hidden = ""
getType -path $DIRECTORY -length $temporal -hidden $hidden
Write-Host " "