Simpatie.ro - matrimoniale
Mrrrr's Forum (VIEW ONLY)
Un forum care ofera solutii pentru unele probleme legate in general de PC. Pe langa solutii, aici puteti gasi si alte lucruri interesante // A forum that offers solutions to some PC related issues. Besides these, here you can find more interesting stuff.
Nou pe simpatie:
misha_light
Femeie
25 ani
Bucuresti
cauta Barbat
26 - 47 ani
Mrrrr's Forum (VIEW ONLY)ReguliInregistrareLoginPozeNu sunteti logat. Lista Forumurilor Pe Tematici
Mrrrr's Forum (VIEW ONLY) / Tutoriale si Ghiduri Utile // Tutorials and useful guides /

[WINDOWS] Generate a List of Folders on My External HDDs

Pagini: 1 Moderat de TRaP, TonyTzu
pus acum 21 ore#1
Mrrrr
AdMiN
Postari: 2392
I want to put a CSV list of folders with sizes on my external HDDs and have an Excel file on my PC using PowerQuery to get those CSV files into a user friendly form.

So I made this approach:
1. Each external HDD gets 2 files in the root of the HDD:
GenerateFolderSizes.ps1
GenerateFolderSizes.cmd
2. Running GenerateFolderSizes.cmd generates a CSV file with a given name, different for each external HDD. I set the names manually from the code inside GenerateFolderSizes.ps1
3. I have an Excel file on my PC that gets the contents of all the CSV files across any external HDDs I might have into a centralized Excel Table.

GenerateFolderSizes.cmd is shorter, so I'll add the code to it first:

@echo off

powershell.exe -NoProfile -ExecutionPolicy Bypass -File "%~dp0GenerateFolderSizes.ps1"

echo.
echo ============================================================
echo Press any key to close this window...
echo ============================================================
pause >nul


GenerateFolderSizes.ps1

# ============================================================
# Generates a CSV containing:
#   - First-level folders
#   - First-level subfolders ONLY for folders containing
#     "KEY_WORD" in their name
#   - Size in GB
#
# The CSV is overwritten every time.
# ============================================================

# ------------------------------------------------------------
# EDIT THE VARIABLE BELOW FOR EACH EXTERNAL HDD
# ------------------------------------------------------------
# The CSV will be created in the same folder as this PS1 file.
# ------------------------------------------------------------

$CsvFileName = "2 TB HDD A.csv"

# ------------------------------------------------------------
# DO NOT EDIT BELOW THIS LINE UNLESS YOU KNOW WHAT YOU ARE DOING
# ------------------------------------------------------------

# Temporarily allow locally-created scripts for the current user
try {
    Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned -Force -ErrorAction SilentlyContinue
}
catch {
    # Continue even if the policy cannot be changed
}


