Note that there are some explanatory texts on larger screens.

plurals
  1. POConverting Get-ChildItem output to string[]
    text
    copied!<p>I have a script that accepts a list of paths to process as an array of strings. I'm using <code>Get-ChildItem</code> to generate those paths. I've come across some interesting behavior in the conversion from <code>System.IO.FileInfo</code> objects to strings.</p> <p>Example 1:</p> <pre><code>PS C:\Users\Nikhil\Documents&gt; [string[]](Get-ChildItem .\GitHub\) toc toc-gh-pages Publish gh-pages.txt </code></pre> <p>Example 2:</p> <pre><code>PS C:\Users\Nikhil\Documents&gt; [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 </code></pre> <p>Example 3: (With -Recurse, so the conversion to strings is illogical and useless)</p> <pre><code>PS C:\Users\Nikhil\Documents\GitHub&gt; [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 ... </code></pre> <p>Example 4:</p> <pre><code>PS C:\Users\Nikhil\Documents\GitHub&gt; [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 ... </code></pre> <p>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?</p> <p>I understand <em>how</em> I can work around this behavior and get the info that I need; what I'm interested in is <em>why</em> this is happening, given that we're just converting <code>System.IO.FileInfo</code> and <code>System.IO.DirectoryInfo</code> objects to strings in all cases.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload