Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade
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:
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:
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:
FolderSize.bat
.cd
command to the directory you want to analyze.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:
du.exe
.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.