df for windoze

As I now seem to spend more time on windows than linux these days I’ve been looking at ways to get the information I’m used to getting. There is, for example, something called ‘grep for windows’. Then there’s cygwin of course or, better still, virtual box (see a few posts down).

When I asked if there was something like ‘df” someone sent me a vbscript file. Being a vbscript newb I didn’t know what to do with it but it turns out you just bung it in a text file, give it a ‘.vbs’ extension and, Bob’s your uncle (or in my case My Dad actually) and you can then just double-click to run it. I’ve played around with the script a little just to get a better understanding of how it works and it does exactly what I want now. Here it is if you’re interested:

Option Explicit

const strComputer = "."
const strReport = "C:\windows\temp\drives.txt"

Dim objWMIService, objItem, colItems, strDriveType, strDiskSize, txt, xCount, yCount

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_LogicalDisk")
txt = "Drive " & String(52," ") & vbtab & "Size" & vbtab & "Used" & vbtab & "Free" & vbtab & "Free(%)" & vbtab & "Amount Used" & vbcrlf & String(110,"~") & vbcrlf
For Each objItem in colItems

DIM pctFreeSpace,strFreeSpace,strusedSpace, driveName, padString, padLength, volumeName, pctString, numPctFreeSpace

padLength = 0
driveName = objItem.ProviderName
IF ISNULL(objItem.Description) THEN
  volumeName=" "
ELSE
  volumeName = objItem.Description
END IF
IF ISNULL(objItem.ProviderName) THEN
  driveName=volumeName
ELSE
  driveName = objItem.ProviderName & " " & volumeName
END IF
padLength = (58 - Len(driveName))
padString = String(padLength," ")
driveName = driveName & padString
pctFreeSpace = INT((objItem.FreeSpace / objItem.Size) * 1000)/10
strDiskSize = Int(objItem.Size /1073741824)

IF NOT ISNULL(strDiskSize) THEN
  strDiskSize = strDiskSize & "Gb"
ELSE
  strDiskSize = "-"
END IF
strFreeSpace = Int(objItem.FreeSpace /1073741824)
IF NOT ISNULL(strFreeSpace) THEN
  strFreeSpace = strFreeSpace & "Gb"
ELSE
  strFreeSpace = "-"
END IF
strUsedSpace = Int((objItem.Size-objItem.FreeSpace)/1073741824)
IF NOT ISNULL(strUsedSpace) THEN
  strUsedSpace = strUsedSpace & "Gb"
ELSE
  strUsedSpace = "-"
END IF
IF ISNULL(pctFreeSpace) THEN
  pctFreeSpace = "-"
  numPctFreeSpace = 100
ELSE
  numPctFreeSpace = 100 - Int(pctFreeSpace)
END IF

xCount = Round((numPctFreeSpace)/10)
If xCount < 0 THEN xCount = 0 
  ELSEIF xCount > 10 THEN
  xCount = 10
END IF
yCount = 10 - xCount

pctString= "[" & String(xCount,"X") & String(yCount,"-") & "]"
txt = txt & objItem.Name & " " & driveName & vbtab & strDiskSize & vbtab & strUsedSpace & vbtab & strFreeSpace & vbtab & pctFreeSpace & vbtab & pctString & vbtab & vbcrlf
Next

writeTextFile txt, strReport
wscript.echo "Report written to " & strReport & vbcrlf & vbcrlf

' Procedure to write output to a text file
private sub writeTextFile(byval txt,byval strTextFilePath)
Dim objFSO,objTextFile
set objFSO = createobject("Scripting.FileSystemObject")
set objTextFile = objFSO.CreateTextFile(strTextFilePath)
objTextFile.Write(txt)
objTextFile.Close
SET objTextFile = nothing
end sub

Actually I’ve just discovered the joys of powershell and it IS a definite joy. It comes with a lot of unixy commands or equivalents (e.g. ‘ls -l | grep winamp‘ in linux becomes ‘ls | findstr Winamp‘ in powershell) and it’s pretty straight-forward to write something that does ‘df’ as this chap demonstrates here: http://binarynature.blogspot.co.uk/2010/04/powershell-version-of-df-command.html

Look around for what you can do with powershell – you might be pleasantly surprised.

Advertisements

About dnadba

I work with databases and stuff
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s