Hi, I'm a beginner in VBscript and I got a script which obtains disk space usage of local drives. However, when some columns would contain long numeric value, some adjacent columns and even values are moving to the right and thus makes the output disorganized. I already
Please see below the contents of the script:
Option Explicit
const strComputer = "."
const strReport = "F:\dba_scripts\diskspace.txt"
Dim objWMIService, objItem, colItems
Dim strDriveType, strDiskSize, txt
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_LogicalDisk WHERE DriveType=3")
txt = "DRIVE" & vbtab & vbtab & "SIZE" & vbtab & vbtab & "USED" & vbtab & vbtab & "FREE" & vbtab & vbtab & "FREE(%)" & vbcrlf
For Each objItem in colItems
DIM pctFreeSpace,strFreeSpace,strusedSpace
pctFreeSpace = INT((objItem.FreeSpace / objItem.Size) * 1000)/10
strDiskSize = round((objItem.Size /1073741824),1) & " GB"
strFreeSpace = round((objItem.FreeSpace /1073741824),1) & " GB"
strUsedSpace = round(((objItem.Size-objItem.FreeSpace)/1073741824),1) & " GB"
txt = txt & objItem.Name & vbtab & vbtab & strDiskSize & vbtab & vbtab & strUsedSpace & vbTab & vbtab & strFreeSpace & vbtab & vbtab & pctFreeSpace & vbcrlf
Next
writeTextFile txt,strReport
wscript.echo "Report written to " & strReport & vbcrlf & vbcrlf & txt
' 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
The output file looks OK but when I send/email it using the free bmail, the results are disorganized (meaning some columns and values moved to the right.
My question is are there ways to make the columns and values results fixed ( meaning no columns and values are moving to the right )?
-
You could write out a table using HTML. This should work in an email.
-
Function RightJustified(ColumnValue, ColumnWidth) RightJustified = Space(ColumnWidth - Len(ColumnValue)) & ColumnValue End Function
Usage example:
output = output & _ RightJustified(strDiskSize, 15) & _ RightJustified(strUsedSpace, 15) & _ RightJustified(strFreeSpace, 15) & _ RightJustified(pctFreeSpace, 15) & _ vbCrLf
EDIT
Add the RightJustified function to your script.
Then, replace this line of your code:
txt = txt & objItem.Name & vbtab & vbtab & strDiskSize & vbtab & vbtab & strUsedSpace & vbTab & vbtab & strFreeSpace & vbtab & vbtab & pctFreeSpace & vbcrlf
with:
txt = txt & objItem.Name & _ RightJustified(strDiskSize, 15) & _ RightJustified(strUsedSpace, 15) & _ RightJustified(strFreeSpace, 15) & _ RightJustified(pctFreeSpace, 15) & _ vbCrLf
EDIT 2
I added the RightJustified function at the bottom of your script, and then called it within your loop to format the columns. I also used it on the column headers. Below is the script and at the bottom is the output on my machine.
Option Explicit const strComputer = "." const strReport = "F:\dba_scripts\diskspace.txt" Dim objWMIService, objItem, colItems Dim strDriveType, strDiskSize, txt Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colItems = objWMIService.ExecQuery("Select * from Win32_LogicalDisk WHERE DriveType=3") txt = RightJustified("DRIVE", 10) & _ RightJustified("SIZE", 15) & _ RightJustified("USED", 15) & _ RightJustified("FREE", 15) & _ RightJustified("FREE(%)", 15) & _ vbCrLf For Each objItem in colItems DIM pctFreeSpace,strFreeSpace,strusedSpace pctFreeSpace = INT((objItem.FreeSpace / objItem.Size) * 1000)/10 strDiskSize = round((objItem.Size /1073741824),1) & " GB" strFreeSpace = round((objItem.FreeSpace /1073741824),1) & " GB" strUsedSpace = round(((objItem.Size-objItem.FreeSpace)/1073741824),1) & " GB" txt = txt & _ RightJustified(objItem.Name, 10) & _ RightJustified(strDiskSize, 15) & _ RightJustified(strUsedSpace, 15) & _ RightJustified(strFreeSpace, 15) & _ RightJustified(pctFreeSpace, 15) & _ vbCrLf Next writeTextFile txt,strReport wscript.echo "Report written to " & strReport & vbcrlf & vbcrlf & txt ' Procedure to write output to a text file 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 Function RightJustified(ColumnValue, ColumnWidth) RightJustified = Space(ColumnWidth - Len(ColumnValue)) & ColumnValue End Function
Output produced:
DRIVE SIZE USED FREE FREE(%) C: 48.4 GB 40.6 GB 7.8 GB 16.1 D: 100.6 GB 56.8 GB 43.8 GB 43.5
Grant Wagner : +1 as long as the E-mail is sent and received using a fixed-width font.aphoria : Good point. Assuming the email is sent as plaintext, I think most readers will display it with a fixed-width font.titanium : Thanks for the answer. How do I implement the function in the script I provided?titanium : I've tested sending the output as body of message, but the output is not properly displayed (meaning there are still columns and values not moving to the right )titanium : the output looks like this: DRIVE SIZE USED FREE FREE(%) C: 16 GB 11.2 GB 4.8 GB 29.7 D: 51.8 GB 2.5 GB 49.3 GB 95 E: 279.4 GB 117.8 GB 161.6 GB 57.8 F: 201 GB 35.1 GB 165.9 GB 82.5 G: 151 GB 50.4 GB 100.6 GB 66.6 H: 100 GB 3.3 GB 96.7 GB 96.6 I: 101 GB 2.2 GB 98.8 GB 97.8 J: 534.6 GB 400.6 GB 134 GB 25titanium : Sorry for posting the output , i don't know it's not being displayed "as is". I tested it but the columns are not aligned so I just adjusted the values which I don't think is not a good way. Is there a way to include the columns as part of the rightjustified function?titanium : Thanks. Is it now working in some servers. However, in one server I encountered "VBScript runtime error: Invalid use of Null: 'round' ". I noticed one drive has filesystem of RAW and is "not ready". This could be the cause of the error though I'm not so sure. If I'm right, is there a way to include in the script that this kind of drive should not be scanned? THere is also one 2 servers that run on Windows NT 4 which does not have cscript to run vbscript. Is there an alternative tool to rn vbscript in Windows NT?aphoria : I happen to have one remaining NT Server at work, so I checked and it does have Windows Script Host 5.6 installed (which includes WSCRIPT.EXE and CSCRIPT.EXE). I think you may need to have at least IE4 installed to get it.aphoria : Check the objItem.FileSystem property to make sure it is not RAW. Or, maybe it would be better to check that it is FAT, FAT32, or NTFS.
0 comments:
Post a Comment