windows batch file/cmd to show top 20 files?

I dont want to download anything, but get windows to work out what the top 20 files are on a harddrive or folder structure... from command line

have no clue how to do this on windows ((as I say without downloading anything))

any ideas??
LVL 1
hdazAsked:
Who is Participating?
 
Steve KnightConnect With a Mentor IT ConsultancyCommented:
Hmm, why is it including them twice now, pah!...
Oh I see... the putting in loop at the end is putting in for every iteration of the loop... this looks better:


on error resume next

' Script to find all files in a folder beyond a certain size
' Stores array of top 20

Dim fso
Dim oFile
Dim oFolder
Set fso = CreateObject("Scripting.FileSystemObject")

Dim OnlyBiggerThan, SmallestSize, Size, n
Dim FileList(20,2)
Dim Count

Const NumFiles=20 ' Number of files to record.  Make same as array above
Const ShowDebug=0   ' Make 1 to show files as added to array
OnlyBiggerThan=102400 ' Kb = 100Mb.  Only report files bigger than this


' Prepare array
For n=1 to NumFiles
  FileList(n,1)=""
  FileList(n,2)=0
NEXT

wscript.echo "Started at " & time

' Call initial folder and it will work down subdirs
' By running recursively through subdirs after doing files
' in each dir. 

if wscript.arguments.count > 0 then
  FindFiles wscript.arguments.item(0)
else
  wscript.echo "No directory specified.  Usage: cscript //nologo findbiggest.vbs ""c:\windows"""
  wscript.quit
end if

' Display results at the end
For n=1 to NumFiles
  if FileList(n,2)<>0 then wscript.echo right("           " & formatnumber(FileList(n,2),0,false,false,true),12) & " = " & FileList(n,1)
NEXT

wscript.echo "Ended at " & time & " having checked " & count & " files"

Set oFolder = Nothing
Set oFile = Nothing
Set fso = Nothing

wscript.quit

' Function to run through all files and check them then call itself recursively through subdirs.
Function FindFiles(foldername)
Set oFolder = fso.GetFolder(foldername)

For Each oFile in oFolder.Files
  Count=Count+1
  on error resume next
  size=int(ofile.size/1024)
  if err.number=0 then

    If size > OnlyBiggerThan then
      If size > SmallestSize then
          AddToArray foldername & "\" & oFile.name,size
       End If
    End If
  Else
    Err.Clear
  End if
Next

on error resume next 
For Each subFolder In oFolder.SubFolders
    on error resume next
    FindFiles subFolder.Path
    if err.number<>0 then err.clear
Next

End Function

Sub AddToArray(filename,filesize)
' Keep array of biggest 20 files in order.
' SmallestSize = don't need to add anything unde
' Find slot for this size.  Check up the array until file size is bigger
' If file size is bigger than this entry (n) then shuffle entries below this down the array one

For N=1 to NumFiles
  if filesize > FileList(n,2) then
    for M=NumFiles-1 to N step -1
      FileList(M+1,1)=FileList(M,1)
      FileList(M+1,2)=FileList(M,2)
    Next
    FileList(n,1)=filename
    FileList(n,2)=filesize
    if ShowDebug=1 then wscript.echo "Adding file " & filename & " at position " & n
    exit for  
  end if
Next

SmallestSize=FileList(NumFiles,2)

End Sub

Open in new window

0
 
Steve KnightIT ConsultancyCommented:
Here's one I made earlier :-)

http://scripts.dragon-it.co.uk/links/batch-biggest-files-in-folders

Other way from explorer, search for all files over 100Mb say then sort into order...

Steve
0
 
babesiaConnect With a Mentor Commented:
Dragon-it looks like the best way.

If looking for a simple file size version  , I use diruse from windows resource kit for listing large files

diruse <drive letter> /M /S /D /Q:10000
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
Bill PrewConnect With a Mentor Commented:
This is a great example of where Powershell can shine, just do the following one liner from the powershell command prompt:

get-childItem C:\yourdir -recurse -ea SilentlyContinue | Sort-object Length -Descending | Select-object -first 20 | Select-object FullName, Length

Open in new window

~bp
0
 
hdazAuthor Commented:
Hi Steve,

Thanks for you post, yes thats the closest to my requirement it works okay on small folders.. I tried it for a large folder as an experiment on my c:\ did not work so well... almost killed the laptop due to resource issues.. i3 Core 64Bit 4GB. I left it running for what seemed a good hour or so...

Thanks for the reply babesia and billprew, you do realise that both your solutions at least 50/60% in billprew(s) case requires installation of diruse and powershell......

Iv not yet tried either diruse or the powershll cmd line for performance.. ((but I wil))

Regards
hdaz
0
 
Steve KnightIT ConsultancyCommented:
yes realistically if you think about it it needs to look at every file to get the file size and then sort them and then pick top ones....

The batch is slowish but will work - the second one getting the folder names being slower as it involves writing temp file of all on drive then sorting that huge file then reading top entries...

Is this ongoing requirement.... Explorer does do a reasonably good job.... Though slow too if you check.

And i know you dont want external util but treesize is free and gives you such info, size of each dir etc....

