Refactored Codes
Powershell script to print contents of a directory in tree format
In this blog, we will write a function, that will print the contents of a path in a tree fomat. This will traverse child-item of a directory. If the child-item is a file then it will print the name of the file, along with their creation time, their last access time and last write time. If it happens that the child-item is a directory then we recurse over the child directories to print their content and so on. To make it more clear, you can look at the screenshot to see the output. -- Print Single File --
Format-DirectoryTreeView -Path .\Archive\baseline.xml
-- Print Entire Directory --
Format-DirectoryTreeView -Path .\PSKoans
How do we achieve this? To print in the tree format, we have to maintain a variable to keep track of the level. ie $Level. Which allows us to shift the outout of our function towards right, to give a tree like hierarchy. The we use that level to print that many white spaces, before printing the name of the directory. And, we store that value in $space = ' ' * $Level; $Path is the path to the target directory, whose content we are going print.
  1. First we check, if the given path is valid. If it is not valid, then we do nothing.
  2. Then we check, if the Path is a File, if it is a file then print the Full Name of the file and return.
  3. If the path is a directory
    1. We get list of files from the directory
    2. If list of files is not empty, then we print the file names and all the relevant time related details.
    3. We get the list of directories from the current directory.
    4. if list of directories is not empty, then we call the function on each directory. Where $Path is child directory of current director and $Level is increased by 2.
 
function Format-DirectoryTreeView {
    param(
    [Parameter(Position=0)][int]$Level = 1,
    [Parameter(Position=1)][string]$Path = '.'
    )

    if(Test-Path $Path){
    $item = Get-Item -Path $Path;
    $itemType = $item.GetType().Name;
    if($itemType -eq 'FileInfo') {
        $fileInfo = [Ordered]@{Name = $item.FullName; CreatedTime = $item.CreationTime; LastAccessTime = $item.LastAccessTime; LastWriteTime = $item.LastWriteTime };
        Write-Output $fileInfo;
        return;
    }

    $space = ' ' * $Level;
    Write-Output "$space $($PSStyle.FileInfo.Directory)$($item.Name)$($PSStyle.Reset)";

    $files = Get-ChildItem -File -Path $Path;
    if($files -ne $null){
        if($files.length -gt 0) {
        # If we are here that means - Get-ChildItem has returned a list of fileinfo objects
        foreach($file in $files){
            Write-Output "$space    $($PSStyle.Foreground.Blue)`u{21B3} $($PSStyle.Formatting.Debug)$($file.Name)$($PSStyle.Reset) [Created At : $($file.CreationTime), Accessed At : $($file.LastAccessTime), Written At : $($file.LastWriteTime)]";
        }
        } else {
        # If we are here that means - this directory has only one fileinfo object.
        # when there is only one fileinfo object - 
        # then Get-ChildItem returns a scalar value - it does not return - a list or collection
        $file = $files[0];
        Write-Output "$space    $($PSStyle.Foreground.Blue)`u{21B3} $($PSStyle.Formatting.Debug)$($file.Name)$($PSStyle.Reset) [Created At : $($file.CreationTime), Accessed At : $($file.LastAccessTime), Written At : $($file.LastWriteTime)]";
        }
    }

    $directories = Get-ChildItem -Directory -Path $Path;
    if($directories.length -gt 0) {
        foreach($directory in $directories){
        Format-DirectoryTreeView -Level ($Level + 2) -Path $directory.FullName;
        }
    }
    }
}
The End.