Solved

Batch file to list subfolders and their sizes

Posted on 2009-03-31
15
3,221 Views
Last Modified: 2012-05-06
I have a server with a lot of folders in d:\files.  I'm wondering if there's a way via batch file to query that folder for a subfolder listing but also get the total size of each of those subfolders and export that to a text file.

For example:  say d:\files has 9 subfolders.  The script would need to query d:\files and list those 9 subfolders (and only those 9, not the entire tree) and then list the total data size for each of those 9 subfolders.  That info is then saved to a text file.

Possible?
0
Comment
Question by:annexit
  • 7
  • 5
  • 3
15 Comments
 
LVL 33

Expert Comment

by:knightEknight
ID: 24035572
do a google search on tail.exe and download it to d:\files (or anywhere in your path).  Then do this:

d:
cd d:\files
for /f %%D in ('dir/ad/b') do @echo %%D: >> d:\files\list.txt  &  dir/s %%D | find "File(s)" | tail -n 1 >> d:\files\list.txt
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 24035579
... the code above is meant to be run as a batch file.  You can run it as a stand-alone command instead but you will need to use a single percent symbol instead of double -- e.g.  %D  instead of  %%D  every where you see it.
0
 

Author Comment

by:annexit
ID: 24039158
I'm testing this on my workstation on the c:\temp folder.  I've modified your code to look like this:

f %%D in ('dir/ad/b') do @echo %%c: >> c:\temp\list.txt  &  dir/s %%D | find "File(s)" | tail -n 1 >> c:\temp\list.txt

I get these errors at the prompt:

C:\Temp>for /F %D in ('dir/ad/b') do @echo %c:    1>>c:\temp\list.txt  & dir/s %
D   | find "File(s)"   | tail -n 1  1>>c:\temp\list.txt
TAIL: can't open 1
TAIL: can't open 1
File Not Found

C:\Temp>pause
Press any key to continue . . .

This is the list.txt file contents:
%c:  
%c:

Thoughts?
0
 

Author Comment

by:annexit
ID: 24039165
I should note that TAIL.EXE is located in the c:\temp directory, along with this batch file and the outputted list.txt file.
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 24039218
This should all be on one line - I changed your %c to %D:

for /F %D in ('dir/ad/b') do @echo %D:    1>>c:\temp\list.txt  & dir/s %D   |  find "File(s)"  |  tail -n 1  1>>c:\temp\list.txt


0
 
LVL 33

Expert Comment

by:knightEknight
ID: 24039221
(you should delete list.txt first)
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 24039237
... and of course, if you are going to run it in a batch file, you need to double the percent symbols:

del c:\temp\list.txt
for /F %%D in ('dir/ad/b') do @echo %%D:    1>>c:\temp\list.txt  & dir/s %%D   |  find "File(s)"  |  tail -n 1  1>>c:\temp\list.txt


0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:annexit
ID: 24040111
Same error:
C:\Temp>del c:\temp\list.txt
Could Not Find c:\temp\list.txt
C:\Temp>for /F %D in ('dir/ad/b') do @echo %D:       1>>c:\temp\list.txt  & dir/
s %D     | find "File(s)"    | tail -n 1   1>>c:\temp\list.txt
TAIL: can't open 1
TAIL: can't open 1
File Not Found
C:\Temp>pause
Press any key to continue . . .
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 24040225
your version of tail.exe must not be the same as the one I am using.

some things to try ...

|  tail --lines=1   1>>c:\temp\list.txt
 |  tail -n 1   >>c:\temp\list.txt

the -n 1 parameter tells tail.exe (my version) to only output one line.  I'm guessing your version must have a different flag for this.  What does the help screen suggest?   tail.exe --help
0
 
LVL 4

Expert Comment

by:ChristopherDunn
ID: 24040984
If you didn't want to use another application, you could just overwrite the variable with a for loop and that would always yield the last line.

This is pretty much the same as knightEknight's solution with a small change to eliminate the need for tail.

It works perfectly fine, but I can't get it not to echo "File not found" for each empty subdirectory. Do you know how to fix that knight?
@echo OFF

setlocal enabledelayedexpansion

set rootdir=D:\temp
 

IF EXIST "%rootdir%\list.txt" DEL "%rootdir%\list.txt"

Echo Building list %rootdir%\list.txt...
 

For /f "tokens=*" %%D in ('dir %rootdir%\ /ad /b') do (

  set size=

  For /f "tokens=* usebackq" %%S in (`dir /s "%rootdir%\%%D"^|Find "File(s)"`) do set size=%%S

  IF "!size!"=="" set size=No Files Found

  Echo %%D: !size! >>%rootdir%\list.txt

)

Echo Complete. Press Any key to exit

Pause>nul

goto :eof

Open in new window

0
 
LVL 33

Assisted Solution

by:knightEknight
knightEknight earned 100 total points
ID: 24041801
Without testing, this might eliminate the "File not Found" messages:
For /f "tokens=* usebackq" %%S in ('dir /s "%rootdir%\%%D" 2^>nul ^|Find "File(s)"') do set size=%%S
0
 

Author Comment