A vbscript version might be quicker, perhaps keeping top 20 as it goes along i suppose too in an array...

What sort of size / no. Of files are we talking about here, and is it local or network (that would nicely kill network for  a bit!).

Steve
0
 
Steve KnightIT ConsultancyCommented:
oh and lets say there are 40 files of the same size that come under the top 20 category D e.g. iSO cd image or something - your top 20 may well then just show first few such files alphabetically say.

You could also run the batch or whatever on smaller areas, e.g.

Programatically or otherwise run it on each top level dir and then combine the n x 20 results and pick the top 20 of those...

0
 
Bill PrewCommented:
@hdaz

==> ... billprew, you do realise that both your solutions at least 50/60% in billprew(s)
==> case requires installation of diruse and powershell......


Not completely true, later versions of Windows now ship with Powershell already installed and built-in. Since you didn't specify a specific version of Windows I wanted to share this potential solution.

~bp
0
 
hdazAuthor Commented:
Hi, Yep hence the 50/60% comment.. :), the batch file route should work on all versions of windows give or take a few things..

How would you rate the powershell on performance??? 100,000 files?
0
 
Bill PrewCommented:
It did the c:\windows folder on my laptop, which is about 70,000 files, in about 1.75 minutes.

~bp
0
 
Bill PrewCommented:
Running it a second time cut that time about in half, I assume because the directory structures were cached in memory from the first run.

~bp
0
 
Steve KnightIT ConsultancyCommented:
OK.... tested my batch file, slightly amended as it had lost a few characters when shown through my website above and here was my test run results.  This alsp is an i3 and it takes about 25% processor, presumably one core ...

 
@echo off
setlocal enabledelayedexpansion

echo Started at %date% %time%
echo.

REM check if a dir has been specified, if not use current dir
REM Specify on command line or can drop a dir onto an shortcut to this batch on desktop etc.
if not "%~1"=="" pushd "%~1%"

set tempfile="%temp%\%random%.txt"

REM Loop through all files in all subdirs of the current directory. For each gets the file size in bytes
REM and pads it out to be 20 characters for sorting later and writes that to a temp file with the full filename

(for /f "tokens=*" %%a in ('dir /a-d /b /s') do (
set "size=                          %%~za"
set "size=!size:~-20,20!"
echo !size!,%%~fa
)
) > %tempfile%

REM Take this temporary file, use SORT to sort it in reverse order by size
REM for each entry, remove the spaces from the file size, and keeping the first 20 entries
REM using a counter.

set count=0
for /f "tokens=1* delims=," %%a in ('sort /r ^< %tempfile%') do (
set size=%%a
set size=!size: =!
echo "%%b",!size!
if !count!==20 goto exit
set /a count+=1
)

:exit
echo.
set /p x=Count of files checked: <NUL
for /f "delims=: tokens=3" %%C in ('find /v "" /c %tempfile%') do echo %%C
echo Ended at %date% %time%
del %tempfile%
popd

Open in new window


In comparison, due to indexing, Explorer found the same list in seconds...

Run 1 - 50k files approx:
 
C:\3. UTILS\scripts>dirsize1.cmd "C:\1. DATA"
Started at 15/12/2011 22:51:34.08

"C:\1. DATA\Videos\Misc\Italy 2005\Italy1_004.AVI",11406152704
"C:\1. DATA\Videos\Misc\Italy 2005\italy1-big.mpg",4172383591
"C:\1. DATA\Videos\Misc\Italy 2005\tuscany2005.mpg",3127823808
"C:\1. DATA\Videos\Camera\2011\10-Oct-02 (02-10-2011)\PRIVATE\AVCHD\BDMV\STREAM\00008.m2ts",2044514304
"C:\1. DATA\Videos\Camera\2010\12-Dec-25 (25-12-2010)\PRIVATE\AVCHD\BDMV\STREAM\00005.m2ts",2044250112
"C:\1. DATA\Videos\Misc\Louise & Mike\davey3.avi",2020672000
"C:\1. DATA\Videos\Misc\davey3.avi",2020672000
"C:\1. DATA\Videos\00023.m2ts",1902317568
"C:\1. DATA\Videos\Sort Out\Toy Story 3 2010 [ HD BRrip x264 ENG - ITA aac 5.1 Sub eng - ita]\Toy Story 3.mkv",1597174971
"C:\1. DATA\Videos\Misc\steve and jo ALL.avi",1568096802
"C:\1. DATA\Videos\Camera\2009\10 Oct-29 (29-10-2009)\PRIVATE\AVCHD\BDMV\STREAM\00004.m2ts",1399400448
"C:\1. DATA\Videos\Misc\Steve & Jo\Steve & Jo Part 1 (arriving).avi",1379169692
"C:\1. DATA\Videos\Misc\Steve & Jo\Steve & Jo Part 4 (reading).avi",1344034220
"C:\1. DATA\Videos\Misc\Italy 2005\italy1.mpg",1341423620
"C:\1. DATA\Videos\Misc\Steve & Jo\Steve & Jo Part A (Speeches - Steve & Mike +Cake II).avi",1333644062
"C:\1. DATA\Videos\Camera\2011\12-Dec-06 (06-12-2011)\PRIVATE\AVCHD\BDMV\STREAM\00022.m2ts",1332191232
"C:\1. DATA\Videos\Camera\2011\10-Oct-29 (29-10-2011)\PRIVATE\AVCHD\BDMV\STREAM\00018.m2ts",1292138496
"C:\1. DATA\Videos\Camera\2011\10-Oct-28 (28-10-2011)\PRIVATE\AVCHD\BDMV\STREAM\00001.m2ts",1248559104
"C:\1. DATA\Videos\Camera\2009\08 Aug-29 (29-8-2009)\PRIVATE\AVCHD\BDMV\STREAM\00011.m2ts",1151834112
"C:\1. DATA\Videos\Misc\Steve & Jo\Steve & Jo Part 9 (Cake & Speeches - Rob).avi",1098112308
"C:\1. DATA\Videos\Camera\2010\04 Apr-01 (01-04-2010)\PRIVATE\AVCHD\BDMV\STREAM\00002.m2ts",1087451136

