Solved

Batch file to list subfolders and their sizes

Posted on 2009-03-31
15
3,334 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 

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
 

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 DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

Question has a verified solution.

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

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

688 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