Function Code to Convert Bytes to KB/MB/GB/TB

January 31st, 2010

http://sogeeky.blogspot.com/2007/04/vbscript-function-code-to-convert-bytes.html

When querying for disk space sizes using WMI, it returns the numbers in bytes. These large numbers in bytes do not make much sense until they are converted into Kilobytes (KB), Megabyte (MB), Gigabyte (GB), or Terabyte (TB) and so on. Quite frequently I need to convert these sizes in bytes to KB/MB/GB/TB for better interpretation. I therefore created a quick VBScript function which I call inside VBScript code whenever I need to convert numbers in bytes to KB/MB/GB/TB.

Function ConvertSize(Size)
Do While InStr(Size,",") 'Remove commas from size
CommaLocate = InStr(Size,",")
Size = Mid(Size,1,CommaLocate - 1) & _
Mid(Size,CommaLocate + 1,Len(Size) - CommaLocate)
Loop

Suffix = " Bytes"
If Size >= 1024 Then suffix = " KB"
If Size >= 1048576 Then suffix = " MB"
If Size >= 1073741824 Then suffix = " GB"
If Size >= 1099511627776 Then suffix = " TB"

Select Case Suffix
Case " KB" Size = Round(Size / 1024, 1)
Case " MB" Size = Round(Size / 1048576, 1)
Case " GB" Size = Round(Size / 1073741824, 1)
Case " TB" Size = Round(Size / 1099511627776, 1)
End Select

ConvertSize = Size & Suffix
End Function

qChange.vbs

January 27th, 2010

'Qchange.vbs

'Syntax:
'   cscript qchange.vbs changes.txt
'   File format: \\ServerName\Source_Printer;\\New_server\Source_printer

On Error Resume Next

Const VERBOSE = "N"   'Display progress (Y or N)
Const Title = "Print Connection Migrator"
Const ForReading = 1

Dim strDefaultPrinter
Dim InstalledPrinters 'Array of printer names
Dim Textfile          'File with print Q names
Dim OldPrintQueues()  'Dynamic array to store old print queue names, from the text file
Dim NewPrintQueues()  'Dynamic array to store new print queue names, from the text file
Dim fso         'File System Object
Dim objTextFile 'Text file object
Dim strNextLine 'Line of the text file
Dim i
Dim WshNetwork

Set WshNetwork = CreateObject("WScript.Network")

' 1. - Get the command line args        ###

SET Parameters = Wscript.arguments

'If no command line arguments provided, quit
If Parameters.Count = 0 Then
WScript.Quit(1)
Else
Textfile = Parameters.item(0)
End If

If Textfile = "" or Not Right(TextFile,4) = ".txt" or Not FileExist(Textfile) Then
Error=MsgBox("No valid input file provided. Stopping the script now.",vbokonly, Title)
WScript.Quit(1)
End If

If VERBOSE = "Y" Then Wscript.Echo "Reading input file"

' 2. Read the text file and import it in a Array    ###

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile (TextFile, ForReading)

i=-1
While not objTextFile.AtEndOfStream
i = i+1
Redim Preserve OldPrintQueues(i)
ReDim Preserve NewPrintQueues(i)
strLine = objTextFile.Readline
'Do not import the comment lines
If Left(strLine,2) = "\\" Then
OldPrintQueues(i) = Left(strLine,InStr(strline,";")-1)
If VERBOSE = "Y" Then Wscript.Echo " old Q is: " & OldPrintQueues(i)
NewPrintQueues(i) = Mid(strline,InStr(strline,";")+1,Len(strline))
If VERBOSE = "Y" Then Wscript.Echo " new Q is: " & NewPrintQueues(i)
End If
Wend

objTextFile.Close

' 3. Store the name of the default Printer        ###

strDefaultPrinter = DefaultPrinter
If VERBOSE = "Y" Then Wscript.Echo " Default is: " & strDefaultPrinter

' 4. WMI query for current printer connections    ###

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery _
("Select * from Win32_Printer")

' 5. Main Loop through printer collection         ###

For Each objPrinter in colInstalledPrinters
If VERBOSE = "Y" Then Wscript.Echo " Current connection: " & objPrinter.Name
If Left(objPrinter.Name, 2) = "\\" Then 'Work only On network printers
'Search the corresponding printer and create it
i = 0 'set the indice at the beginning of the array (prepare to loop)
'Wscript.Echo UBound(OldPrintQueues)
Do Until i > UBound(OldPrintQueues)
If UCase(objPrinter.Name) = UCase(OldPrintQueues(i)) Then
'Create the connection to the new printer
If VERBOSE = "Y" Then Wscript.Echo " New connection: " & NewPrintQueues(i)
WshNetwork.AddWindowsPrinterConnection NewPrintQueues(i)
If UCase(strDefaultPrinter) = UCase(objPrinter.Name) Then 'This is the default printer
'Set the default Printer
WshNetwork.SetDefaultPrinter NewPrintQueues(i)
End If
'Delete the printer connection
WshNetwork.RemovePrinterConnection objPrinter.Name
If VERBOSE = "Y" Then Wscript.Echo " Removing : " & objPrinter.Name
End If
i = i + 1
Loop
End If 'End of check for network printers
Next 'End of the loop through the printers of this user

Set WshNetwork = Nothing

'-----------------
' Functions
'-----------------

'Return the default printer
Function DefaultPrinter
Dim strComputer
Dim Result

strComputer = "."
Result = ""

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery _
("Select * from Win32_Printer")
For Each objPrinter in colInstalledPrinters
If objPrinter.Default = True Then
Result = objPrinter.Name
End If
Next
DefaultPrinter = Result
End Function

'-----------------

'Does File Exist (Boolean)
Function FileExist (FileFullPath)
Dim Fso
Set Fso = CreateObject("Scripting.FileSystemObject")
If (Fso.FileExists(FileFullPath)) Then
FileExist = True
Else
FileExist = False
End If
End Function

Hello world!

January 27th, 2010

Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!