Count of files checked:  50042
Ended at 15/12/2011 22:54:24.43

C:\3. UTILS\scripts>

Open in new window



Run 2 - 69k files approx:
 
C:\3. UTILS\scripts>dirsize1.cmd "C:\windows"
Started at 15/12/2011 22:56:05.16

"C:\Windows\Installer\135557.msp",522330112
"C:\Windows\Installer\23650d.msp",194340864
"C:\Windows\Microsoft.NET\Framework64\v4.0.30319\SetupCache\Client\netfx_core.mzz",181483595
"C:\Windows\System32\drivers\AVG\incavi.avm",140441604
"C:\Windows\Installer\$PatchCache$\Managed\00004119110000000100000000F01FEC\14.0.6029\MSORES.DLL",72521600
"C:\Windows\Installer\$PatchCache$\Managed\00004119110000000100000000F01FEC\14.0.4763\MSORES.DLL",72521600
"C:\Windows\Installer\$PatchCache$\Managed\00004109340000000100000000F01FEC\14.0.6029\MSORES.DLL.x86",72521600
"C:\Windows\Installer\$PatchCache$\Managed\00004109340000000100000000F01FEC\14.0.4763\MSORES.DLL.x86",72521600
"C:\Windows\SoftwareDistribution\DataStore\DataStore.edb",67174400
"C:\Windows\winsxs\amd64_microsoft-windows-i..e-trigramdictionary_31bf3856ad364e35_6.1.7600.16385_none_12d6b2e3587e9b12\PINTLGT.IMD",66978668
"C:\Windows\IME\IMESC5\DICTS\PINTLGT.IMD",66978668
"C:\Windows\Installer\272629.msp",65792512
"C:\Windows\Installer\245db.msi",60136960
"C:\Windows\winsxs\amd64_microsoft-windows-s..chrecognizereng.ale_31bf3856ad364e35_6.1.7600.16385_en-gb_e3a447542ad2c5da\l2057.ngr",57343084
"C:\Windows\Speech\Engines\SR\en-GB\l2057.ngr",57343084
"C:\Windows\System32\MRT.exe",52174280
"C:\Windows\Installer\$PatchCache$\Managed\00004119110000000100000000F01FEC\14.0.6029\GROOVE.EXE",51727736
"C:\Windows\Installer\$PatchCache$\Managed\00004119110000000100000000F01FEC\14.0.4763\GROOVE.EXE",51456888
"C:\Windows\Installer\245cb.msi",50642944
"C:\Windows\Installer\2364d1.msp",38672896
"C:\Windows\Installer\2364e7.msp",35326464

Count of lines:  69434
Ended at 15/12/2011 23:02:25.77

Open in new window


Nice to see a 1/2Gb MSP file dropped in there by Windows etc... thanks MS!

Same set again #2 ran approx. the same times...

And for 162k files (all C:): 31 mins:

 
C:\3. UTILS\scripts>dirsize1.cmd "C:\"
Started at 15/12/2011 23:09:56.08
<results snipped out>
Count of files checked:  162099
Ended at 15/12/2011 23:40:57.26

Open in new window


So wrote this VBS:

 
on error resume next

' Script to find all files in a folder beyond a certain size
' Stores array of top 20

Dim fso
Dim oFile
Dim oFolder
Set fso = CreateObject("Scripting.FileSystemObject")

Dim OnlyBiggerThan, SmallestSize, Size, n
Dim FileList(20,2)
Dim Count

Const NumFiles=20 ' Number of files to record.  Make same as array above
Const ShowDebug=0   ' Make 1 to show files as added to array
OnlyBiggerThan=10240 ' Kb = 10Mb.  Only report files bigger than this


' Prepare array
For n=1 to NumFiles
  FileList(n,1)=""
  FileList(n,2)=0
NEXT

wscript.echo "Started at " & time

' Call initial folder and it will work down subdirs
' By running recursively through subdirs after doing files
' in each dir. 

FindFiles  "C:\1. DATA"

' Display results at the end
For n=1 to NumFiles
  if FileList(n,2)<>0 then wscript.echo n, FileList(n,1) & "," & FileList(n,2)
