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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to add a header to a text file

Status
Not open for further replies.

gmagerr

Technical User
Aug 11, 2001
323
US
Hi,

I have two scripts. One gets me drive space for a list of servers, and the other will add a header to the top of the text file created by the first script. If I run them as individual scripts, it works fine. When I try to add the script that writes the header into the script that generates the disk report, the output file is blank. Here are the two scripts, then combined

Code:
'==========================================================================
Option Explicit
On Error Resume Next
'==========================================================================
' VARIABLE DECLARATIONS
'==========================================================================
Dim objShell, objNetwork, objFSO
Dim objWMIService, objItem, colItems
Dim strDriveType, strDiskSize, txt
Dim oFSO,  oFile, sText, objTextFile, strComputer
Dim pctFreeSpace,strFreeSpace,strusedSpace

Set oFSO  = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
Set objNetwork = WScript.CreateObject("WScript.Network")
Set objFSO = CreateObject("Scripting.FilesystemObject")
Set objTextFile = objFSO.CreateTextFile(strReport)

'==========================================================================
' STATIC VARIABLE ASSIGNMENTS
'==========================================================================
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const strReport = "DriveReport.txt"
Const sFile  = "Servers.txt"

'==========================================================================
' MAIN SCRIPT CODE
'==========================================================================
If  oFSO.FileExists(sFile) Then
Set oFile  = oFSO.OpenTextFile(sFile, 1)

Do While Not  oFile.AtEndOfStream
	sText = oFile.ReadLine

If Trim(sText) <> "" Then
	strComputer = sText
	Set  objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
	Set  colItems =  objWMIService.ExecQuery("Select * from Win32_LogicalDisk WHERE  DriveType=3")

txt = ""
'	txt = vbcrlf & "==============================================================" & vbcrlf
'	txt = txt  & vbCrLf & "Server" & vbTab & vbtab  & "Drive" & vbtab  & "Size" & vbtab  & "Used" & vbtab  & "Free" & vbtab  & "Free(%)"
'	txt = txt  & vbcrlf & "==============================================================" & vbcrlf

For Each  objItem in  colItems
	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 & sText & vbTab & objItem.Name & vbtab  & strDiskSize & vbtab  & strUsedSpace & vbTab  & strFreeSpace & vbtab  & pctFreeSpace & vbCrLf
Next

objTextFile.Write(txt)

End If

Loop
objTextFile.Close
oFile.Close

Else
WScript.Echo "There  was no servers.txt file found. " & vbCrLf  & "Please create it with  a list of the servers you want to monitor."

End If

'==========================================================================
' SUBS AND FUNCTIONS
'==========================================================================

Now the header script

Code:
Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("DriveReport.txt", ForReading)

strContents = objFile.ReadAll
objFile.Close

strFirstLine = "Server" & vbTab & vbtab  & "Drive" & vbtab  & "Size" & vbtab  & "Used" & vbtab  & "Free" & vbtab  & "Free(%)"

strNewContents = strFirstLine & vbCrLf & strContents

Set objFile = objFSO.OpenTextFile("DriveReport.txt", ForWriting)
objFile.WriteLine strNewContents

objFile.Close

Now combined. This produces a blank DriveReport.txt

Code:
'==========================================================================
Option Explicit
On Error Resume Next
'==========================================================================
' VARIABLE DECLARATIONS
'==========================================================================
Dim objShell, objNetwork, objFSO
Dim objWMIService, objItem, colItems
Dim strDriveType, strDiskSize, txt
Dim oFSO,  oFile, sText, objTextFile, strComputer
Dim pctFreeSpace,strFreeSpace,strusedSpace

Set oFSO  = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
Set objNetwork = WScript.CreateObject("WScript.Network")
Set objFSO = CreateObject("Scripting.FilesystemObject")
Set objTextFile = objFSO.CreateTextFile(strReport)

'==========================================================================
' STATIC VARIABLE ASSIGNMENTS
'==========================================================================
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const strReport = "DriveReport.txt"
Const sFile  = "Servers.txt"

'==========================================================================
' MAIN SCRIPT CODE
'==========================================================================
If  oFSO.FileExists(sFile) Then
Set oFile  = oFSO.OpenTextFile(sFile, 1)

Do While Not  oFile.AtEndOfStream
	sText = oFile.ReadLine

If Trim(sText) <> "" Then
	strComputer = sText
	Set  objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
	Set  colItems =  objWMIService.ExecQuery("Select * from Win32_LogicalDisk WHERE  DriveType=3")

txt = ""
'	txt = vbcrlf & "==============================================================" & vbcrlf
'	txt = txt  & vbCrLf & "Server" & vbTab & vbtab  & "Drive" & vbtab  & "Size" & vbtab  & "Used" & vbtab  & "Free" & vbtab  & "Free(%)"
'	txt = txt  & vbcrlf & "==============================================================" & vbcrlf

For Each  objItem in  colItems
	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 & sText & vbTab & objItem.Name & vbtab  & strDiskSize & vbtab  & strUsedSpace & vbTab  & strFreeSpace & vbtab  & pctFreeSpace & vbCrLf
Next

objTextFile.Write(txt)

End If

Loop
objTextFile.Close
oFile.Close

Else
WScript.Echo "There  was no servers.txt file found. " & vbCrLf  & "Please create it with  a list of the servers you want to monitor."

End If

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("DriveReport.txt", ForReading)

strContents = objFile.ReadAll
objFile.Close

strFirstLine = "Server" & vbTab & vbtab  & "Drive" & vbtab  & "Size" & vbtab  & "Used" & vbtab  & "Free" & vbtab  & "Free(%)"

strNewContents = strFirstLine & vbCrLf & strContents

Set objFile = objFSO.OpenTextFile("DriveReport.txt", ForWriting)
objFile.WriteLine strNewContents

objFile.Close

'==========================================================================
' SUBS AND FUNCTIONS
'==========================================================================
 
Why not simply write the header just after creating the file but before starting the loop ????

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Also it looks like you are running this line:
Set objTextFile = objFSO.CreateTextFile(strReport)

...before you have defined the strReport constant.

Also I would remove the "On Error Resume Next
 
PHV,

I've been trying to add the header in the script all morning. I am not sure how to get it to insert outside of the loop. I'
ve moved it all around, but with no success. It always seems to wind up in the loop somehow. If I move it about the "Do While Not" the file comes out blank. I'm stumpped on this one. Should be simple
 
Got it! Thanks guys, here's the change

Code:
'==========================================================================
' MAIN SCRIPT CODE
'==========================================================================
Set objTextFile = objFSO.CreateTextFile(strReport)

	txt = ""
	txt = txt & "Server" & vbTab & vbtab  & "Drive" & vbtab  & "Size" & vbtab  & "Used" & vbtab  & "Free" & vbtab  & "Free(%)" & vbCrLf 
	
objTextFile.Write(txt)

If  oFSO.FileExists(sFile) Then
Set oFile  = oFSO.OpenTextFile(sFile, 1)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top