batch file to list files over a certain file size recursively

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
jabbyjim
Posts: 5
Joined: 08 Dec 2015 10:34

batch file to list files over a certain file size recursively

#1 Post by jabbyjim » 08 Dec 2015 13:11

Greetings .. I considered myself pretty good at this until I have seen the work in this forum. WOW!

This is what I have so far ... which lists file size then the file w/directory but I'm unsure how to restrict output by the file size.

Code: Select all

@echo off
set topnode=C:\
set maxsize = 10000
pushd %topnode%
for /R "%topnode%" %%a in (*.*) do echo %%~zfa
popd


which gives me results that look like this .. which is a good start
----------------------------------------------
18160 C:\Program Files\7-Zip\Lang\ba.txt
18850 C:\Program Files\7-Zip\Lang\be.txt
20580 C:\Program Files\7-Zip\Lang\bg.txt
23005 C:\Program Files\7-Zip\Lang\bn.txt
10645 C:\Program Files\7-Zip\Lang\br.txt
13798 C:\Program Files\7-Zip\Lang\ca.txt

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

Re: batch file to list files over a certain file size recursively

#2 Post by Squashman » 09 Dec 2015 09:30

Code: Select all

@echo off
set topnode=C:\
set maxsize = 10000
pushd %topnode%
for /R "%topnode%" %%G in (*.*) do IF %%~zG LEQ %maxsize% echo %%G
popd

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: batch file to list files over a certain file size recursively

#3 Post by Compo » 09 Dec 2015 10:48

Alternative:

Code: Select all

@ForFiles /P C:\ /S /C "Cmd /C If @IsDir==FALSE If @FSize LEq 10000 Echo @FSize0x09@Path"

jabbyjim
Posts: 5
Joined: 08 Dec 2015 10:34

Re: batch file to list files over a certain file size recursively

#4 Post by jabbyjim » 09 Dec 2015 12:01

Squashman wrote:

Code: Select all

@echo off
set topnode=C:\
set maxsize = 10000
pushd %topnode%
for /R "%topnode%" %%G in (*.*) do IF %%~zG LEQ %maxsize% echo %%G
popd


Wow thank you for this. Oddly enough it did something completely goofy on my computer though .. Not sure why.. but this looks good. Pulling out the variable ~z is interesting because I also wanted to convert to MB but I'm unsure how to do that yet. This is certainly more complicated as I continue!

jabbyjim
Posts: 5
Joined: 08 Dec 2015 10:34

Re: batch file to list files over a certain file size recursively

#5 Post by jabbyjim » 09 Dec 2015 12:07

Compo wrote:Alternative:

Code: Select all

@ForFiles /P C:\ /S /C "Cmd /C If @IsDir==FALSE If @FSize LEq 10000 Echo @FSize0x09@Path"


how the ... holy crap How did you do this? This worked but ... I'm at a complete loss in how this works!?! I'm BLOWN away

Honestly you just blew my mind .. I never even knew about ForFiles until this moment .. which changes EVERYTHING.

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

Re: batch file to list files over a certain file size recursively

#6 Post by Squashman » 09 Dec 2015 13:06

jabbyjim wrote:
Compo wrote:Alternative:

Code: Select all

@ForFiles /P C:\ /S /C "Cmd /C If @IsDir==FALSE If @FSize LEq 10000 Echo @FSize0x09@Path"


how the ... holy crap How did you do this? This worked but ... I'm at a complete loss in how this works!?! I'm BLOWN away

Honestly you just blew my mind .. I never even knew about ForFiles until this moment .. which changes EVERYTHING.

Yes. FORFILES is a unique utility but it is also extremely slow because a new CMD process will be created and destroyed for every file that FORFILES processes.

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

Re: batch file to list files over a certain file size recursively

#7 Post by Squashman » 09 Dec 2015 13:12

jabbyjim wrote:
Squashman wrote:

Code: Select all

@echo off
set topnode=C:\
set maxsize = 10000
pushd %topnode%
for /R "%topnode%" %%G in (*.*) do IF %%~zG LEQ %maxsize% echo %%G
popd


Wow thank you for this. Oddly enough it did something completely goofy on my computer though .. Not sure why.. but this looks good. Pulling out the variable ~z is interesting because I also wanted to convert to MB but I'm unsure how to do that yet. This is certainly more complicated as I continue!

I don't deal with goofy. I like to keep things black and white with concrete examples. You will have to explain what you mean.

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

Re: batch file to list files over a certain file size recursively

#8 Post by Squashman » 09 Dec 2015 13:25

One other option using RoboCopy.

Code: Select all

robocopy "C:\." "C:\." /MAX:10000 /L /nocopy /is /NJH /NJS /TS /NC /NDL /S

You will have to run this as administrator otherwise you will see errors.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: batch file to list files over a certain file size recursively

#9 Post by foxidrive » 09 Dec 2015 13:27

jabbyjim wrote:I never even knew about ForFiles until this moment .. which changes EVERYTHING.

It helps to know EVERYTHING about your task when you ask for support - that way you get the best suggestions and most appropriate solution.

