Monday, May 24, 2010

Calculate free space on drive using DOS

Run the following from SQL Server to check out the free space amount on a drive.

1. @echo off >nullfile.txt

2. set Drive=C:
3. for /F "tokens=7" %%a in ('fsutil volume diskfree %Drive% ^find /i  "# of free" ')
4.       do set free=%%a
5. set free=%free:~0,-6%
6. echo c: %free%


Now, the following will run a bat file to get free space on a drive and write the results to a table.

  • IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.tables WHERE table_name = 'FreeSpace') DROP TABLE FreeSpace


  • CREATE TABLE FreeSpace ( SpaceInMB varchar(100) NULL )

  • INSERT INTO FreeSpace EXEC master..xp_CMDShell 'C:\Scripts\FreeSpace.bat'

No comments:

Post a Comment