# Check disk free space in .BAT file



## hammad_siddiqui (Aug 16, 2011)

I have to make a script to restart SQL Server services using .BAT file if the free space in the drive where tempDB exists is less than 2GB. I already have code to restart the service. 

But I just need a script to check the free space. 
I have Windows Server 2003. I had found the script that runs on XP but it did not work on 2003.

Following is the script for Windows XP. Can somebody help me in providing the script for Windows 2003??? Thanks!


echo off
setlocal enabledelayedexpansion
for /F "tokens=1,*" %%A in ('fsutil fsinfo drives') do @for %%K in (%%B) do @for %%U in (%%K) do @for /F 
"tokens=1 delims=\" %%e in ('fsutil fsinfo drivetype %%U ^| find "Fixed"') do @for /F "tokens=4" %%o in ('fsutil 
fsinfo ntfsinfo %%e ^| find "Free Clusters"') do @set /a free=%%o / 256 > nul & echo %%e !free!
echo !free!
if /i %free% leq 2048 echo Less


----------



## Rockn (Jul 29, 2001)

I am sure there is a better way to manage the tempDB in SQL manager instead of restarting the service to do it.


----------



## Squashman (Apr 4, 2003)

Any batch file that runs on XP should run on Windows 2003 as far as I remember.

Where did you get this batch file from? If you got it from another website could you point us to it.

It is always nice if you wrap code tags around your code.

Just looking at this batch file it looks like it is going to find the free space on all the fixed drives attached to your server. Which seems like overkill to me. You just want to find the free space on one drive. Seems like it would be easier to use the DIR command on that one drive and parse out the last line which is the free space.


----------



## Rockn (Jul 29, 2001)

This is a SQL management issue and how you are using tempdb's. Restarting the service is not the way to handle it.


----------



## Squashman (Apr 4, 2003)

Rockn said:


> This is a SQL management issue and how you are using tempdb's. Restarting the service is not the way to handle it.


SQL is not my expertise. So I am going to let you handle this one. If you need something in batch let me know. I will come back.


----------



## yura2201 (Jun 14, 2008)

@echo off 
freedisk /D C: 2GB 
set discspace=%errorlevel% 
if not %discspace%==0 ( 
if exist \\comp_name\c$ (net send comp_name "disk C < 2GB") 
) 

instead net send you can run you script...

also you can use Performance logs and Alerts in Win2003


----------

