Wednesday, January 6, 2010

Find out Disk Space Remotely thanks to DIR / Borrowing code

I just came across a really handy script to find out Total Disk sapce and free space and it works remotely just by using the dir command, this is awesome.

Source http://www.computing.net/answers/programming/batch-file-reporting-hdd-space/15057.html

Thank you Douglas for sharing this with the rest of the world.
-----------------------------------
:: tester.bat
:: 2007 Douglas Craig with massive assistance by Mechanix2Go

@echo off

setLocal EnableDelayedExpansion

:: check for path
if ()==(%1%) goto syntax

:: check existence of path
if not exist %1 goto badpath

:: find total used space
for /f "tokens=3 delims= " %%A in ('dir /s/-c %1 ^|find "File(s)"') do (
set totalused=%%A
)

:: find total free space
for /f "tokens=3 delims= " %%A in ('dir /-c %1 ^|find "Dir(s)"') do (
set freespace=%%A
)

:: output truncated totals in temp file for use later
echo.Used !totalused:~0,-9! >"testtemp.txt"
echo.Free !freespace:~0,-9! >>"testtemp.txt"

:: input truncated total used space from temp file
for /f "tokens=2 delims= " %%A in ('type testtemp.txt ^|find "Used"') do (
set totalused=%%A
)

:: input truncated free space
for /f "tokens=2 delims= " %%A in ('type testtemp.txt ^|find "Free"') do (
set freespace=%%A
)

:: calculate total drive space, percentage used, and percentage free
set /a totalspace=!totalused!+!freespace!
set /a percentused=(!totalused!*100)/!totalspace!
set /a percentfree=100-!percentused!

:: output results
echo.
echo Total space: %totalspace% GB
echo Free space: %freespace% GB (%percentfree%%%)
echo Used space: %totalused% GB (%percentused%%%)

:: clean up temp file
del testtemp.txt

goto end

:: syntax info
:syntax
echo.
echo.Usage: TESTER [path]
echo.
echo. where [path] is a directory on drive or network path
echo.
echo. example: TESTER \
echo. TESTER \\mynetwork\sharedfolder
goto end

:: invalid path
:badpath
echo.
echo.The path %1 is not accessible.
echo.

:end
-----------------------------------
What I would do is :
for /f %%i in (serverlist.txt) do TESTER.cmd \\%%i\c$ >>%%i.txt

et voilĂ 

0 comments:

Post a Comment