NEXT

wscript.echo "Ended at " & time & " having checked " & count & " files"

Set oFolder = Nothing
Set oFile = Nothing
Set fso = Nothing

wscript.quit

' Function to run through all files and check them then call itself recursively through subdirs.
Function FindFiles(foldername)
Set oFolder = fso.GetFolder(foldername)

For Each oFile in oFolder.Files
  Count=Count+1
  size=int(ofile.size/1024)

  If size > OnlyBiggerThan then
    If size > SmallestSize then
        AddToArray foldername & "\" & oFile.name,size
     End If
  End If
Next
 
For Each subFolder In oFolder.SubFolders
    FindFiles subFolder.Path
Next

End Function

Sub AddToArray(filename,filesize)
' Keep array of biggest 20 files in order.
' SmallestSize = don't need to add anything unde
' Find slot for this size.  Check up the array until file size is bigger
' If file size is bigger than this entry (n) then shuffle entries below this down the array one

For N=1 to NumFiles
  if filesize > FileList(n,2) then
    for M=NumFiles-1 to N step -1
      FileList(M+1,1)=FileList(M,1)
      FileList(M+1,2)=FileList(M,2)
      FileList(n,1)=filename
      FileList(n,2)=filesize
    Next
    if ShowDebug=1 then wscript.echo "Adding file " & filename & " at position " & n
    exit for  
  end if
Next

SmallestSize=FileList(NumFiles,2)

End Sub

Open in new window


which took some 7 seconds for the 50k files in step #1:





C:\3. UTILS\scripts>cscript //nologo findbiggest.vbs
Started at 00:00:56
<snip file details>
Ended at 00:01:03 having checked 50042 files

Open in new window

0
 
hdazAuthor Commented:
Hi Steve,

Am I mising something... from the VBS version??

C:\Users\hdaz>cscript //nologo C:\Users\hdaz\Desktop\findbiggest.vbs
Started at 12:32:42
Ended at 12:32:42 having checked  files

???

0
 
Steve KnightIT ConsultancyCommented:
Did you change the path:

FindFiles  "C:\1. DATA"

As it stands it works on a specific directory.  Can amend to pickup current dir or from command line say if it works for you OK.

Steve
0
 
hdazAuthor Commented:
C:\Users\hdaz>cscript //nologo C:\Users\hdaz\Desktop\findbiggest.vbs "C:\"

Started at 13:44:46
Ended at 13:44:46 having checked  files

So I added the end part to scan whole of c:\... nothing...


changed the line within the file to ----- FindFiles  "C:\"
C:\Users\hdaz>cscript //nologo C:\Users\hdaz\Desktop\findbiggest.vbs
Started at 13:46:16
1 C:\\hiberfil.sys,2489036
2 C:\\hiberfil.sys,2489036
3 C:\\pagefile.sys,2250292
4 C:\\pagefile.sys,2250292
5 C:\\SBSmon.zip,1628820
6 C:\\SBSmon.zip,1628820
Ended at 13:46:16 having checked 10 files

so not looking within subfolders?? ((this is on Win7))) wonder if its permission issues?
0
 
Steve KnightIT ConsultancyCommented:
Hmm, yes actually I've just tried this on Win 7 too and it gets "access denied" on various directories when running as administrator user, in "run as administrator" command prompt... Pah!

Try it on a data directory say, or C:\users

At the moment when it gets an error it resumes at the next line but I think that is chucking it out of the current loop though, i.e. if it is looking for subdirs of c:\ and one gets "access denied" then it dosn't do any more subdirs...

Hmm, will look see if I can do the error checking in a different way, and will add it to get the dir. from the command line. At the moment it is hard coded... it WAS midnight!

I get the same on these certain dirs in Win7 from explorer, dir in cmd.exe etc. so it is Windows 7 being "Helpful" to protect the masses from viruses and the like at the expense of people who want to use their machines for something useful!!

Steve
0
 
hdazAuthor Commented:
Hi Steve,

Thanks for your thoughts and efforts..... You might be ranked Genius but sleep is more important than my question..

Explorer wastes so much of my file I cant be done with it...

two other mini thoughts/suggestions would be better to place the size first then you always know to give x number of digits to the left before giving the file name so formatting would be slightly nicer...

I also wondered if it would work faster if one just dropped every file 900MB or 1G in size??

Just a couple of thoughts.. :)
0
 
hdazAuthor Commented:
Should read
Explorer wastes so much of my life I cant be done with it... it will change again in a few months and mess everyone up again..
0
 
Steve KnightIT ConsultancyCommented:
Absolutely.  I was using such a util before to give reports on individual user drives and some people only have smaller files of course but will wanted to know biggest 20... It also checks the smallest size it has kept in the array anyway in the next line so very little extra overhead.  Having said that all you have to do is amend the line showing the file size to ignore from 10240 to 900000 or 1000000 for 900Mb / 1Gb (Approx) for instance.

Will see what I can do on the access denied stuff, Bill -are you still there, any ideas, is this a UAC thing?

Steve
0
 
Steve KnightIT ConsultancyCommented:
Actually try this:
'on error resume next

