---
# System prepended metadata

title: PID-1128

---

To get the file sizes of all files and directories in a directory and its subdirectories using batch, use the following batch script:
```batch
@echo off
setlocal enabledelayedexpansion

set "dir=C:\path\to\directory"
set "totalSize=0"

for /r "%dir%" %%f in (*) do (
    if %%~af EQU d (
        rem Process directories
        set "dirSize=0"
        for /f %%s in ('dir /a /s /b "%%f" ^| find /c ":"') do (
            set /a "dirSize=%%s"
        )
        set /a "totalSize+=dirSize"
        echo [DIR] %%f - !dirSize! files/dirs
    ) else (
        rem Process files
        set "fileSize=%%~zf"
        set /a "totalSize+=fileSize"
        echo [FILE] %%f - !fileSize! bytes
    )
)
echo Total size of %dir%: %totalSize% bytes
```
Explanation of Code:
* The code iterates through all items (files and directories) in the specified directory and its subdirectories (`for /r "%dir%" %%f in (*) do`).
* For each item, it checks whether it's a directory or a file using `if %%~af EQU d`.
* If it's a directory, it calculates the size of the directory by counting the number of files and subdirectories within it using the `dir /a /s /b "%%f" ^| find /c ":"` command.
* It then adds the directory's size to the `totalSize` variable and displays information about the directory, including its path (`%%f`) and the number of files and subdirectories it contains (`dirSize`).
* If the item is a file, it calculates the file's size using `%%~zf`, adds it to the `totalSize`, and displays information about the file, including its path and size in bytes.

However, this will be slower and less accurate than using PowerShell.