How to list directory file and size; including subdirs

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

How to list directory file and size; including subdirs

#1 Post by MKANET » 04 Jun 2012 12:27

I just found out the hard way that the command forefiles has a file size bug in it (where it returns the wrong file size for very large files). So... I would like to do what I should have done in the first place... use an alternate method to have more flexibility. However, I need a help recreate the same format as below.

Ultimately, all I need a list of files full filepath\name and respective byte size. (sorted: largest first)

I noticed that dir a: /-c /o-g-s /s /b sort of does this without the size of the file.

I'm guessing I need at least one for /f command to get the job done. I've been trying and trying, but just can't think of how to do it using the for /f command. I'm sure this is probably a piece of cake for you gurus out there..

Format I would like it to return

Code: Select all

a:\4-Thur\00020000.ubd 7316504576
a:\4-Thur\00030000.ubd 7471104
a:\5-Fri\00010000.ubd 32768
a:\5-Fri\00020000.ubd 5503909888
a:\Diskeeper\IfaastMon.dat 4063232
a:\Diskeeper\IFaastRegions.dat 1208
a:\OSImage\00010000.ubd 32768
a:\OSImage\00020000.ubd 7908687872



Thanks as always!!

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: How to list directory file and size; including subdirs

#2 Post by Squashman » 04 Jun 2012 13:06

%~zI - expands %I to size of file

You will probably have to create a text file of all your output first and then use the same code as you did last time to sort them in size order, because I believe the sort function of DIR will sort by the size of each file in each folder instead of the whole output.

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

Re: How to list directory file and size; including subdirs

#3 Post by aGerman » 04 Jun 2012 13:26

What about

Code: Select all

>list.txt (
  for /f "delims=" %%i in ('dir "a:\" /a-d /b /o-s /s') do echo %%i %%~zi
)

Regards
aGerman

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: How to list directory file and size; including subdirs

#4 Post by MKANET » 04 Jun 2012 14:07

Thanks aGerman!

I'm guessing that there's no way to sort the entire list by size in the first try. Currently, it sorts each folder's contents by size. Apparently, this is a limitation with the DIR command. If there's no way to do this in the first try, then, I can use a handy piece of code that "Antonio" made which sorts this entire list:

Code: Select all

set /A max=0x7FFFFFFF
for /F "delims=" %%a in (list.txt) do (
   set name=
   for %%b in (%%a) do (
      if not defined name (
         set name=%%b
      ) else (
         set size=%%b
         set /A revSize=max-!size:~0,9!
         set line[!revSize!]=!name! %%b
      )
   )
)
(for /F "tokens=1* delims==" %%a in ('set line[') do echo %%b) > sortedlist.txt


MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: How to list directory file and size; including subdirs

#5 Post by MKANET » 04 Jun 2012 15:03

I just found out that there's something wrong with Antonio's sorting code. It seems to have the same problem as the output from

Code: Select all

>list.txt (
  for /f "delims=" %%i in ('dir "a:\" /a-d /b /o-s /s') do echo %%i %%~zi
)


They are being sorted by size; however, only per each respective folder.

Maybe, I'm explaining it not clearly enough... I was hoping for the list to be sorted by size regardless of what folder they are in on the a:\ drive.

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

Re: How to list directory file and size; including subdirs

#6 Post by aGerman » 04 Jun 2012 15:13

Well, Aacinis code is not bulletproof (because of the delayed expansion and the numeric limit) but you could simplify it as follows (untested):

Code: Select all

setlocal EnableDelayedExpansion
for /F "delims=" %%a in ('dir "a:\" /a-d /b /s') do (
  set /A revSize=0x7FFFFFFF-%%~za
  set line[!revSize!]=%%a %%~za
)
(for /F "tokens=1* delims==" %%a in ('set line[') do echo %%b)>list.txt

Regards
aGerman

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: How to list directory file and size; including subdirs

#7 Post by Squashman » 04 Jun 2012 15:31

Wouldn't !K's solution would work in this instance since we longer have to deal with the delimiter issue he had before in his previous thread.

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: How to list directory file and size; including subdirs

#8 Post by MKANET » 04 Jun 2012 15:52

agerman, the last code you posted was returning errors for some of the items it was processing:

Invalid number. Numbers are limited to 32-bits of precision.
Invalid number. Numbers are limited to 32-bits of precision.
Invalid number. Numbers are limited to 32-bits of precision.
Invalid number. Numbers are limited to 32-bits of precision.
Invalid number. Numbers are limited to 32-bits of precision.
Invalid number. Numbers are limited to 32-bits of precision.
Invalid number. Numbers are limited to 32-bits of precision.