' Script to find all files in a folder beyond a certain size
' Stores array of top 20

Dim fso
Dim oFile
Dim oFolder
Set fso = CreateObject("Scripting.FileSystemObject")

Dim OnlyBiggerThan, SmallestSize, Size, n
Dim FileList(20,2)
Dim Count

Const NumFiles=20 ' Number of files to record.  Make same as array above
Const ShowDebug=0   ' Make 1 to show files as added to array
OnlyBiggerThan=10240 ' Kb = 10Mb.  Only report files bigger than this


' Prepare array
For n=1 to NumFiles
  FileList(n,1)=""
  FileList(n,2)=0
NEXT

wscript.echo "Started at " & time

' Call initial folder and it will work down subdirs
' By running recursively through subdirs after doing files
' in each dir. 

if wscript.arguments.count > 0 then
  FindFiles fso.GetAbsolutePathName(".")
else
  wscript.echo "No directory specified.  Usage: cscript //nologo findbiggest.vbs ""c:\windows"""
  wscript.quit
end if

' Display results at the end
For n=1 to NumFiles
  if FileList(n,2)<>0 then wscript.echo n, FileList(n,1) & "," & FileList(n,2)
NEXT

wscript.echo "Ended at " & time & " having checked " & count & " files"

Set oFolder = Nothing
Set oFile = Nothing
Set fso = Nothing

wscript.quit

' Function to run through all files and check them then call itself recursively through subdirs.
Function FindFiles(foldername)
Set oFolder = fso.GetFolder(foldername)

For Each oFile in oFolder.Files
  Count=Count+1
  on error resume next 
  size=int(ofile.size/1024)

  If size > OnlyBiggerThan then
    If size > SmallestSize then
        AddToArray foldername & "\" & oFile.name,size
     End If
  End If
  err.clear
Next

on error resume next 
For Each subFolder In oFolder.SubFolders
    on error resume next
    FindFiles subFolder.Path
    err.clear
Next

End Function

Sub AddToArray(filename,filesize)
' Keep array of biggest 20 files in order.
' SmallestSize = don't need to add anything unde
' Find slot for this size.  Check up the array until file size is bigger
' If file size is bigger than this entry (n) then shuffle entries below this down the array one

For N=1 to NumFiles
  if filesize > FileList(n,2) then
    for M=NumFiles-1 to N step -1
      FileList(M+1,1)=FileList(M,1)
      FileList(M+1,2)=FileList(M,2)
      FileList(n,1)=filename
      FileList(n,2)=filesize
    Next
    if ShowDebug=1 then wscript.echo "Adding file " & filename & " at position " & n
    exit for  
  end if
Next

SmallestSize=FileList(NumFiles,2)

End Sub

Open in new window

0
 
Steve KnightIT ConsultancyCommented:
Or this with better formatted number and path from command line (sorry above was me half testing current dir...)


on error resume next

' Script to find all files in a folder beyond a certain size
' Stores array of top 20

Dim fso
Dim oFile
Dim oFolder
Set fso = CreateObject("Scripting.FileSystemObject")

Dim OnlyBiggerThan, SmallestSize, Size, n
Dim FileList(20,2)
Dim Count

Const NumFiles=20 ' Number of files to record.  Make same as array above
Const ShowDebug=0   ' Make 1 to show files as added to array
OnlyBiggerThan=10240 ' Kb = 10Mb.  Only report files bigger than this


' Prepare array
For n=1 to NumFiles
  FileList(n,1)=""
  FileList(n,2)=0
NEXT

wscript.echo "Started at " & time

' Call initial folder and it will work down subdirs
' By running recursively through subdirs after doing files
' in each dir. 

if wscript.arguments.count > 0 then
  FindFiles wscript.arguments.item(0)
else
  wscript.echo "No directory specified.  Usage: cscript //nologo findbiggest.vbs ""c:\windows"""
  wscript.quit
end if

' Display results at the end
For n=1 to NumFiles
  if FileList(n,2)<>0 then wscript.echo right("           " & formatnumber(FileList(n,2),0,false,false,true),12) & " = " & 

FileList(n,1)
NEXT

wscript.echo "Ended at " & time & " having checked " & count & " files"

Set oFolder = Nothing
Set oFile = Nothing
Set fso = Nothing

wscript.quit

' Function to run through all files and check them then call itself recursively through subdirs.
Function FindFiles(foldername)
Set oFolder = fso.GetFolder(foldername)

For Each oFile in oFolder.Files
  Count=Count+1
  on error resume next 
  size=int(ofile.size/1024)

  If size > OnlyBiggerThan then
    If size > SmallestSize then
        AddToArray foldername & "\" & oFile.name,size
     End If
  End If
  err.clear
Next

on error resume next 
For Each subFolder In oFolder.SubFolders
    on error resume next
    FindFiles subFolder.Path
    err.clear
Next

End Function

Sub AddToArray(filename,filesize)
' Keep array of biggest 20 files in order.
' SmallestSize = don't need to add anything unde
' Find slot for this size.  Check up the array until file size is bigger
' If file size is bigger than this entry (n) then shuffle entries below this down the array one

