How to list directory file and size; including subdirs

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

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

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

Hmm, that works just fine for me:
Edit: No, it doesn't. I tested only with a few files where it worked. The dots and reverse sorting seems to be necessary.

Code: Select all

.... removed ...

The = should never cause any problem since the first character in the path is the drive letter.

Regards
aGerman

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

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

#17 Post by MKANET » 04 Jun 2012 17:33

So, what would happen if there are two files in the list with the same size? The only way I see this happening is the same file in two different directories; which could easily happen.

However, I doubt there will be any files that would ever have an = symbol.

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

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

#18 Post by aGerman » 04 Jun 2012 17:50

Well, it could also happen that there are two files with same size but different name in the same directory.
The files would be sorted depending on the first different character in the full name (including path).

As I mentioned before an equal sign in the name will not affect the functionality of the code, characters like ampersands would though.

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

#19 Post by dbenham » 04 Jun 2012 18:37

I frequently have files of the exact same size in the same folder. The problem with the environment sort as written isn't the sort. The problem is that at most one file name can be assigned to a given size.

set .FileSize=FileName FileSize

Fixed by

A) set .FileSize:FileName=FileName FileSize, except name can't contain =. Parsing rules would have to change. Really don't need the FileName and size in the value, could simply use a constant value and parse just the variable name.

or

B) set .FileSize.sequenceNumber=FileName FileSize, sequenceNumber must be incremented for each file.


Dave Benham

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

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

#20 Post by MKANET » 04 Jun 2012 19:18

Dave, what you said went completely over my head. Im trying to understand how what you put would integrate into:

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) >%MYFILES%\outfile2.txt


Where would set .FileSize:FileName=FileName FileSize fit in? Also, agerman mentions that it's okay to use the equal sign in the filename. But you say the name can't contain an =

BTW: I preferred the code above because it was the most simple; with no temp files involved; despite it running slower.

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

#21 Post by dbenham » 04 Jun 2012 20:49

Code: Select all

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

Suppose you have 2 files, both size 20, named "A:\TEST=A.TXT", and "A:\TEST=B.TXT" -
There can only be one variable named ".............................20:A:\TEST"

Even if you only have one file size 20 with name "A:\TEST=A.TXT" -
The variable name will be ".............................20:A:\TEST" and the value will be "A.TXT=TEST=A.TXT 20".
The output will be the same as the value. The value you want is "TEST=A.TXT 20"

I used to avoid the use of temp files. But I've since learned that they are frequently the best option. Often times the temp file solution is easier to code and significantly faster.


Dave Benham
Last edited by dbenham on 05 Jun 2012 06:41, edited 1 time in total.

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

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

#22 Post by Squashman » 05 Jun 2012 05:58

I like Dave's final solution. Got one addition to it and it might speed it up on a very large unsorted text file.

Just used the /REC switch

Code: Select all

for /f "tokens=1* delims=. " %%A in ('sort /r /rec 15 unsorted.txt') do echo %%A %%B

Post Reply