Solved

VB Script help needed!

Posted on 2014-01-13
10
381 Views
Last Modified: 2014-09-19
I need to have a script that looks at a specific line in a ini file and depending on that line set the default printer.  I have the scripting for setting the printer figured out but cant come up with the If statements.  The Set default printer script should follow this rule

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("c:\CPSI\cptermw.ini", ForReading)

Do Until objTextfile.At EndOfStream
     strNextLine = objTextFile.Readline

intLineFinder = Instr(strNextLine, "TTYREAL")
If intLineFinder = 991 Then  
Set WSHNetwork = CreateObject ("WScript.Network")
WSHNetwork.SetDefaultPrinter "Specialty_Clinic_Check-In 1"

If intLineFinder =992 Then
Set WSHNetwork = CreateObject ("WScript.Network")
WSHNetwork.SetDefaultPrinter "Specialty_Clinic_Check-In_2"

If intLineFinder = 993 then
Set WSHNetwork = CreateObject ("WScript.Network")
WSHNetwork.SetDefaultPrinter "Specialty_Clinic_Check-In 1"
0
Comment
Question by:rcolving
  • 5
  • 4
10 Comments
 
LVL 10

Expert Comment

by:remmett70
ID: 39777773
Instead of an If statement.  You could use a Select Case statement


Set WSHNetwork = CreateObject ("WScript.Network")

intLineFinder = intLineFinder = Instr(strNextLine, "TTYREAL")

Select Case intLineFinder
      Case "991"  WSHNetwork.SetDefaultPrinter "Specialty_Clinic_Check-In 1"
      Case "992"  WSHNetwork.SetDefaultPrinter "Specialty_Clinic_Check-In_2"
      Case "993"  WSHNetwork.SetDefaultPrinter "Specialty_Clinic_Check-In 1"
End Select
0
 

Author Comment

by:rcolving
ID: 39779018
I'll give that a try will I still need -

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("c:\CPSI\cptermw.ini", ForReading)

Do Until objTextfile.At EndOfStream
     strNextLine = objTextFile.Readline
0
 

Author Comment

by:rcolving
ID: 39779081
Ok not working, shoot!  I'm a real novice a vb scripting so need a little guidance! There is a line in the ini file, filename is cpermw.ini, that I have to use to set the default printer.  The area in the ini file looks like the text below -

[TTY Configuration]
TTYREAL=

This value can be 991, 992, or 993.  I'm trying to get my script to look at the  TTYREAL value and want to set the default printer accordingly.  I like the idea of the Select case but can't get the script right to look at or find that line in the ini file located at c:\CPSI\cptermw.ini.  Any help would be appreciated.

I've attached the ini file and my vb script file.
Cptermw.ini
Set-Default-Printers-test.vbs
0
 
LVL 10

Expert Comment

by:remmett70
ID: 39779452
Try this

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("c:\CPSI\cptermw.ini")

Do Until objTextFile.AtEndOfStream
  strLine = objTextFile.Readline
    Select Case strLine
      Case "TTYREAL=991" WSHNetwork.SetDefaultPrinter "Specialty_Clinic_Check-In 1"
        Case "TTYREAL=992" WSHNetwork.SetDefaultPrinter "Specialty_Clinic_Check-In_2"
      Case "TTYREAL=993" WSHNetwork.SetDefaultPrinter "Specialty_Clinic_Check-In 1"
    End Select
Loop
objTextFile.Close
0
 

Author Comment

by:rcolving
ID: 39779543
I get the following Error
Line: 6
Char: 4
Error: Expected 'While', 'Until' or end of statement
Code: 800A0404
Source: Microsoft VBScript compilation err
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 10

Expert Comment

by:remmett70
ID: 39779638
Did you copy and Paste or type this into your script manually?  That error is acting like there is a mistype in the Do line.

Make sure that line is
 
Do Until
0
 

Author Comment

by:rcolving
ID: 39779749
You were right had an extra character so I copy and pasted now get-

Line: 8
Char: 28
Error: Object required: "
Code: 800A01A8
Source: Microsoft VBScript runtime error


I've attached screen print of printers
Printers.docx
0
 
LVL 10

Accepted Solution

by:
remmett70 earned 250 total points
ID: 39779879
Add this to the beginning of the script.

Set WSHNetwork = CreateObject ("WScript.Network")
0
 

Author Closing Comment

by:rcolving
ID: 39779944
Remmett70 was patient and very helpful with my attempt at vb scripting.  Couldn't have done it without him/her.
0
 

Expert Comment

by:TFHDIT
ID: 40333286
@rcolving Here is a PowerShell version.

if ([string]$r = Select-String \\path\to\the\file.ini -Pattern $env:COMPUTERNAME -Context 0,1) {
    $tty = $r.substring($r.indexOf("=")+1,3)
}

Open in new window

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
powerN  challenge 3 49
Sorting in Excel with Group Headers if the Exist 2 70
changeXy challenge 13 58
topping3 challenge 14 52
Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

743 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

17 Experts available now in Live!

Get 1:1 Help Now