get size and # of files from dir /s command

Microsoft Windows xp professional w/serv...
January 9, 2011 at 08:48:08
Specs: Windows 7

How can I get size and # of files from doing a dir /s and into a variable?

I was thinking about doing something like this

dir /s >directorylist.txt

read up 3 lines from that to parse # of files and total bytes then output into %totalfiles% and %sizefiles% and echo that

I am just starting to learn this so I don't know if I explained it very well

Thank You!

See More: get size and # of files from dir /s command

Report •

January 9, 2011 at 09:59:53
Try this:


FOR /f "tokens=1 delims=:" %%a IN ('DIR /s^| FINDSTR /n /i "total files listed"') DO SET LNum=%%a
FOR /f "skip=%LNum% tokens=1-2 delims= " %%a IN ('DIR /s') DO (
SET FNum=%%a %%b
FOR /f "skip=%LNum% tokens=3-4 delims= " %%a IN ('DIR /s') DO (
SET FSize=%%a %%b
GOTO Next2
ECHO %FSize%

Can I put a question here?

Report •

January 9, 2011 at 18:30:59
Hi Matt,

It works on my Windows 2003 R2 box but on Windows XP it does not. How bizarre! I really like where we're going so far. My ultimate intention for this is to incorporate it into a backup script I'm using in conjunction with robocopy and 7zip, splitting them into 100mb files and then uploading via FTP. I want this dir script here to read all the .7z to be FTPed, then send a 1mb test file to the FTP server to get an estimate of time and pipe this directory info and do some math to figure out estimated duration. Do you think this is doable?

Thanks for everything!

Report •

January 9, 2011 at 18:50:55
Edit: I saw win7 listed but missed tags for xp

Unless they changed the format on win7, this might also work:

for /f "tokens=1,3" %%a in ('dir /a/s/-c ^| find "File(s)" ') do (
    set totalfiles=%%a
    set sizefiles=%%b
echo %sizefiles% Bytes
echo %totalfiles% Files

Report •

Related Solutions

January 9, 2011 at 22:00:23
Hello Judago-

Actually both do work on Win7 and XP. I found out what my problem was. I'm only wanting to collect the totals on the current directory, not recursive. If you do a dir in the current directory without /s, it's missing the line:

Total Files Listed:

Right above the # of files & bytes

So yours actually works better because it doesn't look at "Total Files Listed:". Also what does the ^ do right before the | ?

This scripting stuff is new territory for me. I've worked in support roles the last 15 years but never delved into scripting this much. It does seem, though, that UNIX seems more straightforward, IMHO :)

Thanks so much to you both!


Report •

January 9, 2011 at 22:24:52
"^" is the escape character in batch, the pipe needs to be escaped so it isn't executed with the for loop, but by the for loop.

Report •

Ask Question