-1

I have a parent folder which contains more than 100 sub-folders. I want a text file which contain sub-folders and their size. Could you please help me to build the batch program?

pfnuesel
  • 14,093
  • 14
  • 58
  • 71
Lakshmi K
  • 7
  • 1

3 Answers3

0

Only for the files name without the size you can use this code-

dir *.* /b /s >> C:\ListOfFile.txt

Change your "C:\" path to whenever you want to save your file.

The scion
  • 1,001
  • 9
  • 19
  • Thank you sir. This gives output like subfolder1, subfolder2, subfolder3 etc. I wanted to built the program to porvide the size of each sub-folder too. – Lakshmi K May 12 '16 at 07:25
0

This code can did the trick :

@echo off
Title Get Size of Folder and its subfolders
set "Folder=C:\temp"
Set Log=Folder_Size.txt 
(
    echo The size of "%Folder%" is 
    Call :GetSize "%Folder%"
)> "%Log%"
For /f "delims=" %%a in ('Dir "%Folder%" /AD /b /s') do ( 
    (
        echo The size of "%%a" is 
        Call :GetSize "%%a"
    )>> "%Log%"
)
start "" "%Log%"
::***********************************************************************
:GetSize
(
echo wscript.echo GetSize("%~1"^)
echo Function GetSize(MyFolder^)
echo    Set fso = CreateObject("Scripting.FileSystemObject"^)
echo    Set objFolder= fso.GetFolder(MyFolder^)  
echo    GetSize = FormatSize(objFolder.Size^)
echo End Function
echo '*******************************************************************
echo 'Function to format a number into typical size scales
echo Function FormatSize(iSize^)
echo    aLabel = Array("bytes", "KB", "MB", "GB", "TB"^)
echo    For i = 0 to 4
echo        If iSize ^> 1024 Then
echo            iSize = iSize / 1024
echo        Else
echo            Exit For
echo        End If
echo    Next
echo    FormatSize = Round(iSize,2^) ^& " " ^& aLabel(i^)
echo End Function
echo '*******************************************************************
)>%tmp%\Size.vbs
Cscript /NoLogo %tmp%\Size.vbs
Del %tmp%\Size.vbs
Exit /b
::***********************************************************************
Hackoo
  • 18,337
  • 3
  • 40
  • 70
0

Although you did not show any effort to try it on your own, I decided to help you, because the task might not be that particularly trivial to accomplish...

Have you ever seen the output of dir /S (the command to list files in a specified directory and all its sub-directories)? It summarises the total count of files at the end, including also the overall size:

D:\TEMP>dir /S /-C
 Volume in drive D is DATA
 Volume Serial Number is XXXX-XXXX

 Directory of D:\TEMP

2016/05/12  12:00    <DIR>          .
2016/05/12  12:00    <DIR>          ..
               0 File(s)              0 bytes

     Total Files Listed:
               0 File(s)              0 bytes
               2 Dir(s)      ?????????? bytes free

So we can do a dir /S command for every directory at the given location, capture its output using a for /F loop, retrieve the line before the last one and extract the size value.

The following pure script -- let us call it folder_sizes.bat -- does exactly these steps:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Get provided arguments:
set "FOLDER=%~1"
set "LOG=%~2"

rem // Check provided arguments:
if not defined LOG set "LOG=con"
setlocal EnableDelayedExpansion
>&2 (
    if not defined FOLDER (
        echo(ERROR: no directory specified!
        exit /B 1
    ) else if not exist "!FOLDER!\" (
        rem /* (trailing "\" to check for directory) */
        echo(ERROR: directory not found!
        exit /B 1
    )
)

rem // Process all sub-directories of given directory:
> "!LOG!" (
    for /D %%D in ("!FOLDER!\*") do (
        for /F "skip=10 tokens=3" %%F in ('
            dir /S /-C "%%~fD"
        ') do (
            set "BYTES=!VALUE!"
            set "VALUE=%%F"
        )
        setlocal DisableDelayedExpansion
        set "ITEM=%%~nxD"
        setlocal EnableDelayedExpansion
        rem // Return name and size of sub-directory:
        echo(!ITEM!   !BYTES!
        endlocal
        endlocal
    )
)
endlocal

endlocal
exit /B

To run this script on a certain directory -- for instance D:\TEMP -- and to write the log data to the file folder_sizes.log in the current directory, use the following command line:

folder_sizes.bat "D:\TEMP" ".\folder_sizes.log"

In the script, delayed expansion is toggled in order to avoid problems with (sub-)directories containing exclamantion marks (!) in their names.

Notice that the output of the dir /S command is language-dependent, so the script might not provide the expected results on systems other than English ones. In such situations, the option string "skip=10 tokens=3" of the for /F loop, particularly the token option, needs to be adapted accordingly.

aschipfl
  • 33,626
  • 12
  • 54
  • 99