Dos script to compare folder size

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
roasted_almond
Posts: 2
Joined: 25 Jan 2013 23:41

Dos script to compare folder size

#1 Post by roasted_almond » 25 Jan 2013 23:49

Hello,

I need a script to compare folder size and display which folder has largest size.
example:

I have folders on one server
for server1:
folders are d:\app\temp, d:\data\temp2 and d:\res\temp3

for server 2:
folder e:\backuplocation

I want to copy d:\app\temp, d:\data\temp2 and d:\res\temp3 folders from server1 to e:\backuplocation on server 2 but before copying to e:\backuplocation, i need to check whether there is sufficient free space is available or not at e:\backuplocation. For that we need to take sum of sizes of d:\app\temp, d:\data\temp2 and d:\res\temp3 folders and compare to free size of e: drive on server2.

Currently sizes of d:\app\temp is 40 GB , d:\data\temp2 is 100 GB and d:\res\temp3 is 10 GB so sum will be 150 GB.

Both servers are windows servers.

Can someone please help me in this? Its really important for me.

Thakns in advance :)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Dos script to compare folder size

#2 Post by foxidrive » 26 Jan 2013 01:04

This should give you the numbers: EDIT: See my post below, as this just prints space used in all the listed folders.


Code: Select all

@echo off
For %%z in (d:\app\temp d:\data\temp2 d:\res\temp3 e:\backuplocation) do (
for /f "tokens=3" %%a in ('dir %%z ^|find " File(s) "') do echo %%a
)

roasted_almond
Posts: 2
Joined: 25 Jan 2013 23:41

Re: Dos script to compare folder size

#3 Post by roasted_almond » 26 Jan 2013 01:12

I am sorry, but its not working....

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Dos script to compare folder size

#4 Post by foxidrive » 26 Jan 2013 02:56

What OS are you using? Does it print anything to the console?

Try this - it just prints the four folders in order and shows you how much it uses, and is free.

Code: Select all

@echo off
For %%z in (d:\app\temp d:\data\temp2 d:\res\temp3) do (
for /f "tokens=3" %%a in ('dir %%z ^|find " File(s) "') do echo %%z - %%a used
)

For %%z in (e:\backuplocation) do (
for /f "tokens=3" %%a in ('dir %%z ^|find " Dir(s) "') do echo %%z - %%a free
)

pause

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Dos script to compare folder size

#5 Post by abc0502 » 26 Jan 2013 03:07

You can use this VBscript to get folder size, but it will be in bytes:

Code: Select all

dim oFS, oFolder
set oFS = WScript.CreateObject("Scripting.FileSystemObject")
set oFolder = oFS.GetFolder("D:\folder_name")
wscript.echo oFolder.Name & " : " & round(oFolder.Size,2)
Change :
oFolder.Size
to :
oFolder.Size/1024/1024/1024
to get the size in GB

source

Edited:
I was able to modify it to output The result to an external file, in this form:
Folder_Name:Size_in_GB

This is the modified VBscript:

Code: Select all

dim oFS, oFolder, objShell
set oFS = WScript.CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("wscript.shell")
Set fsHandle = oFS.OpenTextFile ("D:\OutPut.txt",8,True)
set oFolder = oFS.GetFolder("D:\Folder_Name")
fsHandle.Writeline oFolder.Name & " : " & round(oFolder.Size/1024/1024/1024,2)

and this is the batch, it should give you all the size of all folders one after the other:

Code: Select all

@Echo OFF
:: Set Outfile and Folders Names
SET "OutFile=D:\out.txt"
SET "Folders=D:\folder_1 D:\Folder_2 D:\Folder_3"
:: Delete Old Results
IF Exist "%OutFile%" Del /Q "%OutFile%"
:: Get Size of All Folders
For %%a In (%Folders%) Do (
   Call :size "%%a" "%OutFile%"
)
:: Display Each Folder with it's Size in GB
For /F "tokens=1,2 delims=:" %%A In ('TYPE "%OutFile%"') Do (
   Echo Folder Name: %%A
   Echo Folder Size:%%B GB
   Echo.
   )
