Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can I include root folder to get-childitem output programmatically?

Tags:

powershell

If I run this simple command:

gci e:\mytree -r | select fullname

it gives this output:

E:\mytree\folder1                                                                                                                                                                      
E:\mytree\folder2                                                                                                                                                                      
E:\mytree\folder3                                                                                                                                                                      
E:\mytree\file1.txt                                                                                                                                                                    
E:\mytree\file12.txt                                                                                                                                                                   
E:\mytree\folder1\folder.with.dots                                                                                                                                                     
E:\mytree\folder1\folder.with.dots\file inside folder with dots.txt                                                                                                                    
E:\mytree\folder3\file4.doc     

Is there any way to include in the output even root folder, so that I could have

E:\mytree
E:\mytree\folder1                                                                                                                                                                      
E:\mytree\folder2                                                                                                                                                                      
E:\mytree\folder3                                                                                                                                                                      
E:\mytree\file1.txt                                                                                                                                                                    
E:\mytree\file12.txt                                                                                                                                                                   
E:\mytree\folder1\folder.with.dots                                                                                                                                                     
E:\mytree\folder1\folder.with.dots\file inside folder with dots.txt                                                                                                                    
E:\mytree\folder3\file4.doc    
like image 578
Nicola Cossu Avatar asked Apr 21 '11 10:04

Nicola Cossu


People also ask

How do you get the full Path of ChildItem?

Use the Get-ChildItem cmdlet in PowerShell to get the full path of the file in the current directory. Get-ChildItem returns one or more items from the specified location and using the file FullName property, it gets the full path of the file.

How do I Get-ChildItem to exclude folders?

To exclude directories, use the File parameter and omit the Directory parameter, or use the Attributes parameter. To get directories, use the Directory parameter, its "ad" alias, or the Directory attribute of the Attributes parameter. Gets files.

What is the command you would use to get help for the Get-ChildItem command?

The Get-ChildItem cmdlet gets the items in one or more specified locations. If the item is a container, it gets the items inside the container, known as child items. You can use the Recurse parameter to get items in all child containers and use the Depth parameter to limit the number of levels to recurse.

What are the files that should be visible in the root folder?

More About Root Folders & Directories The same concept applies here as on your local computer—the files and folders in this root folder contain the main web page files, such as HTML files, that should be displayed when someone accesses the main URL of the website.


3 Answers

get-childitem $root -recurse | foreach-object -begin { $arr = @((get-item $root).fullname) } -process { $arr+= $_.fullname } -end { $arr }

Using foreach-object cmdlet's begin switch, we do some work before handling the objects from get-childitem: we create an array and put the filepath of the root in there.

Then for each object in the pipeline, we append its filepath to the array.

Finally, we output the array to the pipeline.

like image 60
Daniel Richnak Avatar answered Oct 01 '22 08:10

Daniel Richnak


@(gi e:\mytree) + @(gci e:\mytree -r) | select fullname

@(..) forces the return value from each expression to be an array

like image 24
John Anson Avatar answered Oct 01 '22 06:10

John Anson


Another option is to just roll a function such as below.

Function Get-ItemTree {
    [CmdletBinding()]
    Param (
        [Parameter(Mandatory = $true)]
        [ValidateScript({if (Test-Path $_ -PathType 'Container'){$true}else{throw "Invalid Path: '$_'.  You must provide a valid directory."}})]
        [String]$Path
    )
    Get-Item -Path $Path
    Get-ChildItem -Path $Path -Recurse -OutBuffer 1000
}

You can add in any parameters you wish to pass through; such as a -Filter argument. Here I've hardcoded the assumption that if using this function you'd want it to be run on a container rather than leaf, you'd want to recurse, and may want to fetch a bunch of files at a time (giving a slight performance gain via the OutBuffer option).

It's not as sexy or clever as other solutions, but should be efficient, easy to reuse, easy to understand, and works for empty containers.

This is similar to your own answer, but has a few differences.

  • Parameter validation is done using the ValidateScript attribute designed for parameter validation.
  • Failed validation throws an error, rather than using Write-Host (a cmdlet you should generally avoid)
  • You build up an array of results, then return it, rather than feeding the output straight to the pipeline. For large directories this means you'll be creating a very large array in memory / you may hit performance issues or run out of memory.
  • You convert each file system object to a PSObject with a full name property, but don't really have a need for that. If you want to reduce the size of objects by removing un-required attributes you can simply pipe the output to Select-Object -Properties FullName or Select-Object -ExpandProperty FullName (the former creating the same output as your current function, the latter returning an array of strings holding the fullname value).

Long term, I've suggest this Enhancement to MS.

like image 30
JohnLBevan Avatar answered Oct 01 '22 06:10

JohnLBevan