by:annexit
ID: 24042685
The "File not Found" message is OK, that isn't a big issue.  I tested it and it seems fine otherwise, with the exception that if the root folder has spaces in it the script bombs out.  Any way to add quotes in there somewhere to avoid this?
0
 
LVL 4

Accepted Solution

by:
ChristopherDunn earned 400 total points
ID: 24043180
Try this script. I also modified it to make the output very nice and readable. The "File not founds" were fixed thanks to knight (I was forgetting that pesky caret).
Sample Output=========================================

Contents of D:\Desktop 

 

Adobe CS3 ....................................... 247 File(s)    551,220,005 bytes 

dosUSBboot ...................................... 17 File(s)     4,370,843 bytes 

HEAT Notes and SQL .............................. 8 File(s)      123,102 bytes 

Icon Archive .................................... 15 File(s)     20,548 bytes 

MCDST ........................................... 107 File(s)    815,502,553 bytes 

Microsoft Certifications ........................ 3 File(s)      144,687 bytes 

Screen Cap ...................................... 5 File(s)      302,909 bytes 
 
 
 

listfiles.bat=========================================

@echo OFF

setlocal enabledelayedexpansion

set rootdir=D:\Desktop

set listfile=D:\Desktop\list.txt
 

IF EXIST "%listfile%" DEL "%listfile%"
 

Echo Building list of %rootdir%,

Echo and logging to %listfile%...

Echo.
 

Echo Contents of %rootdir% >>"%listfile%"

Echo. >>"%listfile%"
 

For /f "tokens=* usebackq" %%D in (`dir "%rootdir%\" /ad /b`) do (

  Echo Reading %%D...

  set size=

  For /f "tokens=1-4 usebackq delims= " %%a in (`dir /s "%rootdir%\%%D" 2^>nul ^| Find "File(s)"`) do (

    set files=%%a %%b

    set size=%%c %%d

  )

  IF "!files!"=="" set files=0 File^(s^)

  IF "!size!"=="" set size=0 Bytes

  set bufferedname=%%D ...

  For /L %%N in (1,1,45) do IF "!bufferedname:~48,1!"=="" (set bufferedname=!bufferedname!.)

  set bufferedfiles=!files!

  For /L %%N in (1,1,15) do IF "!bufferedfiles:~14,1!"=="" (set bufferedfiles=!bufferedfiles! )

  Echo !bufferedname! !bufferedfiles!!size! >>"%listfile%"

)

Echo.

Echo Complete. Press Any key to exit.

Pause>nul

goto :eof

Open in new window

0
 

Author Comment

by:annexit
ID: 24043269
It works perfectly!  Thanks guys!
0
 
LVL 4

Expert Comment

by:ChristopherDunn
ID: 24043462
You're welcome!

If you wanted to right justify the file sizes (so you can more easily see which folders are bigger) use the code below.

Have a great day,

-Chris
Sample Output=========================================

Contents of D:\Desktop 

 

Adobe CS3 ....................................... 247 File(s) ................ 551,220,005 bytes 

dosUSBboot ...................................... 17 File(s) ................... 4,370,843 bytes 

HEAT Notes and SQL .............................. 8 File(s) ...................... 123,102 bytes 

Icon Archive .................................... 15 File(s) ...................... 20,548 bytes 

MCDST ........................................... 107 File(s) ................ 815,502,553 bytes 

Microsoft Certifications ........................ 3 File(s) ...................... 144,687 bytes 

Screen Cap ...................................... 5 File(s) ...................... 302,909 bytes 
 
 

listfiles.bat=========================================

@echo OFF

setlocal enabledelayedexpansion

set rootdir=D:\Desktop

set listfile=D:\Desktop\list.txt
 

IF EXIST "%listfile%" DEL "%listfile%"
 

Echo Building list of %rootdir%,

Echo and logging to %listfile%...

Echo.
 

Echo Contents of %rootdir% >>"%listfile%"

Echo. >>"%listfile%"
 

For /f "tokens=* usebackq" %%D in (`dir "%rootdir%\" /ad /b`) do (

  Echo Reading %%D...

  set size=

  For /f "tokens=1-4 usebackq delims= " %%a in (`dir /s "%rootdir%\%%D" 2^>nul ^| Find "File(s)"`) do (

    set files=%%a %%b

    set size=%%c %%d

  )

  IF "!files!"=="" set files=0 File^(s^)

  IF "!size!"=="" set size=0 Bytes

  set bufferedname=%%D ...

  For /L %%N in (1,1,45) do IF "!bufferedname:~48,1!"=="" (set bufferedname=!bufferedname!.)

  set bufferedfiles=!files! .

  For /L %%N in (1,1,21) do IF "!bufferedfiles:~20,1!"=="" (set bufferedfiles=!bufferedfiles!.)

  set bufferedsize=. !size!

  For /L %%N in (1,1,25) do IF "!bufferedsize:~24,1!"=="" (set bufferedsize=.!bufferedsize!)

  Echo !bufferedname! !bufferedfiles!!bufferedsize!>>"%listfile%"

)

Echo.

Echo Complete. Press Any key to exit.

Pause>nul

goto :eof

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Update a File not in all folders 2 43
Python 2.7 - Passing arguments 8 58
how to execute all sub dir 2 39
Command prompt output to variable? 9 19
If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now