locked
Get-childitem RRS feed

  • Question

  • Hi,

    I want to count the total number of files from an input file which contains UNC paths, i.e. \\server01\test\files

    How can this be done ?

    Thanks

    Bill

    Tuesday, September 6, 2011 9:21 PM

Answers

  • Something like this?

    $counter = 0
    get-content unclist.txt |
    foreach-object {
    $counter += (cmd /c dir $_ /b /a-d).count
    }


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    Tuesday, September 6, 2011 9:39 PM
  • Or this:

    foreach ($Path in Get-Content c:\InputFile.txt)
    {
      Write-Output "$path = $((Get-ChildItem -Path $Path).count)"
    }

    Karl


    My Blog: http://unlockpowershell.wordpress.com
    My Book: Windows PowerShell 2.0 Bible
    My E-mail: -join ("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})
    Tuesday, September 6, 2011 10:39 PM
  • Get-Content .\files.txt | Get-ChildItem | Where-Object {!$_.PSIsContainer} | Measure-Object
    Shay Levy [MVP]
    PowerShay.com
    PowerShell Toolbar
    Wednesday, September 7, 2011 8:54 AM

All replies

  • Something like this?

    $counter = 0
    get-content unclist.txt |
    foreach-object {
    $counter += (cmd /c dir $_ /b /a-d).count
    }


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    Tuesday, September 6, 2011 9:39 PM
  • Or this:

    foreach ($Path in Get-Content c:\InputFile.txt)
    {
      Write-Output "$path = $((Get-ChildItem -Path $Path).count)"
    }

    Karl


    My Blog: http://unlockpowershell.wordpress.com
    My Book: Windows PowerShell 2.0 Bible
    My E-mail: -join ("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})
    Tuesday, September 6, 2011 10:39 PM
  • Get-childitem will work, but for just getting a count of files it seems like swatting a fly with a Buick.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    Tuesday, September 6, 2011 10:42 PM
  • Get-Content .\files.txt | Get-ChildItem | Where-Object {!$_.PSIsContainer} | Measure-Object
    Shay Levy [MVP]
    PowerShay.com
    PowerShell Toolbar
    Wednesday, September 7, 2011 8:54 AM
  • Get-Content .\files.txt | Get-ChildItem | Where-Object {!$_.PSIsContainer} | Measure-Object
    Shay Levy [MVP]
    PowerShay.com
    PowerShell Toolbar
    • Edited by Shay Levi Wednesday, September 7, 2011 2:09 PM
    Wednesday, September 7, 2011 9:39 AM
  • It should also be noted that mjolinor's solution is the only one that does
    files only. both Karl and Shay include folders in their counts.
     
     

    Justin Rich
    http://jrich523.wordpress.com
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Wednesday, September 7, 2011 1:55 PM
  • True, thanks for the input, updating thread.
    Shay Levy [MVP]
    PowerShay.com
    PowerShell Toolbar
    Wednesday, September 7, 2011 2:07 PM
  • We also don't recurse....

    Of course, the original question did not meantion a recursive search, but did clearly say "Files" ;)

    Karl


    My Blog: http://unlockpowershell.wordpress.com
    My Book: Windows PowerShell 2.0 Bible
    My E-mail: -join ("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})
    Wednesday, September 7, 2011 2:32 PM