I have a script that accepts a list of paths to process as an array of strings. I'm using Get-ChildItem
to generate those paths. I've come across some interesting behavior in the conversion from System.IO.FileInfo
objects to strings.
Example 1:
PS C:\Users\Nikhil\Documents> [string[]](Get-ChildItem .\GitHub\)
toc
toc-gh-pages
Publish gh-pages.txt
Example 2:
PS C:\Users\Nikhil\Documents> [string[]](Get-ChildItem .\GitHub\*)
C:\Users\Nikhil\Documents\GitHub\toc
C:\Users\Nikhil\Documents\GitHub\toc-gh-pages
C:\Users\Nikhil\Documents\GitHub\Publish gh-pages.txt
Example 3: (With -Recurse, so the conversion to strings is illogical and useless)
PS C:\Users\Nikhil\Documents\GitHub> [string[]](Get-ChildItem .\toc-gh-pages -Recurse)
assets
css
lib
_layouts
_site
.gitattributes
.gitignore
index.html
_config.yml
jquery.toc.zip
docs.less
docs.min.css
google-code-prettify
jquery.toc
lang-apollo.js
...
Example 4:
PS C:\Users\Nikhil\Documents\GitHub> [string[]](Get-ChildItem .\toc-gh-pages\*.* -Recurse)
C:\Users\Nikhil\Documents\GitHub\toc-gh-pages\assets\jquery.toc.zip
C:\Users\Nikhil\Documents\GitHub\toc-gh-pages\css\docs.less
C:\Users\Nikhil\Documents\GitHub\toc-gh-pages\css\docs.min.css
C:\Users\Nikhil\Documents\GitHub\toc-gh-pages\lib\google-code-prettify\lang-apollo.js
C:\Users\Nikhil\Documents\GitHub\toc-gh-pages\lib\google-code-prettify\lang-basic.js
C:\Users\Nikhil\Documents\GitHub\toc-gh-pages\lib\google-code-prettify\lang-clj.js
C:\Users\Nikhil\Documents\GitHub\toc-gh-pages\lib\google-code-prettify\lang-css.js
C:\Users\Nikhil\Documents\GitHub\toc-gh-pages\lib\google-code-prettify\lang-dart.js
C:\Users\Nikhil\Documents\GitHub\toc-gh-pages\lib\google-code-prettify\lang-erlang.js
C:\Users\Nikhil\Documents\GitHub\toc-gh-pages\lib\google-code-prettify\lang-go.js
C:\Users\Nikhil\Documents\GitHub\toc-gh-pages\lib\google-code-prettify\lang-hs.js
C:\Users\Nikhil\Documents\GitHub\toc-gh-pages\lib\google-code-prettify\lang-lisp.js
C:\Users\Nikhil\Documents\GitHub\toc-gh-pages\lib\google-code-prettify\lang-llvm.js
C:\Users\Nikhil\Documents\GitHub\toc-gh-pages\lib\google-code-prettify\lang-lua.js
...
The addition of a wildcard to the path causes the resulting strings to be full path names, instead of just file/folder names. Why does this happen?
I understand how I can work around this behavior and get the info that I need; what I'm interested in is why this is happening, given that we're just converting System.IO.FileInfo
and System.IO.DirectoryInfo
objects to strings in all cases.
Logical answer:
You are casting the FileInfo
objects to strings. When you do gci path
, you have files and directories only from that folder. So it is ok to just convert to the name of the item.
When you are using gci path\*
, it is across folders, and just converting to names is not valid, so it includes the path.
Depending on whether you want name or the full path, do this:
gci $path | select -expand Name
or
gci $path | select -expand FullName
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With