For N=1 to NumFiles
  if filesize > FileList(n,2) then
    for M=NumFiles-1 to N step -1
      FileList(M+1,1)=FileList(M,1)
      FileList(M+1,2)=FileList(M,2)
      FileList(n,1)=filename
      FileList(n,2)=filesize
    Next
    if ShowDebug=1 then wscript.echo "Adding file " & filename & " at position " & n
    exit for  
  end if
Next

SmallestSize=FileList(NumFiles,2)

End Sub

Open in new window

0
 
hdazAuthor Commented:
hmmm getting pretty close to perfect..

surely the first run should only do everything past c:\Users\hdaz\Desktop\ but as you can see its picking up the path i am running the script from and not the option section.. is that how you expected it to work? would the option not over rule the first part?

C:\Users\hdaz>cscript //nologo C:\Users\hdaz\Desktop\findbiggest.vbs "c:\Users\hdaz\Desktop\"
Started at 16:36:35
1 C:\Users\hdaz\Downloads\CentOS-5.7-i386-bin-DVD\CentOS-5.7-i386-bin-DVD-1of2.iso,3520902
2 C:\Users\hdaz\Downloads\CentOS-5.7-i386-bin-DVD\CentOS-5.7-i386-bin-DVD-1of2.iso,3520902
3 C:\Users\hdaz\Desktop\a_Debug\bd_09_12_2011.dmp.gz,1942602
4 C:\Users\hdaz\AppData\Local\VirtualStore\SBSmon.zip,1628820
5 C:\Users\hdaz\Downloads\sol-11-1111-live-x86.iso,840434
6 C:\Users\hdaz\Downloads\sol-11-1111-live-x86.iso,840434
7 C:\Users\hdaz\Downloads\CentOS-5.7-i386-bin-DVD\CentOS-5.7-i386-bin-DVD-2of2.iso,657310
8 C:\Users\hdaz\Downloads\CentOS-5.7-i386-bin-DVD\CentOS-5.7-i386-bin-DVD-2of2.iso,657310
9 C:\Users\hdaz\Downloads\CentOS-5.7-x86_64-bin-DVD\CentOS-5.7-x86_64-bin-DVD-2of2.iso,655358
10 C:\Users\hdaz\Downloads\CentOS-5.7-x86_64-bin-DVD\CentOS-5.7-x86_64-bin-DVD-2of2.iso,655358
11 C:\Users\hdaz\Downloads\VMware-server-2.0.2-203138.exe,519660
12 C:\Users\hdaz\Downloads\VMware-server-2.0.2-203138.exe,519660
13 C:\Users\hdaz\Downloads\VMware-VMvisor-InstallerCD-4.1_Update1-348481.x86_64-DellCustomized_A05.iso,432376
14 C:\Users\hdaz\Downloads\VMware-VMvisor-InstallerCD-4.1_Update1-348481.x86_64-DellCustomized_A05.iso,432376
15 C:\Users\hdaz\Downloads\VMware-viclient-all-5.0.0-455964.exe,358462
16 C:\Users\hdaz\Downloads\VMware-VMvisor-Installer-5.0.0-469512.x86_64.iso,297714
17 C:\Users\hdaz\Downloads\VMware-VMvisor-Installer-5.0.0-469512.x86_64.iso,297714
18 C:\Users\hdaz\Downloads\jbosstools-3.2.0.GA.aggregate-Update-2011-02-16_18-30-44-H329.zip,169190
19 C:\Users\hdaz\Desktop\a_Debug\aNotify_from_web_aSite_log.zip,153009
20 C:\Users\hdaz\Downloads\VMware-converter-all-5.0.0-470252.exe,129727
Ended at 16:36:57 having checked 12707 files

