How do I find the biggest file in a folder and subfolders on the command line?

I tried this command:

dir /s /a:-d /o:-s /b

However this command gives the biggest file hierarchy wise. For example, first it gives the biggest file in the main folder and listed out other files, then the biggest file in the subfolder. I need the biggest file on the top whether it is in the folder or subfolder.


Solution 1:

PowerShell can do this very easily:

Get-ChildItem -Path "C:\SomeParentDirectory" -Recurse | Sort-Object -Descending Length

Solution 2:

I have got the solution. Here it is:

SETLOCAL EnableDelayedExpansion
set tes=0
set name=
set path=

for /r %%h in (*.*) do (
IF !tes! LSS %%~zh (
SET tes= %%~zh
SET name= %%~nh
SET path= %%~ph
)
)

echo name = !name! >> Biggest.txt
echo size = !tes! >> Biggest.txt
echo path = !path! >> Biggest.txt