views:

2426

answers:

4

Hi guys just wondering if u can help me modify this script that ive been playing around with, i cant get it to accept wildcard charcters '*'

@echo off
setLocal EnableDelayedExpansion
set /a value=0
set /a sum=0
FOR /R %1 %%I IN (*) DO (
set /a value=%%~zI/1024
set /a sum=!sum!+!value!
)
@echo Size is: !sum! k

Its in a batch file called dirsize and is called like so

dirsize c:\folder

I want it to check folder sizes for me, this one here is an example, the cache in firefox

dirsize C:\users\%username%\AppData\Local\Mozilla\Firefox\*.default\Cache

Returns the value 0

But if I go

dirsize C:\users\%username%\AppData\Local\Mozilla\Firefox\sr1znnb4.default\Cache

It works and I get the value 55322 returned.. Help me please? Thank you in advance kind people

A: 

You would need to wrap what you currently have in another for loop which expands the * into a series of directories.

So

@echo off
setLocal EnableDelayedExpansion
set /a sum=0
for /d %%D in (%1) do (
  FOR /R %%D %%I IN (*) DO (
    set /a value=%%~zI/1024
    set /a sum=!sum!+!value!
  )
)
echo Size is: !sum! k

might work (untested, though, but should give you the general idea)

Joey
A: 

If you have a Windows version of grep (you can find one at GNU utilities for Win32) you can do something like this:

dir /s %1 | grep --after-context=1 "Total Files Listed:"
Patrick Cuff
A: 

PowerShell makes this easy, of course:

    (gci . -Recurse | Measure-Object -Property Length -Sum).Sum

And PowerShell is already installed on Windows 7. Get on the bandwagon! :-)

Also, C:\users\%USERNAME%\AppData\ isn't a reliable way of finding AppData. Try %APPDATA% (or $env:APPDATA in PowerShell).

Jay Bazuzi
A: 

Ok for a starter, lets say wildcards are not well supported throughout batch files, and specially in for loops.
That's what I came up with:

DirSize.cmd "valid path"

@echo off
rem Args[1]= "Path" or "valid path with wildcard"
set _size=0
set dRaw=%~1
echo %~1|findstr "*"&& set dSource=%~dp1|| (
    set dSource=%~1\
    if not "%dRaw:~-1%"=="\" set dRaw=%dRaw%\
)
for /f "delims=" %%D in ('dir /ad /b %dRaw%') do call :DirSize "%dSource%%%~D"
set /a size=_size/1024
echo size=%size% k (%_size% bytes)
exit /b 0

:DirSize
    call :FileSize "%~1"
    for /f "delims=" %%d in ('dir /ad /b "%~1"2^>nul') do call :DirSize "%~1\%%~d"
    exit /b 0

:FileSize
    for /f "delims=" %%f in ('dir /a-d /b "%~1\"2^>nul') do for %%F in (%1\%%f) do set /a _size+=%%~zF
    exit /b 0

___Usage___
Argument: Needs to be a path (not a file) that either contain wildcards or not. Path need top be valid when typing dir path

___Notes___
This script does check for all files and sub-directories, even hidden/system ones. change the dir /a argument and add -h-s (like /ad-h-s) to remove this behaviour.

I revised the argument checking to my liking now, so I think it is usable.

I hope this helps.

Jay