none
Script for Capture information of Folders and files and Export to Excel RRS feed

  • General discussion

  • Hi Guys,

    I need help for create some script to capture the amount of folders and files in a network directory and export to an Excel file.

    I don't need to see the folders and files, just the amount of files and folders/subfolders.

    Then i will incluide this to a HTA Script with a better view for the user choose the quantity, files and folder that he wants to see and export to a Excel File with a specify format on tables.

    If anyone can help me, I will be very grateful !
    • Changed type Bill_Stewart Tuesday, April 14, 2020 1:29 PM
    • Moved by Bill_Stewart Tuesday, April 14, 2020 1:29 PM This is not "scripts on demand"
    Wednesday, August 21, 2019 1:55 PM

All replies

  • First post at top of forum:

    This forum is for scripting questions rather than script requests


    -- Bill Stewart [Bill_Stewart]

    Wednesday, August 21, 2019 2:07 PM
  • $RemotePath = "c:\temp"
    $ExportFilePath = "c:\temp\output.csv"
    $tree = Get-ChildItem -Path $RemotePath -Recurse
    Write-Host "Number of Directory and files in $RemotePath : " -ForegroundColor Green -NoNewline
    Write-host "$($tree.count)" -ForegroundColor Yellow
    Write-host "Exporting list in a .csv file" -ForegroundColor green
    $tree | Export-Csv -Path $ExportFilePath -Encoding UTF8 -Delimiter ";" -NoTypeInformation # using -encoding to avoid of accents. using -delimiter because in Fr, it's easier to improve presentation with excel. 
    

    Something simple like this ? I don't think so.

    $tree = Get-ChildItem -Path $RemotePath -Recurse -Directory 
    $result = @()  # initialize a array
    
    foreach ($dir in $tree)
        {
        $object = New-Object -TypeName PSObject # create a PSObject and feed it
        $object | Add-Member -MemberType NoteProperty -Name 'Dir'              -Value $($dir.name)
        $object | Add-Member -MemberType NoteProperty -Name 'DirFullName'      -Value $($dir.Fullname)
        $object | Add-Member -MemberType NoteProperty -Name 'NberOfFilesInDir' -Value (Get-ChildItem -Path $($dir.fullName) -File).count
        $result += $object # add the PSObject to the Array
        }
    $result
    $result | Export-Csv -Path $ExportFilePath -Encoding UTF8 -Delimiter ";" -NoTypeInformation # using -encoding to avoid of accents. using -delimiter because in Fr, it's easier to improve presentation with excel. 
    
    

    Perhaps something like this.

    Oliv

    Wednesday, August 21, 2019 4:18 PM
  • $result = @() # initialize a array

    There's no need to create an empty array and continuously append new objects to it.

    This pattern is unfortunately common and probably comes from trying to emulate other languages. PowerShell's streaming capability performs better and renders this pattern unnecessary in most cases.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, August 21, 2019 4:53 PM