pause
Exit /B

:size <Folder_Name> <Output_File_Path>
(
echo.dim oFS, oFolder, objShell
echo.set oFS = WScript.CreateObject^("Scripting.FileSystemObject"^)
echo.Set objShell = CreateObject^("wscript.shell"^)
echo.Set fsHandle = oFS.OpenTextFile ^("%~2",8,True^)
echo.set oFolder = oFS.GetFolder^("%~1"^)
echo.fsHandle.Writeline oFolder.Name ^& " : " ^& round^(oFolder.Size/1024/1024/1024,2^)
)>"%temp%\size.vbs"
"%temp%\size.vbs"

Change The "OutFile" Variable to the destination where the size will be written to.
Change The "Folders" Variable to the names of each folder seprated by a "SPACE" ( folder names can't have spaces in it )

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Dos script to compare folder size

#6 Post by abc0502 » 26 Jan 2013 04:11

This one will also calculate the total size of all folders that you will get there size
Folder Names Can't have Spaces

Code: Select all

@Echo OFF
:: Set Outfile and Folders Names
SET "OutFile=D:\out.txt"
SET "Folders=D:\Fiesta D:\KOS D:\PointBlank"
:: Delete Old Results
IF Exist "%OutFile%" Del /Q "%OutFile%"
:: Get Size of All Folders
For %%a In (%Folders%) Do (
   Call :size "%%a" "%OutFile%"
)
:: Display Each Folder with it's Size in GB
Echo.
For /F "tokens=1,2 delims=:" %%A In ('TYPE "%OutFile%"') Do (
   Echo  Folder Name: %%A
   Echo  Folder Size:%%B GB
   Echo.
   )
:: Display Total Size of All Folders
set Total=0
For /F "tokens=1,2,3 delims=:." %%A In ('Type "%OutFile%"') Do SET /A Total += %%B%%C
:: Case 1 Less Than 1 GB and Greater Than 0.1 "in 2nd half"
IF "%Total:~0,-2%" == "" (
   IF NOT "%Total:~-2,-1%" == "" (
      Echo  ^> Total Size: 0.%Total:~-2% GB
      Goto :End
   )
)
:: Case 2 Less Than 1 GB and Less Than 0.1 "in 2nd half"
IF "%Total:~0,-2%" == "" (
   IF "%Total:~-2,-1%" == "" (
      Echo  ^> Total Size: 0.0%Total:~-2% GB
      Goto :End
   )
)
:: Case 3 Greater Than 1 GB and Less Than 0.1 "in 2nd half"
IF NOT "%Total:~0,-2%" == "" (
   IF "%Total:~-2,-1%" == "" (
      Echo  ^> Total Size: %Total:~0,-2%.0%Total:~-2% GB
      Goto :End
   )
)
:: Case 4 Greater Than 1 GB and Greater Than 0.1 "in 2nd half"
Echo  ^> Total Size: %Total:~0,-2%.%Total:~-2% GB

:End
Pause >nul
Del /Q "%OutFile%" >nul
Exit /B

:size <Folder_Name> <Output_File_Path>
(
echo.dim oFS, oFolder, objShell
echo.set oFS = WScript.CreateObject^("Scripting.FileSystemObject"^)
echo.Set objShell = CreateObject^("wscript.shell"^)
echo.Set fsHandle = oFS.OpenTextFile ^("%~2",8,True^)
echo.set oFolder = oFS.GetFolder^("%~1"^)
echo.fsHandle.Writeline oFolder.Name ^& " : " ^& round^(oFolder.Size/1024/1024/1024,2^)
)>"%temp%\size.vbs"
"%temp%\size.vbs"
IF You get an error in a message box related to VBscript, check your folder names

Post Reply