How to List Files Sorted by Size in Batch Script
A common administrative task is to identify the largest or smallest files in a directory to manage disk space or find specific log files. While you could manually look through File Explorer, a batch script provides a fast, repeatable, and powerful way to generate these lists. The primary tool for this job is the built-in DIR command, which has robust sorting capabilities.
This guide will teach you how to use the DIR command's sorting options to list files from smallest to largest and largest to smallest. You will also learn the advanced technique of capturing this sorted list into a FOR loop to perform actions on the files, such as creating a "Top 5 Largest Files" report.
The Core Command: DIR /O
The DIR command's /O switch is used to Order (or sort) the output. It takes a letter to specify the sorting criteria. For our purposes, the most important one is S.
/O: The main sort switch.S: Sorts by Size.
So, the key command is DIR /OS.
Sorting Smallest to Largest (/OS)
By default, the DIR /OS command sorts files in ascending order, meaning it will list the smallest files (including 0-byte files) first and the largest files last.
C:\MyFiles> DIR /OS
The output is a standard directory listing, but the files are ordered by their size in bytes, from smallest to largest.
Volume in drive C is Windows
Volume Serial Number is 1234-ABCD
Directory of C:\MyFiles
10/27/2023 04:15 PM 0 empty.txt
10/27/2023 04:15 PM 25 config.ini
10/27/2023 04:16 PM 35,840 document.docx
10/27/2023 04:16 PM 1,572,864 archive.zip
4 File(s) 1,608,729 bytes
2 Dir(s) 123,456,789,012 bytes free
Sorting Largest to Smallest (/O-S)
This is often the more useful command, as it helps you immediately identify the files consuming the most disk space. To reverse any sort order with the /O switch, you simply add a minus sign (-) before the sort letter.
C:\MyFiles> DIR /O-S
Now in the output the file list is inverted, with the largest file appearing at the top.
Volume in drive C is Windows
...
Directory of C:\MyFiles
10/27/2023 04:16 PM 1,572,864 archive.zip
10/27/2023 04:16 PM 35,840 document.docx
10/27/2023 04:15 PM 25 config.ini
10/27/2023 04:15 PM 0 empty.txt
4 File(s) 1,608,729 bytes
...
The Advanced Method: Capturing Sorted Output for Scripting
Simply displaying the list is useful, but a script often needs to do something with that list. To achieve this, we can process the output of the DIR command using a FOR /F loop. This allows us to work with the sorted list of filenames one by one.
This script iterates through the files, from smallest to largest, and echoes each filename.
@ECHO OFF
ECHO --- Files in this directory, sorted by size (smallest first) ---
REM The 'skip=5' is used to bypass the header lines of the DIR command.
FOR /F "skip=5 tokens=*" %%F IN ('DIR /OS /A-D') DO (
ECHO File: "%%F"
)
/A-D is used to exclude directories from the listing.
Common Pitfalls and How to Solve Them
The Unreliable Method to Avoid: DIR | SORT
You might be tempted to pipe the output of DIR into the SORT command. This does not work reliably for sorting by size. The SORT command works on character columns, and the file size in a standard DIR listing does not start in the same column for every file, leading to incorrect alphabetical sorting instead of numerical sorting.
Let's see the error:
C:\> DIR | SORT
...
10/27/2023 04:16 PM 1,572,864 archive.zip <-- Starts with 1
10/27/2023 04:16 PM 35,840 document.docx <-- Starts with 3
...
Solution: Always use the built-in /O switch in the DIR command itself. It is the only reliable method.
Including Subdirectories (Recursive Sort)
By default, DIR only lists files in the current directory. To get a single, sorted list of all files in a directory tree, you need to add the /S (recursive) switch.
This command finds the largest files across the entire C:\Logs directory tree.
DIR C:\Logs /S /O-S /A-D
Practical Example: Reporting the Top 5 Largest Files
This script combines the techniques we've learned to create a useful report of the top 5 largest files in a given folder. This demonstrates the power of processing the sorted DIR output.
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "START_FOLDER=%CD%"
SET "FILE_LIMIT=5"
SET "count=0"
ECHO --- Top %FILE_LIMIT% Largest Files in "%START_FOLDER%" ---
ECHO.
REM /O-S: Sort largest first
REM /A-D: Exclude directories
REM /B: Bare format (just the filename), which is easier to parse
FOR /F "delims=" %%F IN ('DIR "%START_FOLDER%" /O-S /A-D /B') DO (
SET /A "count+=1"
IF !count! LEQ %FILE_LIMIT% (
FOR %%S IN ("%%F") DO ECHO Size: %%~zS bytes - File: "%%F"
)
)
ENDLOCAL
ENABLEDELAYEDEXPANSIONis needed for the counter.- The inner
FORloop is a simple trick to get the size (%%~zS) of the filename provided by the outer loop.
Output
--- Top 5 Largest Files in "C:\MyFiles" ---
Size: 1572864 bytes - File: "archive.zip"
Size: 35840 bytes - File: "document.docx"
Size: 25 bytes - File: "config.ini"
Size: 0 bytes - File: "empty.txt"
Conclusion
The DIR command is a powerful and flexible tool for listing files sorted by size directly from the command line.
- Use
DIR /OSto sort files from smallest to largest. - Use
DIR /O-Sto sort files from largest to smallest. - Add the
/Sswitch to perform the sort recursively on an entire directory tree. - To use the sorted list within a script, process the output of
DIRwith aFOR /Floop.
By mastering these simple switches, you can easily create scripts to monitor and manage disk space usage effectively.