none
Get-ChildItem -File -Recurse -LiteralPath "C:\test [1]" does not return any information RRS feed

  • Question

  • I'm running Powershell v3.0 on a Windows 7 machine

    I have a script to return recursive file counts for all directories for a give path:

    $myPath = Get-ChildItem -Directory

    foreach ($subDirectory in $myPath) {

        Write-Output Get-ChildItem -File -Recurse -LiteralPath $subDirectory.FullName | Measure-Object -Property length | Select-Object -ExpandProperty Count

    }

    This script works great until I have directories with square brackets ("[" or "]") in the directory name.  In that case, nothing is returned.  In fact, running the following script for a real folder with files in it displays nothing on the console when there are definitely files in this directory:

        Get-ChildItem -File -Recurse -LiteralPath ".\test [1]"

    Any ideas?  Is this a known bug?  I've searched this forum and others and have found nobody with the same issue, so I suspect I'm missing something.

    Thanks in advance,

    Ryan

    Monday, May 6, 2013 4:00 PM

Answers

  • Ended up escaping all brackets and using the normal -Path attribute:


    $myPath = Get-ChildItem -Directory

    foreach ($subDirectory in $myPath) {

        Write-Output Get-ChildItem -File -Recurse -Path $subDirectory.FullName.Replace("[","``[").Replace("]","``]") | Measure-Object -Property length | Select-Object -ExpandProperty Count

    }

    I think there's a bug using the -LiteralPath attribute; after reading the following technet article, I realized how few characters would need escaped for the solution and just decided on good ol' .Replace:

    http://msdn.microsoft.com/en-us/library/windows/desktop/aa717088%28v=vs.85%29.aspx


    - Ryan


    • Marked as answer by EBGRyan Monday, May 6, 2013 7:54 PM
    • Edited by EBGRyan Monday, May 6, 2013 7:55 PM
    Monday, May 6, 2013 7:54 PM

All replies

  • Does this help as a start ? Quoted from http://technet.microsoft.com/en-us/library/ff730956.aspx

    $myPath = Get-ChildItem -Directory 'c:\temp\*``[*'
    foreach ($subDirectory in $myPath) {
        Write-Output $subDirectory.FullName
        Write-Output Get-ChildItem -File -Recurse -LiteralPath $subDirectory.FullName | Measure-Object -Property length | Select-Object -ExpandProperty Count
    }

    • Proposed as answer by ImMax Monday, May 6, 2013 4:25 PM
    Monday, May 6, 2013 4:19 PM
  • The quoted query would return only directories containing square brackets, though I wanted my query to return all results (square brackets or not).  However I may be able to use this to concatenate results with a query for folders not containing square brackets.  I would rather not do the concatenation of results for the following reasons:

    1.  There are at least 4 separate queries I would have to write: no brackets, only the opening bracket "[", only the closing bracket "]", and containing both brackets.

    2.  I hate work-arounds, but I'll use them if the "correct" way does not work

    Thanks for the idea, I may use it if there is indeed no single-line way of doing it.


    - Ryan


    • Edited by EBGRyan Monday, May 6, 2013 4:48 PM
    Monday, May 6, 2013 4:47 PM
  • how about this? or am i missing something

    Get-ChildItem -Recurse -Path ".\test*"


    MK


    • Edited by ImMax Monday, May 6, 2013 5:11 PM
    Monday, May 6, 2013 5:10 PM
  • "Test [1]" was just an example, I actually won't know the names of the folders in advance.  Also, the issue is retrieving the individual (recursive) file counts for each directory in the current directory.

    - Ryan

    Monday, May 6, 2013 7:36 PM
  • Ended up escaping all brackets and using the normal -Path attribute:


    $myPath = Get-ChildItem -Directory

    foreach ($subDirectory in $myPath) {

        Write-Output Get-ChildItem -File -Recurse -Path $subDirectory.FullName.Replace("[","``[").Replace("]","``]") | Measure-Object -Property length | Select-Object -ExpandProperty Count

    }

    I think there's a bug using the -LiteralPath attribute; after reading the following technet article, I realized how few characters would need escaped for the solution and just decided on good ol' .Replace:

    http://msdn.microsoft.com/en-us/library/windows/desktop/aa717088%28v=vs.85%29.aspx


    - Ryan


    • Marked as answer by EBGRyan Monday, May 6, 2013 7:54 PM
    • Edited by EBGRyan Monday, May 6, 2013 7:55 PM
    Monday, May 6, 2013 7:54 PM
  • Did you ever report this as a bug?
    Saturday, October 5, 2013 8:39 PM
  • I just reported this. Vote here if you think this should be fixed: https://connect.microsoft.com/PowerShell/feedbackdetail/view/1707779/

    Monday, August 24, 2015 3:16 PM