Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Need Help (New to VBS)

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,64178
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Need Help (New to VBS)
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Need Help (New to VBS) - 9/10/2008 3:16:48 PM   
  JoshJarvis

 

Posts: 5
Score: 0
Joined: 9/10/2008
Status: offline
My Problem...


I am just trying to have the script ONLY write the text file if there is a local printer...If there are no local printers installed I do not want a text file created.  As of right now it does create a text file called: LocalPrinters.txt if there are local printers found (example): PDFCreatorHP Deskjet5250HP Laserjet4200 but it is also creating a BLANK text file called: LocalPrinters.txt if there are no local printers found. Is there a way to script it so that if there are no Local printers found it will just exit with something like: Const ForWriting = 2
strComputer = "."
Set objNetwork = CreateObject("Wscript.Network")
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer")
For Each objPrinter in colPrinters
If objPrinter.Attributes And 64 Then
     strText = objPrinter.Name   Else
     wscript.quit

      End If
Next
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile _
("C:\LocalPrinters.txt", ForWriting, True)
objFile.Write strText
objFile.Close



< Message edited by JoshJarvis -- 9/11/2008 1:57:03 AM >


_____________________________

Josh Jarvis
 
 
Post #: 1
 
 RE: Need Help (New to VBS) - 9/10/2008 5:06:18 PM   
  ehvbs

 

Posts: 2201
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi JoshJarvis,

assuming that the file should contain all local printers and that after the deinstallation
of all local printers there should be no file with stale information hanging around:

Const ForWriting = 2
Cinst csFSpec = "C:\LocalPrinters.txt"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set dicNames = CreateObject( "Scripting.Dictionary" )

strComputer = "."
Set objNetwork = CreateObject("Wscript.Network")
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer")
For Each objPrinter in colPrinters
      If objPrinter.Attributes And 64 Then
          dicData(  objPrinter.Name ) = ""
      End If
Next

If 0 < dicData.Count Then
   objFSO.CreateTextFile( csFSpec, True ) .Write Join( dicData.Keys, vbCrLf )
Else
   If objFSO.FileExists( csFSpec ) Then objFSO.DeleteFile csFSpec
End If

[Not tested]

Please double check my and your usage of - and the Docs for - .CreateTextFile!

Good luck!

ehvbs

(in reply to JoshJarvis)
 
 
Post #: 2
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Need Help (New to VBS) Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts