Solved

VBScript: Output to screen

Posted on 2010-09-06
3
1,788 Views
Last Modified: 2012-05-10
Hi there,

I have this script that outputs to a file (Thanks Psy053).

I need it to output on screen so I can use the datas in a batch file.

@ECHO OFF
FOR /F "tokens=1-10 delims=," %%A IN ('cscript GetVolumeInfos.vbs') DO ECHO %%A,%%B,%%C,%%D,%%E,%%F,%%G,%%H,%%I,%%J
PAUSE

I know this batch file is kind of useless, but it's just to test the VBScript.

REF: http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_26454651.html

Thanks for yur help,
Rene


strComputer = "."

strFileName = "DiskSpace.log"

Const Gig = 1073741824



Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFile = objFSO.OpenTextFile(strFileName,8,True)



Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colOS = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")

Set colDisks = objWMIService.ExecQuery ("Select * from Win32_LogicalDisk")



For each objOS in colOS

	strMajorVersion = Left(objOS.Version,3)

Next



For Each objDisk in colDisks



	If Not IsNull(objDisk.Size / Gig) Then retUFSize = FormatNumber(objDisk.Size / Gig,2)

	If Not IsNull(objDisk.FreeSpace / Gig) Then retUFFree = FormatNumber(objDisk.FreeSpace / Gig,2)



	retDT = DriveType(objDisk.DriveType)



	If strMajorVersion < 5.2 Then

		retFrag = "N/A, N/A"

	Else

		retFrag = Fragmentation(objDisk.DeviceID, objDisk.DriveType)

	End If



	objFile.WriteLine "" & _

		objDisk.DeviceID & ", " & _

		objDisk.DriveType & ", " & _

		retDT & ", " & _

		objDisk.FileSystem & ", " & _

		objDisk.Size & ", " & _

		retUFSize & ", " & _

		objDisk.FreeSpace & ", " & _

		retUFFree & ", " & _

		retFrag



Next

objFile.Close





Function Fragmentation(strDID, strDT)

If strDT = 3 Then

	Set colVolumes = objWMIService.ExecQuery ("Select * from Win32_Volume Where (DriveLetter='" & strDID & "')")

	For Each objVolume in colVolumes

		ret = objVolume.DefragAnalysis(boolDefrag, objReport)

		If ret = 0 Then

			Fragmentation = objReport.TotalPercentFragmentation & "%, " & objReport.FilePercentFragmentation & "%"

		Else

			Fragmentation = "Error, Error"

		End If

	Next

Else

	Fragmentation = "N/A, N/A"

End If



End Function







Function DriveType(strDT)



Select Case strDT

Case "0"

	DriveType = "Unknown"

Case "1"

	DriveType = "No_Root_Directory"

Case "2"

	DriveType = "Removable_Disk"

Case "3"

	DriveType = "Local_Disk"

Case "4"

	DriveType = "Network_Drive"

Case "5"

	DriveType = "Compact_Disk"

Case "6"

	DriveType = "RAM_Disk"

End Select 



End Function

Open in new window

0
Comment
Question by:ReneGe
3 Comments
 
LVL 52

Accepted Solution

by:
Bill Prew earned 450 total points
ID: 33614694
Try this please.

~bp
strComputer = "."
strFileName = "DiskSpace.log"
Const Gig = 1073741824
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colOS = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
Set colDisks = objWMIService.ExecQuery ("Select * from Win32_LogicalDisk")
 
For each objOS in colOS
        strMajorVersion = Left(objOS.Version,3)
Next
 
For Each objDisk in colDisks
 
        If Not IsNull(objDisk.Size / Gig) Then retUFSize = FormatNumber(objDisk.Size / Gig,2)
        If Not IsNull(objDisk.FreeSpace / Gig) Then retUFFree = FormatNumber(objDisk.FreeSpace / Gig,2)
 
        retDT = DriveType(objDisk.DriveType)
 
        If strMajorVersion < 5.2 Then
                retFrag = "N/A, N/A"
        Else
                retFrag = Fragmentation(objDisk.DeviceID, objDisk.DriveType)
        End If
 
        Wscript.Echo "" & _
                objDisk.DeviceID & ", " & _
                objDisk.DriveType & ", " & _
                retDT & ", " & _
                objDisk.FileSystem & ", " & _
                objDisk.Size & ", " & _
                retUFSize & ", " & _
                objDisk.FreeSpace & ", " & _
                retUFFree & ", " & _
                retFrag
 
Next
 
 
Function Fragmentation(strDID, strDT)
If strDT = 3 Then
        Set colVolumes = objWMIService.ExecQuery ("Select * from Win32_Volume Where (DriveLetter='" & strDID & "')")
        For Each objVolume in colVolumes
                ret = objVolume.DefragAnalysis(boolDefrag, objReport)
                If ret = 0 Then
                        Fragmentation = objReport.TotalPercentFragmentation & "%, " & objReport.FilePercentFragmentation & "%"
                Else
                        Fragmentation = "Error, Error"
                End If
        Next
Else
        Fragmentation = "N/A, N/A"
End If
 
End Function
 
 
 
Function DriveType(strDT)
 
Select Case strDT
Case "0"
        DriveType = "Unknown"
Case "1"
        DriveType = "No_Root_Directory"
Case "2"
        DriveType = "Removable_Disk"
Case "3"
        DriveType = "Local_Disk"
Case "4"
        DriveType = "Network_Drive"
Case "5"
        DriveType = "Compact_Disk"
Case "6"
        DriveType = "RAM_Disk"
End Select 
 
End Function

Open in new window

0
 
LVL 14

Assisted Solution

by:Psy053
Psy053 earned 50 total points
ID: 33614740
Why do you need to use the data in a batch file? I might be able to wrap it all up in a VBScript.
0
 
LVL 10

Author Comment

by:ReneGe
ID: 33614848
==>Psy053
Thanks for asking. My batch file is quite large and I would not feel comfortable to ask for this in a single thread.

However, I will soon start to learn VBScript and will convert this batch file (and more) to VBScript. I will then post multiple threads in this regards.

==>bp
Thanks for helping. It worked without a sweat.


Thanks to you both and lots of cheers...,
Rene

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 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

Need Help in Real-Time?

Connect with top rated Experts

28 Experts available now in Live!

Get 1:1 Help Now