Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

How to List the Size of Windows Folders via CMD

In the Windows environment, managing disk space is crucial for maintaining system performance and efficiency. One common task is determining the size of folders to identify which directories are consuming the most space. While Windows Explorer provides a graphical interface for this, using the Command Prompt (CMD) can be more efficient, especially for automation and scripting purposes.

Listing the size of folders directly via CMD is not straightforward because the built-in DIR command does not provide folder sizes. However, we can achieve this by using a combination of commands and scripts. This article will guide you through the process of listing folder sizes using CMD, making it easier to manage your disk space effectively.

Examples:

  1. Using PowerShell within CMD:

    PowerShell, which is integrated into Windows, can be invoked from CMD to list folder sizes. Here’s how you can do it:

    Open CMD and run the following command:

    powershell -command "Get-ChildItem -Directory | ForEach-Object { $_.FullName + ' ' + (Get-ChildItem -Recurse -Force -ErrorAction SilentlyContinue -Path $_.FullName | Measure-Object -Property Length -Sum).Sum / 1MB + ' MB' }"

    This command uses PowerShell to:

    • List directories in the current path.
    • Calculate the total size of each directory recursively.
    • Output the directory name and its size in megabytes (MB).
  2. Using a Batch Script:

    If you prefer to use a batch script, you can create a more complex solution. Here’s an example of a batch script that calculates folder sizes:

    @echo off
    setlocal EnableDelayedExpansion
    
    rem Change to the directory you want to analyze
    cd /d "C:\Path\To\Your\Directory"
    
    rem Loop through each directory
    for /d %%D in (*) do (
       set "size=0"
       rem Get the size of each file in the directory
       for /r "%%D" %%F in (*) do (
           set /a size+=%%~zF
       )
       rem Convert size to MB
       set /a sizeMB=size/1048576
       echo %%D - !sizeMB! MB
    )
    
    endlocal

    To use this script:

    • Save it as FolderSize.bat.
    • Modify the path in the cd command to the directory you want to analyze.
    • Run the script from CMD.
  3. Using du Utility from Sysinternals:

    Microsoft’s Sysinternals suite includes a utility called du (Disk Usage) that can be used to list folder sizes. Here’s how to use it:

    • Download the Sysinternals suite from the official Microsoft website.
    • Extract the contents and navigate to the directory containing du.exe.
    • Open CMD and run the following command:
      du -q -l 1 "C:\Path\To\Your\Directory"

    This command will list the sizes of all subdirectories in the specified path at a depth of 1 level.

To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.