Page 1 of 1
Dos Command/Batch file to find a folder path & file size
Posted: 21 Oct 2011 03:42
by pallavi_23
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
Re: Dos Command/Batch file to find a folder path & file size
Posted: 21 Oct 2011 06:31
by dbenham
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:
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
Re: Dos Command/Batch file to find a folder path & file size
Posted: 23 Oct 2011 21:31
by pallavi_23
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
Re: Dos Command/Batch file to find a folder path & file size
Posted: 24 Oct 2011 04:58
by !k
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
Re: Dos Command/Batch file to find a folder path & file size
Posted: 22 Nov 2011 00:38
by pallavi_23
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
Re: Dos Command/Batch file to find a folder path & file size
Posted: 22 Nov 2011 01:16
by renzlo
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
Re: Dos Command/Batch file to find a folder path & file size
Posted: 22 Nov 2011 11:10
by !k
renzloreal problem is 2,147,483,648 limit of CMD mathematic
pallavi_23Code: 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
Re: Dos Command/Batch file to find a folder path & file size
Posted: 22 Nov 2011 23:18
by pallavi_23
Hi !k,
Thanks for the mail.
I have tried ur code but its showing some other error message.
here I am attaching the output :
Re: Dos Command/Batch file to find a folder path & file size
Posted: 26 Nov 2011 08:05
by !k
I see no image