C:\Users\hdaz>cscript //nologo C:\Users\hdaz\Desktop\findbiggest.vbs "c:\"
Started at 16:40:47
1 C:\Users\hdaz\Downloads\CentOS-5.7-i386-bin-DVD\CentOS-5.7-i386-bin-DVD-1of2.iso,3520902
2 C:\Users\hdaz\Downloads\CentOS-5.7-i386-bin-DVD\CentOS-5.7-i386-bin-DVD-1of2.iso,3520902
3 C:\Users\hdaz\Desktop\a_Debug\bd_09_12_2011.dmp.gz,1942602
4 C:\Users\hdaz\AppData\Local\VirtualStore\SBSmon.zip,1628820
5 C:\Users\hdaz\Downloads\sol-11-1111-live-x86.iso,840434
6 C:\Users\hdaz\Downloads\sol-11-1111-live-x86.iso,840434
7 C:\Users\hdaz\Downloads\CentOS-5.7-i386-bin-DVD\CentOS-5.7-i386-bin-DVD-2of2.iso,657310
8 C:\Users\hdaz\Downloads\CentOS-5.7-i386-bin-DVD\CentOS-5.7-i386-bin-DVD-2of2.iso,657310
9 C:\Users\hdaz\Downloads\CentOS-5.7-x86_64-bin-DVD\CentOS-5.7-x86_64-bin-DVD-2of2.iso,655358
10 C:\Users\hdaz\Downloads\CentOS-5.7-x86_64-bin-DVD\CentOS-5.7-x86_64-bin-DVD-2of2.iso,655358
11 C:\Users\hdaz\Downloads\VMware-server-2.0.2-203138.exe,519660
12 C:\Users\hdaz\Downloads\VMware-server-2.0.2-203138.exe,519660
13 C:\Users\hdaz\Downloads\VMware-VMvisor-InstallerCD-4.1_Update1-348481.x86_64-DellCustomized_A05.iso,432376
14 C:\Users\hdaz\Downloads\VMware-VMvisor-InstallerCD-4.1_Update1-348481.x86_64-DellCustomized_A05.iso,432376
15 C:\Users\hdaz\Downloads\VMware-viclient-all-5.0.0-455964.exe,358462
16 C:\Users\hdaz\Downloads\VMware-VMvisor-Installer-5.0.0-469512.x86_64.iso,297714
17 C:\Users\hdaz\Downloads\VMware-VMvisor-Installer-5.0.0-469512.x86_64.iso,297714
18 C:\Users\hdaz\Downloads\jbosstools-3.2.0.GA.aggregate-Update-2011-02-16_18-30-44-H329.zip,169190
19 C:\Users\hdaz\Desktop\a_Debug\aNotify_from_web_aSite_log.zip,153009
20 C:\Users\hdaz\Downloads\VMware-converter-all-5.0.0-470252.exe,129727
Ended at 16:41:01 having checked 12707 files
0
 
hdazAuthor Commented:
the new one gives

C:\Users\hdaz>cscript //nologo C:\Users\dfoster\Desktop\findbiggest.vbs "c:\"
C:\Users\hdaz\Desktop\findbiggest.vbs(41, 124) Microsoft VBScript compilation error: Syntax error
0
 
Steve KnightIT ConsultancyCommented:
hmm, will check in a mo.... Maybe i pasted wrong version.... At home with boys hovering around me!
0
 
hdazAuthor Commented:
you dont have a script for that do you..... ;) "hovering"
0
 
hdazAuthor Commented:
oops hoover ;)
0
 
Steve KnightIT ConsultancyCommented:
@echo off
echo Going out....
:loop
  Set Wife=OnDuty
goto loop

or more realistically:

@echo off
echo Not really...

Seems OK to me though:

C:\3. UTILS\scripts>mode 132
C:\3. UTILS\scripts>cscript //nologo findbiggest.vbs c:\
Started at 17:05:26
  11,138,821 = C:\1. DATA\Videos\Misc\Italy 2005\Italy1_004.AVI
  11,138,821 = C:\1. DATA\Videos\Misc\Italy 2005\Italy1_004.AVI
   6,017,920 = C:\5. VMWARE\_Customer Access\DCC-XP-QUATTRO\Windows XP Professional.vmdk
   6,017,920 = C:\5. VMWARE\_Customer Access\DCC-XP-QUATTRO\Windows XP Professional.vmdk
   4,185,024 = C:\5. VMWARE\_Customer Access\DCC-VPN-PC-XP-R5\dcc-vpn-pc-xp.vmdk
   4,185,024 = C:\5. VMWARE\_Customer Access\DCC-VPN-PC-XP-R5\dcc-vpn-pc-xp.vmdk
   4,184,704 = C:\5. VMWARE\_Customer Access\DCC-VPN-PC-XP-R6.5\dcc-vpn-pc-xp.vmdk
   4,184,704 = C:\5. VMWARE\_Customer Access\DCC-VPN-PC-XP-R6.5\dcc-vpn-pc-xp.vmdk
   3,998,464 = C:\5. VMWARE\_Customer Access\DCC-WXP-SPONTEX\DCC-WXP-SPONTEX
   3,998,464 = C:\5. VMWARE\_Customer Access\DCC-WXP-SPONTEX\DCC-WXP-SPONTEX
   3,933,120 = C:\5. VMWARE\DCC-W2K3R2S-01\DCC-W2K3R2S-01-C.vmdk
   3,855,040 = C:\5. VMWARE\DCC-WXP-01\DCC-WXP-01
   3,855,040 = C:\5. VMWARE\DCC-WXP-01\DCC-WXP-01
   3,725,696 = C:\5. VMWARE\DCC-XP-NOTES-85\Windows XP Professional.vmdk
   3,725,696 = C:\5. VMWARE\DCC-XP-NOTES-85\Windows XP Professional.vmdk
   3,152,512 = C:\5. VMWARE\DCC-WXP-02\DCC-WXP-02
   2,665,600 = C:\5. VMWARE\_Customer Access\DCC-VPN-PC-XP-R5\dcc-vpn-pc-xp-ddrive.vmdk
   2,665,600 = C:\5. VMWARE\_Customer Access\DCC-VPN-PC-XP-R5\dcc-vpn-pc-xp-ddrive.vmdk
   2,267,968 = C:\5. VMWARE\DCC-W2K3S-02\DCC-W2K3S-02-C.vmdk
   2,180,416 = C:\5. VMWARE\_Customer Access\DCC-XP-QUATTRO\DCC XP QUATTRO.vmdk
