?
Solved

Add date and time, and overwrite created file in vbs

Posted on 2011-04-20
4
Medium Priority
?
491 Views
Last Modified: 2012-08-13
All,
I'm really hoping someone can help me out here. The attached script is a vbs that queries the diskspace on servers from a list in a plain text file called Server_List.txt. It works great and produces a really nice htm output. However I need to make some changes to it.
I am planning on running this script in a scheduled task and need the script to display the last date and time that it ran at the top of the webpage output near the title, I also I need it to overwrite the output file that it creates as currently it is appending to the end of the created file.

Hope someone can help me out with this.

Regards,
Karl.
Script originally from The Script Guys website Diskspace.txt
0
Comment
Question by:karlpearson
  • 3
4 Comments
 
LVL 12

Expert Comment

by:prashanthd
ID: 35431707
Try the following

regards
Prashanth
On Error Resume Next 
Const ForWriting = 2
Const HARD_DISK = 3 
Const ForReading = 1 
 
'Declaring the variables 
Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set SrvList = objFSO.OpenTextFile("Server_List.txt", ForReading) 
Set ReportFile = objFSO.OpenTextFile ("Diskspace_status.html", ForWriting, True) 
i = 0 
 
'Initializing the HTML Tags for better formatting 
ReportFile.writeline("<html>") 
ReportFile.writeline("<head>") 
ReportFile.writeline("<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>") 
ReportFile.writeline("<title>" & "Server Disk Space Report</title>") 
ReportFile.writeline("<style type='text/css'>") 
ReportFile.writeline("<!--") 
ReportFile.writeline("td {") 
ReportFile.writeline("font-family: Tahoma;") 
ReportFile.writeline("font-size: 11px;") 
ReportFile.writeline("border-top: 1px solid #999999;") 
ReportFile.writeline("border-right: 1px solid #999999;") 
ReportFile.writeline("border-bottom: 1px solid #999999;") 
ReportFile.writeline("border-left: 1px solid #999999;") 
ReportFile.writeline("padding-top: 0px;") 
ReportFile.writeline("padding-right: 0px;") 
ReportFile.writeline("padding-bottom: 0px;") 
ReportFile.writeline("padding-left: 0px;") 
ReportFile.writeline("}") 
ReportFile.writeline("body {") 
ReportFile.writeline("margin-left: 5px;") 
ReportFile.writeline("margin-top: 5px;") 
ReportFile.writeline("margin-right: 0px;") 
ReportFile.writeline("margin-bottom: 10px;") 
ReportFile.writeline("") 
ReportFile.writeline("table {") 
ReportFile.writeline("border: thin solid #000000;") 
ReportFile.writeline("}") 
ReportFile.writeline("-->") 
ReportFile.writeline("</style>") 
ReportFile.writeline("</head>") 
ReportFile.writeline("<body>") 
 
ReportFile.writeline("<table width='50%'>") 
ReportFile.writeline("<tr bgcolor='#CCCCCC'>") 
ReportFile.writeline("<td colspan='7' height='25' align='center'>") 
ReportFile.writeline("<font face='tahoma' color='#003399' size='2'><strong>Server Disk Space Report"&Now&"</strong></font>") 
ReportFile.writeline("</td>") 
ReportFile.writeline("</tr>") 
ReportFile.writeline("</table>") 
 
 
'Declaring the Server Name for report generation 
Do Until SrvList.AtEndOfStream 
    StrComputer = SrvList.Readline 
 
    ReportFile.writeline("<table width='50%'><tbody>") 
    ReportFile.writeline("<tr bgcolor='#CCCCCC'>") 
    ReportFile.writeline("<td width='50%' align='center' colSpan=6><font face='tahoma' color='#003399' size='2'><strong>" & StrComputer & "</strong></font></td>") 
    ReportFile.writeline("</tr>") 
 
 
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") 
    Set colDisks = objWMIService.ExecQuery("Select * from Win32_LogicalDisk Where DriveType = " & HARD_DISK & "") 
 
    ReportFile.writeline("<tr bgcolor=#CCCCCC>") 
        ReportFile.writeline("<td width='05%' align='center'>Drive / Mount</td>") 
        ReportFile.writeline("<td width='05%' align='center'>Total Capacity (in GB)</td>") 
        ReportFile.writeline("<td width='05%' align='center'>Used Capacity (in GB)</td>") 
        ReportFile.writeline("<td width='05%' align='center'>Free Space (in GB)</td>") 
        ReportFile.writeline("<td width='05%' align='center'>Freespace %</td>") 
    ReportFile.writeline("</tr>") 
 
        'Starting the loop to gather values from all Hard Drives 
        For Each objDisk in colDisks 
 
            'Delcaring the Variables 
 
            TotSpace=Round(((objDisk.Size)/1073741824),2) 
            FrSpace=Round(objDisk.FreeSpace/1073741824,2) 
            FrPercent=Round((FrSpace / TotSpace)*100,0) 
            UsSpace=Round((TotSpace - FrSpace),2) 
            Drv=objDisk.DeviceID 
            VolName=objDisk.DeviceID 
 
            'Lnt=Len(VolName) 
 
            'If  Len(VolName) =  3 then 
                If FrPercent > 20 Then 
                    ReportFile.WriteLine "<tr><td align=center>" & Drv & "</td><td align=center>" & TotSpace & "</td><td align=center>" & UsSpace & "</td><td align=center>" & FrSpace & "</td><td BGCOLOR='#00FF00' align=center>" & FrPercent & "%" &"</td></tr>" 
                ElseIf FrPercent < 10 Then 
                    ReportFile.WriteLine "<tr><td align=center>" & Drv & "</td><td align=center>" & TotSpace & "</td><td align=center>" & UsSpace & "</td><td align=center>" & FrSpace & "</td><td bgcolor='#FF0000' align=center>" & FrPercent & "%" &"</td></tr>" 
                Else 
                    ReportFile.WriteLine "<tr><td align=center>" & Drv & "</td><td align=center>" & TotSpace & "</td><td align=center>" & UsSpace & "</td><td align=center>" & FrSpace & "</td><td bgcolor='#FBB917' align=center>" & FrPercent & "%" &"</td></tr>" 
                End If 
            'Else 
            'End If 
        Next 
 
    ReportFile.writeline("<tr>") 
    ReportFile.writeline("<td width='50%' colSpan=6>&nbsp;</td>") 
    ReportFile.writeline("</tr>") 
 
    ReportFile.writeline("</tbody></table>") 
Loop 
ReportFile.WriteLine "</body></html>"

Open in new window

0
 
LVL 12

Accepted Solution

by:
prashanthd earned 2000 total points
ID: 35431721
Replace line no 48 with following

ReportFile.writeline("<font face='tahoma' color='#003399' size='2'><strong>Server Disk Space Report<br>"&Now&"</strong></font>")
0
 

Author Closing Comment

by:karlpearson
ID: 35431747
Brilliant stuff thank you so much sir, I just couldn't get my head round that bit.
Thanks again.
0
 
LVL 12

Expert Comment

by:prashanthd
ID: 35431806
Thanks for the grade.

regards
Prashanth
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses
Course of the Month15 days, 19 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question