Squashman, you're right, I tried !K's solution to sort the files. It worked very well (below). Maybe there's a way to simplify this code:

Code: Select all

>%MYFILES%\outfile.txt (
  for /f "delims=" %%i in ('dir "a:\" /a-d /b /o-s /s') do echo %%i %%~zi
)
for /f "tokens=1,2" %%a in (%MYFILES%\outfile.txt) do (
   set "size=............%%b"
   call set %%size:~-12%%=%%a
)
for /f "tokens=1* delims==." %%a in ('set . ^|sort /r') do >>%MYFILES%\outfile2.txt echo %%b %%a


Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: How to list directory file and size; including subdirs

#9 Post by Squashman » 04 Jun 2012 16:00

Yeah. Don't redirect the output from the first for loop to a file. Just go straight to the set statements.

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: How to list directory file and size; including subdirs

#10 Post by MKANET » 04 Jun 2012 16:15

Sorry go cause so much problems...

It looks like there's a problem with the sorting code. It skips anything that's greater than 99999999999 (99.99GB)

For example, the below number (167GB's) is skipped during sorting. I'm guessing because of the extra digit. Is there any way to get past this limit? I'm sure there are going to be cases where the files sizes are going to exceed 99.99GB.

a:\OSImage\00020000.ubd 179707379712


Edit: Nevermind, I just changed the below code to handle two extra digits:

Code: Select all

   set "size=............%%b"
   call set %%size:~-12%%=%%a
Last edited by MKANET on 04 Jun 2012 16:19, edited 1 time in total.

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

Re: How to list directory file and size; including subdirs

#11 Post by aGerman » 04 Jun 2012 16:19

Enlarge the number of dots

Code: Select all

for /F "delims=" %%a in ('dir "a:\" /a-d /b /s') do (
  set "size=..............................%%~za"
  call set "%%size:~-30%%=%%a %%~za"
)
(for /F "tokens=1* delims==" %%a in ('set .^|sort /r') do echo %%b)>list.txt

Regards
aGerman

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: How to list directory file and size; including subdirs

#12 Post by MKANET » 04 Jun 2012 16:23

Works beautifully. Thanks guys!

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: How to list directory file and size; including subdirs

#13 Post by dbenham » 04 Jun 2012 16:28

The code needs to preserve 13 size characters to make sure every variable is prefixed with at least one period. But I find output to a text file followed by SORT is faster than letting the environment do my sort (that was a mild surprise).

Here is how I would do it using delayed expansion (should be faster, despite the delayed expansion toggling)

Also, no need to sort the initial listing since it is not what you want anyway.

I bumped the max file size to support ~999 terabytes. Probably never needed, but why not? :)

Code: Select all

@echo off
setlocal disableDelayedExpansion
(
  for /r "a:\" %%F in (*) do (
    set "file=%%F"
    set "size=...............%%~zF"
    setlocal enableDelayedExpansion
    echo !size:~-15! !file!
    endlocal
  )
)>unsorted.txt
(
  for /f "tokens=1* delims=. " %%A in ('sort /r unsorted.txt') do echo %%B %%A
)>sorted.txt
del unsorted.txt

I used FOR /R which will exclude hidden and system files. If you need to include hidden and system files then change the 1st FOR loop to

Code: Select all

  for /f "eol=: delims=" %%F in ('dir /b /a-d /s "a:\"') do (


Dave Benham
Last edited by dbenham on 04 Jun 2012 20:56, edited 1 time in total.

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

Re: How to list directory file and size; including subdirs

#14 Post by aGerman » 04 Jun 2012 16:45

BTW If you would use characters greater than 9 (e.g. any letter) instead of the dots you dont need reverse sorting.

Regards
aGerman

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: How to list directory file and size; including subdirs

#15 Post by dbenham » 04 Jun 2012 16:55

Huh :shock: :?
I don't see how that works. Your proposal would produce something like

100
101
102
10
11
12
0
1
2

The files are supposed to be sorted descending by file size. The leading character must sort less than 0 and the /R option must be used.

Also, I just realized the environment sorting method has a nasty bug: it will only work if all of the file sizes are unique :!:
That is not a safe assumption.

It could be fixed, but it would require either

a) include the file name in the variable name, but then it can't handle files with = in name.

b) append delimitted incrementing sequenceNumber to each variable name. Extra work to maintain the sequence number.

It doesn't seem worthwhile to me. I think the SORT method is simpler and faster.


Dave Benham

Post Reply