Ended at 17:05:51 having checked 164691 files

C:\3. UTILS\scripts>
on error resume next

' Script to find all files in a folder beyond a certain size
' Stores array of top 20

Dim fso
Dim oFile
Dim oFolder
Set fso = CreateObject("Scripting.FileSystemObject")

Dim OnlyBiggerThan, SmallestSize, Size, n
Dim FileList(20,2)
Dim Count

Const NumFiles=20 ' Number of files to record.  Make same as array above
Const ShowDebug=0   ' Make 1 to show files as added to array
OnlyBiggerThan=10240 ' Kb = 10Mb.  Only report files bigger than this


' Prepare array
For n=1 to NumFiles
  FileList(n,1)=""
  FileList(n,2)=0
NEXT

wscript.echo "Started at " & time

' Call initial folder and it will work down subdirs
' By running recursively through subdirs after doing files
' in each dir. 

if wscript.arguments.count > 0 then
  FindFiles wscript.arguments.item(0)
else
  wscript.echo "No directory specified.  Usage: cscript //nologo findbiggest.vbs ""c:\windows"""
  wscript.quit
end if

' Display results at the end
For n=1 to NumFiles
  if FileList(n,2)<>0 then wscript.echo right("           " & formatnumber(FileList(n,2),0,false,false,true),12) & " = " & 

FileList(n,1)
NEXT

wscript.echo "Ended at " & time & " having checked " & count & " files"

Set oFolder = Nothing
Set oFile = Nothing
Set fso = Nothing

wscript.quit

' Function to run through all files and check them then call itself recursively through subdirs.
Function FindFiles(foldername)
Set oFolder = fso.GetFolder(foldername)

For Each oFile in oFolder.Files
  Count=Count+1
  on error resume next 
  size=int(ofile.size/1024)

  If size > OnlyBiggerThan then
    If size > SmallestSize then
        AddToArray foldername & "\" & oFile.name,size
     End If
  End If
  err.clear
Next

on error resume next 
For Each subFolder In oFolder.SubFolders
    on error resume next
    FindFiles subFolder.Path
    err.clear
Next

End Function

Sub AddToArray(filename,filesize)
' Keep array of biggest 20 files in order.
' SmallestSize = don't need to add anything unde
' Find slot for this size.  Check up the array until file size is bigger
' If file size is bigger than this entry (n) then shuffle entries below this down the array one

For N=1 to NumFiles
  if filesize > FileList(n,2) then
    for M=NumFiles-1 to N step -1
      FileList(M+1,1)=FileList(M,1)
      FileList(M+1,2)=FileList(M,2)
      FileList(n,1)=filename
      FileList(n,2)=filesize
    Next
    if ShowDebug=1 then wscript.echo "Adding file " & filename & " at position " & n
    exit for  
  end if
Next

SmallestSize=FileList(NumFiles,2)

End Sub

Open in new window

0
 
Steve KnightIT ConsultancyCommented:
OK, actually it seems it has cut two lines in half above, they are OK on my notepad here:

if FileList(n,2)<>0 then wscript.echo right("           " & formatnumber(FileList(n,2),0,false,false,true),12) & " = " & FileList(n,1)

Is all one line, maybe as it shows as different above then it has split when you copied it too?

Steve
0
 
hdazAuthor Commented:
Steve,

I dont think I can break it this time.. :) Many thanks for the changes and effort...

Iv shared the points as fairly as I can because the other two answers where not "wrong" as it were.. :) powershell is installed by defualt on Win2008 SP2.. etc..

hdaz
0
 
Steve KnightIT ConsultancyCommented:
Good... got there in the end yes it was a bit cobbled together due to lack of time.  Will tidy it up and post on my script site for that entry...
Steve
0
 
Bill PrewCommented:
Steve,

I may not have fully digested this, but in taking a first look I had a question.  Should perhaps the following line:

      If size > SmallestSize then

be:

      If size >= SmallestSize then

The worst case that got me thinking about this was let's say all 20 of the largest files were the same size.  Wouldn't we add the first to the array, but not the others?  If that is a problem, then I think it could occur anytime two large files (in the top 20) are process one after the other.

~bp
0
 
Steve KnightIT ConsultancyCommented:
a good point bill.... But we only need to know the smallest size when the array is full, prior to that smallestsize returns 0 as it is the last entry in the array, not the last filled entry.  it however is only a problem in that the smallest size is the size of the last entry in the array so if there are multiple files that size then it would never report them.... But that is hazard of reporting top 20 files, not those over n bytes...

As it stands if you made it >= then the bottom entry would continuously cycle through all the entries of the same size.

I was stuck before when it bombed out on permission denied errors but put some more direct error checking in each area which got AROUND it but dont like it 'cos it's messy!

Steve
0
 
Steve KnightIT ConsultancyCommented:
OK tidied it up a bit.... with optional command line options for max. number to report /n:xx and minimum size to report /s:xxx (in Mb).  Directory now needs /d:

i.e.

cscript fiindbiggest.vbs /d:"c:\" /n:50 /s:100

Is 50 biggest files over 100Mb
findbiggest.vbs
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.