Display File Sizes

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
tkinsella
Posts: 4
Joined: 17 Aug 2010 11:24

Display File Sizes

#1 Post by tkinsella » 17 Aug 2010 17:16

Hello!

I am trying to create batch file that will list the size of every file in a directory and it's sub-directories. This will then export the data into a txt file. Any thoughts?

Thanks!

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Display File Sizes

#2 Post by aGerman » 18 Aug 2010 08:09

Try this

Code: Select all

>your.txt type nul
for /r "D:\your\folder" %%a in (*) do >>your.txt echo %%~za %%a


Regards
aGerman

tkinsella
Posts: 4
Joined: 17 Aug 2010 11:24

Re: Display File Sizes

#3 Post by tkinsella » 18 Aug 2010 09:15

Thanks! How would I be able to sort this list by size, largest to smallest?

Thank you!

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Display File Sizes

#4 Post by aGerman » 18 Aug 2010 10:39

It's not a simple thing.
First I thought about processing the DIR command, but the switch /o-s will sort the output for each subfolder separately.
Then I thought I could use a "bubble sort", but this does not work if the size is larger than 2^31 -1 bytes.
Finaly should be doable to use the SORT command. Because it sorts alphanumerically, we have to prepend some zeros and cut it to a string with fix length.

Code: Select all

@echo off &setlocal
(
  @echo off
  for /r "D:\your\folder" %%a in (*) do (
    set "size=000000000000000000000000%%~za"
    call echo %%size:~-25%% %%a
  )
)|sort /r /o "your.txt"


Regards
aGerman

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Display File Sizes

#5 Post by ghostmachine4 » 18 Aug 2010 22:43

download coreutils for windows, after that , use du together with sort to do the job.

Code: Select all

du -sk * |sort -rn

that's it.

Post Reply