The code you provided doesn't give the output that you showed - so what you have asked is unlikely to be the actual task.

See here: viewtopic.php?f=3&t=6108

jabbyjim
Posts: 5
Joined: 08 Dec 2015 10:34

Re: batch file to list files over a certain file size recursively

#10 Post by jabbyjim » 09 Dec 2015 14:11

foxidrive wrote:
jabbyjim wrote:I never even knew about ForFiles until this moment .. which changes EVERYTHING.

It helps to know EVERYTHING about your task when you ask for support - that way you get the best suggestions and most appropriate solution.

The code you provided doesn't give the output that you showed - so what you have asked is unlikely to be the actual task.

See here: viewtopic.php?f=3&t=6108


Look, I apologize if I did something wrong, but I indeed gave the output of what my original script was doing (granted it was a snippet of my entire hard drive). I was trying to get help with a script without completely requesting the entire thing to be done for me.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: batch file to list files over a certain file size recursively

#11 Post by foxidrive » 09 Dec 2015 14:30

jabbyjim wrote:Look, I apologize if I did something wrong, but I indeed gave the output of what my original script was doing (granted it was a snippet of my entire hard drive).


It was my error - and my frustration at many years of people asking questions to get accurate answers, but not providing accurate details of the task.

I didn't notice the 'f' in the %%~zfa and so I'm sorry to make a blanket statement that isn't true.

Squashman's code should also be very effective, if the files are under 2 GB, and the only issue is that this line

set maxsize = 10000

has spaces around the equals sign, and that is one of the few places in batch files where spaces matter.
Test this snippet - it has additional double quotes in line 2 and 4 that eliminates a crash, when a path is given that contains an & etc.

Code: Select all

@echo off
set "topnode=C:\"
set maxsize=10000
pushd "%topnode%"
for /R "%topnode%" %%G in (*.*) do IF %%~zG LEQ %maxsize% echo %%G
popd

jabbyjim
Posts: 5
Joined: 08 Dec 2015 10:34

Re: batch file to list files over a certain file size recursively

#12 Post by jabbyjim » 09 Dec 2015 15:54

its no problem.. I am sure I confused everyone in my process. Its the way my brain works when I get excited.

anyway, that snippet worked well, thank you.. I just need to make an adjustment so it shows the size also.

currently it just does a list of the files fitting the size parameter
-------------------------------
C:\Program Files\7-Zip\descript.ionz
C:\Program Files\7-Zip\License.txtz
C:\Program Files\7-Zip\readme.txtz
C:\Program Files\AutoHotkey\AutoHotkey Website.urlz
C:\Program Files\AutoHotkey\AutoHotkey.ahkz

I'm experimenting with it now .. but having no luck.. I figured

Code: Select all

for /R "%topnode%" %%G in (*.*) do IF %%~zG LEQ %maxsize% echo %%zG

.. would work but it doesn't.

the Forfiles one liner from Compo did this well actually ... I feel foolish for never knowing about that possibility (forefiles statement), honestly.

but for the record I am all set now, I'm happy to keep going with the alternative script if there is interest though.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: batch file to list files over a certain file size recursively

#13 Post by foxidrive » 09 Dec 2015 15:56

It's the same issue that bamboozled me - just change echo %%G to echo %%~zfG in the code and it'll show the size too.

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

Re: batch file to list files over a certain file size recursively

#14 Post by Squashman » 10 Dec 2015 07:35

Jim,
Maybe this portion of the FOR help file will help you understand how the FOR variable modifiers work.

Code: Select all

In addition, substitution of FOR variable references has been enhanced
You can now use the following optional syntax:

    %~I         - expands %I removing any surrounding quotes (")
    %~fI        - expands %I to a fully qualified path name
    %~dI        - expands %I to a drive letter only
    %~pI        - expands %I to a path only
    %~nI        - expands %I to a file name only
    %~xI        - expands %I to a file extension only
    %~sI        - expanded path contains short names only
    %~aI        - expands %I to file attributes of file
    %~tI        - expands %I to date/time of file
    %~zI        - expands %I to size of file
    %~$PATH:I   - searches the directories listed in the PATH
                   environment variable and expands %I to the
                   fully qualified name of the first one found.
                   If the environment variable name is not
                   defined or the file is not found by the
                   search, then this modifier expands to the
                   empty string

The modifiers can be combined to get compound results:

    %~dpI       - expands %I to a drive letter and path only
    %~nxI       - expands %I to a file name and extension only
    %~fsI       - expands %I to a full path name with short names only
    %~dp$PATH:I - searches the directories listed in the PATH
                   environment variable for %I and expands to the
                   drive letter and path of the first one found.
    %~ftzaI     - expands %I to a DIR like output line

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

Re: batch file to list files over a certain file size recursively

#15 Post by Squashman » 10 Dec 2015 07:54

jabbyjim wrote:I'm experimenting with it now .. but having no luck.. I figured

Code: Select all

for /R "%topnode%" %%G in (*.*) do IF %%~zG LEQ %maxsize% echo %%zG

You are missing the TILDE.

Post Reply