# Make sure the execution policy is reset when the script finishes
try {

    # --------------------------------------------------------
    # Find the folder where THIS script is located
    # --------------------------------------------------------

    $ScriptFolder = Split-Path -Parent $MyInvocation.MyCommand.Definition

    $CsvPath = Join-Path $ScriptFolder $CsvFileName


    # --------------------------------------------------------
    # Display header
    # --------------------------------------------------------

    Clear-Host

    Write-Host ""
    Write-Host "============================================================"
    Write-Host "             FOLDER SIZE SCAN"
    Write-Host "============================================================"
    Write-Host ""

    Write-Host "Location:"
    Write-Host $ScriptFolder
    Write-Host ""

    Write-Host "CSV file:"
    Write-Host $CsvPath
    Write-Host ""

    Write-Host "Scanning folders..."
    Write-Host ""


    # --------------------------------------------------------
    # Get all first-level folders
    # --------------------------------------------------------

    $FirstLevelFolders = @(Get-ChildItem `
        -LiteralPath $ScriptFolder `
        -Directory `
        -Force `
        -ErrorAction SilentlyContinue)


    $Results = foreach ($folder in $FirstLevelFolders) {

        # ----------------------------------------------------
        # If the first-level folder contains "KEY_WORD",
        # scan its immediate subfolders.
        # ----------------------------------------------------

        if ($folder.Name -like '*KEY_WORD*') {

            $subfolders = @(Get-ChildItem `
                -LiteralPath $folder.FullName `
                -Directory `
                -Force `
                -ErrorAction SilentlyContinue)


            if ($subfolders.Count -gt 0) {

                foreach ($subfolder in $subfolders) {

                    Write-Host "Scanning: $($folder.Name)\$($subfolder.Name)"

                    $size = (Get-ChildItem `
                        -LiteralPath $subfolder.FullName `
                        -File `
                        -Recurse `
                        -Force `
                        -ErrorAction SilentlyContinue |
                        Measure-Object -Property Length -Sum).Sum

                    if ($null -eq $size) {
                        $size = 0
                    }


                    [PSCustomObject]@{
                        'First Folder' = $folder.Name
                        'Subfolder'    = $subfolder.Name
                        'Size (GB)'    = [math]::Round($size / 1GB, 2)
                    }
                }

            }
            else {

                # KEY_WORD folder with no subfolders

                Write-Host "Scanning: $($folder.Name)"

                $size = (Get-ChildItem `
                    -LiteralPath $folder.FullName `
                    -File `
                    -Recurse `
                    -Force `
                    -ErrorAction SilentlyContinue |
                    Measure-Object -Property Length -Sum).Sum

                if ($null -eq $size) {
                    $size = 0
                }


                [PSCustomObject]@{
                    'First Folder' = $folder.Name
                    'Subfolder'    = ''
                    'Size (GB)'    = [math]::Round($size / 1GB, 2)
                }
            }

        }
        else {

            # ------------------------------------------------
            # Normal first-level folder.
            # Calculate its complete size.
            # ------------------------------------------------

            Write-Host "Scanning: $($folder.Name)"

            $size = (Get-ChildItem `
                -LiteralPath $folder.FullName `
                -File `
                -Recurse `
                -Force `
                -ErrorAction SilentlyContinue |
                Measure-Object -Property Length -Sum).Sum

            if ($null -eq $size) {
                $size = 0
            }


            [PSCustomObject]@{
                'First Folder' = $folder.Name
                'Subfolder'    = ''
                'Size (GB)'    = [math]::Round($size / 1GB, 2)
            }
        }
    }


    # --------------------------------------------------------
    # Display the final table
    # --------------------------------------------------------

    Write-Host ""
    Write-Host "============================================================"
    Write-Host "SCAN COMPLETE"
    Write-Host "============================================================"
    Write-Host ""

    $Results | Format-Table -AutoSize


    # --------------------------------------------------------
    # Export CSV
    #
    # Export-Csv overwrites the file if it already exists.
    # --------------------------------------------------------

    $Results | Export-Csv `
        -LiteralPath $CsvPath `
        -NoTypeInformation `
        -Encoding UTF8


    # --------------------------------------------------------
    # Confirmation
    # --------------------------------------------------------

    Write-Host ""
    Write-Host "============================================================"
    Write-Host "CSV SUCCESSFULLY CREATED"
    Write-Host "============================================================"
    Write-Host ""

    Write-Host "File:"
    Write-Host $CsvPath
    Write-Host ""

    Write-Host "Rows written: $($Results.Count)"
    Write-Host ""

    Write-Host "The existing CSV was overwritten."
    Write-Host ""

}
finally {

    # --------------------------------------------------------
    # Reset the CurrentUser execution policy
    # --------------------------------------------------------

    try {
        Set-ExecutionPolicy `
            -Scope CurrentUser `
            -ExecutionPolicy Undefined `
            -Force `
            -ErrorAction SilentlyContinue
    }
    catch {
        # Nothing further to do
    }
}

Source: ChatGPT


_______________________________________


 
   
Pagini: 1  
Mergi la