Dos Command/Batch file to find a folder path & file size

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
pallavi_23
Posts: 4
Joined: 21 Oct 2011 03:37

Dos Command/Batch file to find a folder path & file size

#1 Post by pallavi_23 » 21 Oct 2011 03:42

Dos Command/Batch file to find a folder path & file size

Hello,
I need a DOS command/Batch file to get the folder size alone.
Example C:\Project/sample.doc
Size is :15KB

Batch file must be DOS based.

Please help me on this.
Thanks

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Dos Command/Batch file to find a folder path & file size

#2 Post by dbenham » 21 Oct 2011 06:31

Your request is not clear - do you want folder size or file size :?:

File size is easy.

Using an argument passed into a batch file or batch function - This code assumes the path was passed as the first argument:

Code: Select all

set size=%~z1


You can also use FOR - This code assumes you are using a batch script. If using command line then use % instead of %%:

Code: Select all

for %%F in ("C:\Project/sample.doc") do set size=%%~zF


I'm not aware of an easy way to get folder size.

If you want disk size of a folder, I haven't a clue.

If you want cumulative size of all files in a folder (disregarding hidden and system files), this will work:

Code: Select all

set size=0
for %%F in ("C:\Project\*") do set /a size+=%%~zF

As will this:

Code: Select all

for /f "tokens=3" %%S in ('dir "C:\Project\" ^| findstr /c:"File(s)"') do set size=%%S
set size=%size:,=%


If you want cumulative size of all files in a folder, including all sub-folders (disregarding hidden and system files), this should work (untested):

Code: Select all

setlocal disableDelayedExpansion
set myPath="C:\Project"
set size=0
for %%F in (%myPath%) do set /a size+=%%~zF
for /d /r %myPath% %%D in (*) do (
  for /f "tokens=3" %%S in ('dir "%%~fD" ^| findstr /c:"File(s)"') do set tempSize=%%S
  setlocal enableDelayedExpansion
  for /f %%S in ("!tempSize:,=!") do (
    endlocal
    set /a size+=%%S
  )
)
endlocal & set size=%size%


If you want to include hidden and system file sizes, I suppose you could modify the above methods.

Dave Benham

pallavi_23
Posts: 4
Joined: 21 Oct 2011 03:37

Re: Dos Command/Batch file to find a folder path & file size

#3 Post by pallavi_23 » 23 Oct 2011 21:31

Hi Dave Benham,

Many thanks for the mail.

Actually my requirement is to get the folder size and folder path in a particular drive.

Suppopse in my D drive i have 10 folders,so i will run this particular program...it should show all its folder path and size.

In short i want know the path of that folder and the space its occuping(Size).

Now i think its clear.

Thanks,
Pallavi Bhattacharjee

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: Dos Command/Batch file to find a folder path & file size

#4 Post by !k » 24 Oct 2011 04:58

Code: Select all

@echo off &setlocal

set "dir=D:"

for /d %%d in ("%dir%\*") do call :size "%%d"
pause
goto :eof

:size
setlocal enabledelayedexpansion
set /a all=size=0
for /f "tokens=1-3" %%x in ('dir /s/-c %1 2^>nul ^|findstr /c:"File(s)"') do (
   if "!all!"=="1" set size=%%z
   if "%%y"=="File(s):" (set /a all=1) else set /a all=0
)
echo %size% Bytes in %1
endlocal
goto :eof


pallavi_23
Posts: 4
Joined: 21 Oct 2011 03:37

Re: Dos Command/Batch file to find a folder path & file size

#5 Post by pallavi_23 » 22 Nov 2011 00:38

hi,

I am geting the size in Byte so how to convert it into BG and MB?

Here I am attaching my piece of code

echo off &setlocal

set "dir=D:\Project"

for /d %%d in ("%dir%\*") do call :size "%%d"

pause
goto :eof

:size
setlocal enabledelayedexpansion
set dir=%1
set size=0
set part1=%%b
set part2=%%c
)
)

for /R %dir% %%F in (*) do ( set /a size=!size!+%%~zF )
)
echo %size% Bytes in %1

endlocal
goto :eof

renzlo
Posts: 116
Joined: 03 May 2011 19:06

Re: Dos Command/Batch file to find a folder path & file size

#6 Post by renzlo » 22 Nov 2011 01:16

Hi pallavi_23,

In standard conversion, there are 1,000,000 bytes in 1 mb. Try to divide size by 1,000,000. The problem now is dos don't support decimal point.

-renzlo

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: Dos Command/Batch file to find a folder path & file size

#7 Post by !k » 22 Nov 2011 11:10

renzlo
real problem is 2,147,483,648 limit of CMD mathematic

pallavi_23

Code: Select all

@echo off &setlocal enableextensions

set "dir=D:"

for /d %%d in ("%dir%\*") do call :size "%%d"
pause
goto :eof

:size
set "all=0"
setlocal enabledelayedexpansion
for /f "tokens=1-3" %%x in ('dir /s/-c %1 2^>nul ^|findstr /c:"File(s)"') do (
   if "!all!"=="1" set "B=%%z"
   if "%%y"=="File(s):" (set all=1) else set "all=0"
)
call :1000 !B! k &call :1000 !k! M &call :1000 !M! G
echo !G!.!G_! GB (!M!.!M_! MB; !k!.!k_! kB) in %1
endlocal
goto :eof

:1000
set "s=%1"
if "!s:~0,-3!"=="" (set %2=0) else set "%2=!s:~0,-3!"
if "!s:~0,-2!"=="" (set %2_=0) else set "%2_=!s:~-3,1!"
goto :eof

pallavi_23
Posts: 4
Joined: 21 Oct 2011 03:37

Re: Dos Command/Batch file to find a folder path & file size

#8 Post by pallavi_23 » 22 Nov 2011 23:18

Hi !k,

Thanks for the mail.

I have tried ur code but its showing some other error message.

here I am attaching the output :

Image

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: Dos Command/Batch file to find a folder path & file size

#9 Post by !k » 26 Nov 2011 08:05

I see no image

Post Reply