Solved

List files and their sizes in subfolders

Posted on 2013-05-22
8
388 Views
Last Modified: 2013-05-25
Hello,

Is there any easy way to list all files (and their sizes) that are contained in sub folders.
Run something like:

D:\MainFolder\list_files_and_size.bat

And get a list of all files and their sizes in all subfolders of MainFolder?

Thank you
0
Comment
Question by:miyahira
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 8

Expert Comment

by:Dipak
ID: 39189516
dir /s This command displays files in specified directory and all subdirectories.
0
 
LVL 1

Author Comment

by:miyahira
ID: 39189538
Yea, but I only want to display SubFolder, FileName and size. No more.
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 39189572
Here's a fairly simple BAT file that will write to a CSV file with the file size and full path.  Then just open the CSV file in Excel to allow reporting, sorting, etc.  Naturally the output format could be changed if this isn't ideal...

@echo off
set BaseDir=D:\MainFolder
set OutFile=logfile.csv
(
  echo Size,Filename
  for /f "tokens=*" %%F in ('dir /s /b /a-d "%BaseDir%"') do echo %%~zI,"%%~I"
)> "%OutFile%"

Open in new window

~bp
0
 
LVL 1

Author Comment

by:miyahira
ID: 39192633
Hi billprew,
Unfortunately, command:

dir /s /b /a-d "%BaseDir%

Open in new window


only lists path and file name, as shown in image attached.

It doesn't show size of files.
DirSize.png
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 11

Accepted Solution

by:
paultomasi earned 200 total points
ID: 39192917
Your best bet is to use 'FOR /R' as in the following command:

@for /r "D:\MainFolder" %a in (*) do @echo %~dpa %~nxa %~za

Open in new window

Or you can try this neat little batch file:

@echo off
setlocal enabledelayedexpansion

for /r "D:\MainFolder" %%a in (*) do (
  set dp=%%~dpa
  echo !dp:~0,-1! %%~nxa %%~za
)

Open in new window

The '!dp:~0,-1!' strips away the trailing '\' (backslash) character from the file's pathname otherwise, '%%~dpa' gives you the drive and path of the file, '%%~nxa' gives you the filename and it's extension (if any) and '%%~za' gives you the file's size.
0
 
LVL 1

Author Comment

by:miyahira
ID: 39192940
Thanks paultomasi, you're the Best!
0
 
LVL 51

Assisted Solution

by:Bill Prew
Bill Prew earned 50 total points
ID: 39193034
Sorry, typo, should be:

@echo off
set BaseDir=D:\MainFolder
set OutFile=logfile.csv
(
  echo Size,Filename
  for /f "tokens=*" %%F in ('dir /s /b /a-d "%BaseDir%"') do echo %%~zF,"%%~F"
)> "%OutFile%"

Open in new window

~bp
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 39196879
Thank you
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

757 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

21 Experts available now in Live!

Get 1:1 Help Now