Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBS and formatting using bold or colours

Status
Not open for further replies.

JC_123

Technical User
Dec 2, 2018
5
GB
The following code (taken from displays a dialog window with the results of space of hard drives. Is it possible from this code to make the end column of the dialog window (not the textfile as that can't be done) that shows (Free(%)) to be bold or the colour red if the % is below 10% for example? That way you can see visually and quickly that this hard drive is going to be low on space soon? I hope this can be done but I don't know enough about VBS and formatting when dialog windows are displayed.

Option Explicit

const strComputer = "."
const strReport = "c:\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 & "Size" & vbtab & "Used" & vbtab & "Free" & vbtab & "Free(%)" & vbcrlf
For Each objItem in colItems

DIM pctFreeSpace,strFreeSpace,strusedSpace

pctFreeSpace = INT((objItem.FreeSpace / objItem.Size) * 1000)/10
strDiskSize = Int(objItem.Size /1073741824) & "Gb"
strFreeSpace = Int(objItem.FreeSpace /1073741824) & "Gb"
strUsedSpace = Int((objItem.Size-objItem.FreeSpace)/1073741824) & "Gb"
txt = txt & objItem.Name & vbtab & strDiskSize & vbtab & strUsedSpace & vbTab & strFreeSpace & 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
 
Er … your code doesn't appear to display a dialog box at all. Just writes to a text file. And, as you yourself observe, you can't apply formats to a pure text file.
 
Hi stongm

When I run this vbs code the dialog window appears. Commenting out the line:
wscript.echo "Report written to " & strReport & vbcrlf & vbcrlf & txt
will just write to a textfile and not display the window... but that is enabled above.
space_pg5jrl.jpg


Is it possible to apply formats to the dialog window when the Free(%) column is less than 10%?
 
Ah, sure - but that's not really a dialog box. Or, Ok, yes it is, in the sense that it is a standard Windows dialog window, but from vbscript's point of view it is simply a simple text display The underlying engine - wscript or cscript - provides this (and in cscript's case this would be in a command window rather than a graphical window), but your script has no idea how it is actually being displayed, and has no capability to interact with it. So no, you can't change the colour. You probably need to investigate HTAs. Or writing the output as HTML and displaying in a browser window (which is sort of what an HTA would allow as well)
 
Many thanks to all for helping me in this question. I have just left it as a text file and the prompt that shows is fine for now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top